-
Notifications
You must be signed in to change notification settings - Fork 4.9k
feat: Warn on floating promises #34845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d3836e9
Working warning annotation on expect
agg23 a462b68
Generalized implementation and step API
agg23 be4e107
TerminalReporter display of warnings
agg23 0495115
Switched to proxy implementation
agg23 c6763dd
Tests for await warnings
agg23 9f216ec
Merge branch 'main' of https://github.com/microsoft/playwright into f…
agg23 3a31128
Add Promise prototype test
agg23 d84588b
Moved scope logic to separate class
agg23 c1ccd66
Dummy change
agg23 c7150cc
Undo dummy change
agg23 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| export class FloatingPromiseScope { | ||
| readonly _floatingCalls: Set<Promise<any>> = new Set(); | ||
|
|
||
| /** | ||
| * Enables a promise API call to be tracked by the test, alerting if unawaited. | ||
| * | ||
| * **NOTE:** Returning from an async function wraps the result in a promise, regardless of whether the return value is a promise. This will automatically mark the promise as awaited. Avoid this. | ||
| */ | ||
| wrapPromiseAPIResult<T>(promise: Promise<T>): Promise<T> { | ||
| const promiseProxy = new Proxy(promise, { | ||
| get: (target, prop, receiver) => { | ||
| if (prop === 'then') { | ||
| return (...args: any[]) => { | ||
| this._floatingCalls.delete(promise); | ||
|
|
||
| const originalThen = Reflect.get(target, prop, receiver) as Promise<T>['then']; | ||
| return originalThen.call(target, ...args); | ||
| }; | ||
| } else { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| this._floatingCalls.add(promise); | ||
|
|
||
| return promiseProxy; | ||
| } | ||
|
|
||
| hasFloatingPromises(): boolean { | ||
| return this._floatingCalls.size > 0; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| /** | ||
| * Copyright (c) Microsoft Corporation. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| import { test, expect } from './playwright-test-fixtures'; | ||
|
|
||
| const warningSnippet = 'Some async calls were not awaited'; | ||
|
|
||
| test.describe.configure({ mode: 'parallel' }); | ||
|
|
||
| test.describe('await', () => { | ||
| test('should not care about non-API promises', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test } from '@playwright/test'; | ||
| test('test', () => { | ||
| new Promise(() => {}); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).not.toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should warn about missing await on expects when failing', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('custom test name', async ({ page }) => { | ||
| expect(page.locator('div')).toHaveText('A', { timeout: 100 }); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(1); | ||
| expect(stdout).toContain(warningSnippet); | ||
| expect(stdout).toContain('custom test name'); | ||
| }); | ||
|
|
||
| test('should warn about missing await on expects when passing', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| expect(page.locator('div')).toHaveText('A'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should not warn when not missing await on expects when failing', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await expect(page.locator('div')).toHaveText('A', { timeout: 100 }); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(1); | ||
| expect(stdout).not.toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should not warn when not missing await on expects when passing', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| await expect(page.locator('div')).toHaveText('A'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).not.toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should warn about missing await on reject', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| expect(Promise.reject(new Error('foo'))).rejects.toThrow('foo'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should warn about missing await on reject.not', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| expect(Promise.reject(new Error('foo'))).rejects.not.toThrow('foo'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(1); | ||
| expect(stdout).toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should warn about missing await on test.step', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| test.step('step', () => {}); | ||
| await expect(page.locator('div')).toHaveText('A'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should not warn when not missing await on test.step', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| await test.step('step', () => {}); | ||
| await expect(page.locator('div')).toHaveText('A'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).not.toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('should warn about missing await on test.step.skip', async ({ runInlineTest }) => { | ||
| const { exitCode, stdout } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| test.step.skip('step', () => {}); | ||
| await expect(page.locator('div')).toHaveText('A'); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain(warningSnippet); | ||
| }); | ||
|
|
||
| test('traced promise should be instanceof Promise', async ({ runInlineTest }) => { | ||
| const { exitCode } = await runInlineTest({ | ||
| 'a.test.ts': ` | ||
| import { test, expect } from '@playwright/test'; | ||
| test('test', async ({ page }) => { | ||
| await page.setContent('data:text/html,<div>A</div>'); | ||
| const expectPromise = expect(page.locator('div')).toHaveText('A'); | ||
| expect(expectPromise instanceof Promise).toBeTruthy(); | ||
| }); | ||
| ` | ||
| }); | ||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.