Skip to content
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

fix(runner): async assertion auto await should timeout #6391

3 changes: 2 additions & 1 deletion packages/runner/src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ export function withTimeout<T extends (...args: any[]) => any>(

const { setTimeout, clearTimeout } = getSafeTimers()

return ((...args: T extends (...args: infer A) => any ? A : never) => {
// this function name is used to filter error in test/cli/test/fails.test.ts
return (function runWithTimeout(...args: T extends (...args: infer A) => any ? A : never) {
Comment on lines +44 to +45
Copy link
Contributor Author

@hi-ogawa hi-ogawa Aug 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but I needed to add this filter for fails.test.ts snapshot since test/cli/fixtures/fails/unhandled.test.ts's stacktrace moved a little and now it points to here

stderr | unhandled.test.ts > unhandled exception
Error: Uncaught [Error: some error]
    ...
    at file:///home/hiroshi/code/others/vitest/packages/runner/dist/index.js:146:14
    at file:///home/hiroshi/code/others/vitest/packages/runner/dist/index.js:529:11
    at runWithTimeout (file:///home/hiroshi/code/others/vitest/packages/runner/dist/index.js:61:7) Error: some error
    at /home/hiroshi/code/others/vitest/test/cli/fixtures/fails/unhandled.test.ts:8:11
    ...

return Promise.race([
fn(...args),
new Promise((resolve, reject) => {
Expand Down
10 changes: 0 additions & 10 deletions packages/runner/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,16 +223,6 @@ export async function runTest(test: Test | Custom, runner: VitestRunner): Promis
}
await fn()
}
// some async expect will be added to this array, in case user forget to await theme
if (test.promises) {
const result = await Promise.allSettled(test.promises)
const errors = result
.map(r => (r.status === 'rejected' ? r.reason : undefined))
.filter(Boolean)
if (errors.length) {
throw errors
}
Comment on lines -229 to -235
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously it was waiting for Promise.allSettled, but I think we can simply use Promise.all since just throwing on first rejected promise would match with how test works.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, failTask will fail with every error, not just the first one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I copied Promise.allSettled.

}

await runner.onAfterTryTask?.(test, {
retry: retryCount,
Expand Down
13 changes: 12 additions & 1 deletion packages/runner/src/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import type {
SuiteHooks,
Task,
TaskCustomOptions,
TaskPopulated,
Test,
TestAPI,
TestFunction,
Expand Down Expand Up @@ -346,7 +347,7 @@ function createSuiteCollector(
setFn(
task,
withTimeout(
withFixtures(handler, context),
withAwaitAsyncAssetions(withFixtures(handler, context), task),
options?.timeout ?? runner.config.testTimeout,
),
)
Expand Down Expand Up @@ -481,6 +482,16 @@ function createSuiteCollector(
return collector
}

function withAwaitAsyncAssetions<T extends (...args: any[]) => any>(fn: T, task: TaskPopulated): T {
return (async (...args: any[]) => {
await fn(...args)
// some async expect will be added to this array, in case user forget to await them
if (task.promises) {
await Promise.all(task.promises)
}
}) as T
}

function createSuite() {
function suiteFn(
this: Record<string, boolean | undefined>,
Expand Down
29 changes: 29 additions & 0 deletions packages/vitest/LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,35 @@ Repository: sindresorhus/p-locate

---------------------------------------

## package-manager-detector
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This came out when pnpm build, so I committed here.

License: MIT
By: Anthony Fu
Repository: git+https://github.com/antfu-collective/package-manager-detector.git

> MIT License
>
> Copyright (c) 2020-PRESENT Anthony Fu <https://github.com/antfu>
>
> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in all
> copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
> SOFTWARE.

---------------------------------------

## picomatch
License: MIT
By: Jon Schlinkert
Expand Down
6 changes: 5 additions & 1 deletion test/cli/fixtures/fails/test-timeout.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { suite, test } from 'vitest'
import { expect, suite, test } from 'vitest'

test('hi', async () => {
await new Promise(resolve => setTimeout(resolve, 1000))
Expand All @@ -17,3 +17,7 @@ suite('suite timeout simple input', () => {
await new Promise(resolve => setTimeout(resolve, 500))
})
}, 200)

test('auto await async assertion', { timeout: 20 }, () => {
expect(new Promise(() => {})).resolves.toBe(0)
})
3 changes: 2 additions & 1 deletion test/cli/test/__snapshots__/fails.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ exports[`should fail test-extend/test-rest-props.test.ts > test-extend/test-rest
exports[`should fail test-extend/test-without-destructuring.test.ts > test-extend/test-without-destructuring.test.ts 1`] = `"Error: The first argument inside a fixture must use object destructuring pattern, e.g. ({ test } => {}). Instead, received "context"."`;

exports[`should fail test-timeout.test.ts > test-timeout.test.ts 1`] = `
"Error: Test timed out in 200ms.
"Error: Test timed out in 20ms.
Error: Test timed out in 200ms.
Error: Test timed out in 100ms.
Error: Test timed out in 10ms."
`;
Expand Down
2 changes: 1 addition & 1 deletion test/cli/test/fails.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ it.each(files)('should fail %s', async (file) => {
const msg = String(stderr)
.split(/\n/g)
.reverse()
.filter(i => i.includes('Error: ') && !i.includes('Command failed') && !i.includes('stackStr') && !i.includes('at runTest'))
.filter(i => i.includes('Error: ') && !i.includes('Command failed') && !i.includes('stackStr') && !i.includes('at runTest') && !i.includes('at runWithTimeout'))
.map(i => i.trim().replace(root, '<rootDir>'),
).join('\n')
expect(msg).toMatchSnapshot(file)
Expand Down