From d4415fe1122eaa30241a55ec37c0efcad2e1fec9 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Mon, 14 Feb 2022 14:48:48 +0800 Subject: [PATCH] fix: `describe` async support (#750) * Revert "fix: remove `describe` async support (#746)" This reverts commit f9b52c1be16329a0ca5ae5d67a4b3141b4389714. * fix: collect nested suite serially --- examples/lit/test/basic.test.ts | 2 +- examples/puppeteer/test/basic.test.ts | 2 +- .../react-testing-lib/src/components/input.test.tsx | 2 +- packages/vitest/src/runtime/suite.ts | 10 +++++----- packages/vitest/src/types/tasks.ts | 2 +- test/fails/test/runner.test.ts | 4 ++-- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/examples/lit/test/basic.test.ts b/examples/lit/test/basic.test.ts index 8d7fd1a97ac6..4199e2fba677 100644 --- a/examples/lit/test/basic.test.ts +++ b/examples/lit/test/basic.test.ts @@ -7,7 +7,7 @@ declare global { interface Window extends IWindow {} } -describe('Button with increment', () => { +describe('Button with increment', async() => { beforeEach(async() => { document.body.innerHTML = '' await window.happyDOM.whenAsyncComplete() diff --git a/examples/puppeteer/test/basic.test.ts b/examples/puppeteer/test/basic.test.ts index 4a776703b1a2..fd6abbcc98ef 100644 --- a/examples/puppeteer/test/basic.test.ts +++ b/examples/puppeteer/test/basic.test.ts @@ -4,7 +4,7 @@ import type { PreviewServer } from 'vite' import puppeteer from 'puppeteer' import type { Browser, Page } from 'puppeteer' -describe('basic', () => { +describe('basic', async() => { let server: PreviewServer let browser: Browser let page: Page diff --git a/examples/react-testing-lib/src/components/input.test.tsx b/examples/react-testing-lib/src/components/input.test.tsx index 51179878cf96..9ed1c72a78b7 100644 --- a/examples/react-testing-lib/src/components/input.test.tsx +++ b/examples/react-testing-lib/src/components/input.test.tsx @@ -2,7 +2,7 @@ import '@testing-library/jest-dom' import { render, screen, userEvent } from '../utils/test-utils' import { Input } from './Input' -describe('Input', () => { +describe('Input', async() => { it('should render the input', () => { render( { }, m if (factory) await runWithSuite(collector, () => factory(test)) - const allChildren = await Promise.all( - [...factoryQueue, ...tasks] - .map(i => i.type === 'collector' ? i.collect(file) : i), - ) + const allChildren: Task[] = [] + + for (const i of [...factoryQueue, ...tasks]) + allChildren.push(i.type === 'collector' ? await i.collect(file) : i) suite.file = file suite.tasks = allChildren diff --git a/packages/vitest/src/types/tasks.ts b/packages/vitest/src/types/tasks.ts index d2c79c395e85..2542934986d6 100644 --- a/packages/vitest/src/types/tasks.ts +++ b/packages/vitest/src/types/tasks.ts @@ -78,7 +78,7 @@ export interface SuiteCollector { on: (name: T, ...fn: SuiteHooks[T]) => void } -export type SuiteFactory = (test: (name: string, fn: TestFunction) => void) => void +export type SuiteFactory = (test: (name: string, fn: TestFunction) => void) => Awaitable export interface RuntimeContext { tasks: (SuiteCollector | Test)[] diff --git a/test/fails/test/runner.test.ts b/test/fails/test/runner.test.ts index 1a66b5526596..04a17e07f8cb 100644 --- a/test/fails/test/runner.test.ts +++ b/test/fails/test/runner.test.ts @@ -3,9 +3,9 @@ import fg from 'fast-glob' import { execa } from 'execa' import { describe, expect, it } from 'vitest' -describe('should fails', () => { +describe('should fails', async() => { const root = resolve(__dirname, '../fixtures') - const files = fg.sync('*.test.ts', { cwd: root }) + const files = await fg('*.test.ts', { cwd: root }) for (const file of files) { it(file, async() => {