Skip to content

Commit fe0afd6

Browse files
authored
fix(toHaveProperty): serialize falsy arguments as well (#14232)
1 parent 79559ae commit fe0afd6

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

packages/playwright-core/src/client/locator.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ export class Locator implements api.Locator {
276276
async _expect(customStackTrace: ParsedStackTrace, expression: string, options: Omit<FrameExpectOptions, 'expectedValue'> & { expectedValue?: any }): Promise<{ matches: boolean, received?: any, log?: string[] }> {
277277
return this._frame._wrapApiCall(async () => {
278278
const params: channels.FrameExpectParams = { selector: this._selector, expression, ...options, isNot: !!options.isNot };
279-
if (options.expectedValue)
280-
params.expectedValue = serializeArgument(options.expectedValue);
279+
params.expectedValue = serializeArgument(options.expectedValue);
281280
const result = (await this._frame._channel.expect(params));
282281
if (result.received !== undefined)
283282
result.received = parseResult(result.received);

tests/playwright-test/playwright.expect.misc.spec.ts

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ test('should support toHaveJSProperty with builtin types', async ({ runInlineTes
133133
await page.setContent('<div></div>');
134134
await page.$eval('div', e => e.foo = 'string');
135135
const locator = page.locator('div');
136-
await expect(locator).toHaveJSProperty('foo', 'error', {timeout: 1000});
136+
await expect(locator).toHaveJSProperty('foo', 'error', {timeout: 200});
137137
});
138138
139139
test('pass number', async ({ page }) => {
@@ -147,7 +147,7 @@ test('should support toHaveJSProperty with builtin types', async ({ runInlineTes
147147
await page.setContent('<div></div>');
148148
await page.$eval('div', e => e.foo = 2021);
149149
const locator = page.locator('div');
150-
await expect(locator).toHaveJSProperty('foo', 1, {timeout: 1000});
150+
await expect(locator).toHaveJSProperty('foo', 1, {timeout: 200});
151151
});
152152
153153
test('pass boolean', async ({ page }) => {
@@ -161,13 +161,40 @@ test('should support toHaveJSProperty with builtin types', async ({ runInlineTes
161161
await page.setContent('<div></div>');
162162
await page.$eval('div', e => e.foo = false);
163163
const locator = page.locator('div');
164-
await expect(locator).toHaveJSProperty('foo', true, {timeout: 1000});
164+
await expect(locator).toHaveJSProperty('foo', true, {timeout: 200});
165+
});
166+
167+
test('pass boolean 2', async ({ page }) => {
168+
await page.setContent('<div></div>');
169+
await page.$eval('div', e => e.foo = false);
170+
const locator = page.locator('div');
171+
await expect(locator).toHaveJSProperty('foo', false);
172+
});
173+
174+
test('fail boolean 2', async ({ page }) => {
175+
await page.setContent('<div></div>');
176+
await page.$eval('div', e => e.foo = false);
177+
const locator = page.locator('div');
178+
await expect(locator).toHaveJSProperty('foo', true, {timeout: 200});
179+
});
180+
181+
test('pass undefined', async ({ page }) => {
182+
await page.setContent('<div></div>');
183+
const locator = page.locator('div');
184+
await expect(locator).toHaveJSProperty('foo', undefined);
185+
});
186+
187+
test('pass null', async ({ page }) => {
188+
await page.setContent('<div></div>');
189+
await page.$eval('div', e => e.foo = null);
190+
const locator = page.locator('div');
191+
await expect(locator).toHaveJSProperty('foo', null);
165192
});
166193
`,
167194
}, { workers: 1 });
168195
const output = stripAnsi(result.output);
169-
expect(result.passed).toBe(3);
170-
expect(result.failed).toBe(3);
196+
expect(result.passed).toBe(6);
197+
expect(result.failed).toBe(4);
171198
expect(result.exitCode).toBe(1);
172199
expect(output).toContain('Expected: "error"');
173200
expect(output).toContain('Received: "string"');

0 commit comments

Comments
 (0)