Skip to content

Commit 9638db0

Browse files
authored
fix: incorrect error message for non-awaited expect.element() (#8954)
1 parent e8b459d commit 9638db0

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

packages/browser/src/client/tester/expect-element.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ function element<T extends HTMLElement | SVGElement | null | Locator>(elementOrL
1212
throw new Error(`Invalid element or locator: ${elementOrLocator}. Expected an instance of HTMLElement, SVGElement or Locator, received ${getType(elementOrLocator)}`)
1313
}
1414

15-
return expect.poll<HTMLElement | SVGElement | null>(function element(this: object) {
15+
const expectElement = expect.poll<HTMLElement | SVGElement | null>(function element(this: object) {
1616
if (elementOrLocator instanceof Element || elementOrLocator == null) {
1717
return elementOrLocator
1818
}
19-
chai.util.flag(this, '_poll.element', true)
2019

2120
const isNot = chai.util.flag(this, 'negate') as boolean
2221
const name = chai.util.flag(this, '_name') as string
@@ -47,6 +46,10 @@ function element<T extends HTMLElement | SVGElement | null | Locator>(elementOrL
4746

4847
return result
4948
}, processTimeoutOptions(options))
49+
50+
chai.util.flag(expectElement, '_poll.element', true)
51+
52+
return expectElement
5053
}
5154

5255
expect.extend(matchers)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`should fail for non-awaited expect.element 1`] = `
4+
"
5+
⎯⎯⎯⎯⎯⎯⎯ Failed Tests 1 ⎯⎯⎯⎯⎯⎯⎯
6+
7+
FAIL |browser (chromium)| expect-element.test.js > not awaited
8+
Error: expect.element(locator).toBeInTheDocument() was not awaited. This assertion is asynchronous and must be awaited; otherwise, it is not executed to avoid unhandled rejections:
9+
10+
await expect.element(locator).toBeInTheDocument()
11+
12+
❯ expect-element.test.js:18:33
13+
16| const element = page.getByText("Hello Vitest!");
14+
17|
15+
18| expect.element(element).toBeInTheDocument();
16+
| ^
17+
19| })
18+
20|
19+
20+
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯
21+
22+
"
23+
`;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { playwright } from '@vitest/browser-playwright'
2+
import { expect, test } from 'vitest'
3+
import { runInlineTests, ts } from '../../test-utils'
4+
5+
test('should fail for non-awaited expect.element', async () => {
6+
const { stderr } = await runInlineTests({
7+
'expect-element.test.js': ts`
8+
import { expect, test, beforeAll } from 'vitest';
9+
import { page } from 'vitest/browser';
10+
11+
beforeAll(() => {
12+
document.body.innerHTML = 'Hello Vitest!';
13+
});
14+
15+
test('awaited', async () => {
16+
const element = page.getByText("Hello Vitest!");
17+
18+
await expect.element(element).toBeInTheDocument();
19+
})
20+
21+
test('not awaited', () => {
22+
const element = page.getByText("Hello Vitest!");
23+
24+
expect.element(element).toBeInTheDocument();
25+
})
26+
`,
27+
}, {
28+
projects: [
29+
{
30+
test: {
31+
name: 'browser',
32+
browser: {
33+
enabled: true,
34+
headless: true,
35+
provider: playwright(),
36+
instances: [
37+
{ browser: 'chromium' },
38+
],
39+
},
40+
},
41+
},
42+
],
43+
})
44+
45+
expect(stderr).toMatchSnapshot()
46+
})

0 commit comments

Comments
 (0)