From f2ee832add29d07cda487b37d69cfb990502a6aa Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 19 Sep 2024 14:01:52 +0900 Subject: [PATCH] fix(runner): support fixture parsing for lowered async syntax --- packages/runner/src/fixture.ts | 9 ++++- .../fixtures/fixture-no-async/basic.test.ts | 37 +++++++++++++++++++ .../fixture-no-async/vitest.config.ts | 9 +++++ test/config/test/fixture-no-async.test.ts | 21 +++++++++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 test/config/fixtures/fixture-no-async/basic.test.ts create mode 100644 test/config/fixtures/fixture-no-async/vitest.config.ts create mode 100644 test/config/test/fixture-no-async.test.ts diff --git a/packages/runner/src/fixture.ts b/packages/runner/src/fixture.ts index 11bf8756b726..e67fd26e8694 100644 --- a/packages/runner/src/fixture.ts +++ b/packages/runner/src/fixture.ts @@ -212,7 +212,14 @@ function resolveDeps( } function getUsedProps(fn: Function) { - const match = fn.toString().match(/[^(]*\(([^)]*)/) + let fnString = fn.toString() + // match lowered async function and strip it off + // (_0) => __async(this, [_0], function* (x) { ... }) + // (_0, _1) => __async(this, [_0, _1], function* (x, y) { ... }) + if (/^\([_0-9, ]*\)\s*=>\s*__async\(this,/.test(fnString)) { + fnString = fnString.split('__async(this,')[1] + } + const match = fnString.match(/[^(]*\(([^)]*)/) if (!match) { return [] } diff --git a/test/config/fixtures/fixture-no-async/basic.test.ts b/test/config/fixtures/fixture-no-async/basic.test.ts new file mode 100644 index 000000000000..3448328f0b47 --- /dev/null +++ b/test/config/fixtures/fixture-no-async/basic.test.ts @@ -0,0 +1,37 @@ +import { test as base, expect } from "vitest"; + +type Fixture = { + simple: string, + nested: string, +} + +const test = base.extend({ + simple: async ({}, use) => { + await use("simple"); + }, + nested: async ({ simple }, use) => { + await use("nested:" + simple); + }, +}); + +test("test sync", ({ simple, nested }) => { + expect(simple).toBe("simple"); + expect(nested).toBe("nested:simple") +}); + +test("test async", async ({ simple, nested }) => { + expect(simple).toBe("simple"); + expect(nested).toBe("nested:simple") +}); + +test.for([1, 2])("test.for sync %i", (i, { expect, simple, nested }) => { + expect(i).toBeTypeOf("number") + expect(simple).toBe("simple"); + expect(nested).toBe("nested:simple") +}) + +test.for([1, 2])("test.for async %i", async (i, { expect, simple, nested }) => { + expect(i).toBeTypeOf("number") + expect(simple).toBe("simple"); + expect(nested).toBe("nested:simple") +}) diff --git a/test/config/fixtures/fixture-no-async/vitest.config.ts b/test/config/fixtures/fixture-no-async/vitest.config.ts new file mode 100644 index 000000000000..17c470355f56 --- /dev/null +++ b/test/config/fixtures/fixture-no-async/vitest.config.ts @@ -0,0 +1,9 @@ +import { defineConfig } from "vitest/config"; + +export default defineConfig({ + esbuild: { + supported: { + "async-await": false, + }, + }, +}); diff --git a/test/config/test/fixture-no-async.test.ts b/test/config/test/fixture-no-async.test.ts new file mode 100644 index 000000000000..c1f378cdcf7a --- /dev/null +++ b/test/config/test/fixture-no-async.test.ts @@ -0,0 +1,21 @@ +import path from 'node:path' +import { expect, test } from 'vitest' +import { runVitest } from '../../test-utils' + +test('fixture parsing works for lowered async syntax', async () => { + const { stdout } = await runVitest({ + root: path.resolve('fixtures/fixture-no-async'), + reporters: ['tap-flat'], + }) + expect(stdout.replaceAll(/\s*# time=.*/g, '')).toMatchInlineSnapshot(` + "TAP version 13 + 1..6 + ok 1 - basic.test.ts > test sync + ok 2 - basic.test.ts > test async + ok 3 - basic.test.ts > test.for sync 1 + ok 4 - basic.test.ts > test.for sync 2 + ok 5 - basic.test.ts > test.for async 1 + ok 6 - basic.test.ts > test.for async 2 + " + `) +})