fix(runner): support fixture parsing of lowered async syntax (#6531)

This commit is contained in:
Hiroshi Ogawa 2024-09-25 17:32:27 +09:00 committed by GitHub
parent fb79792dc9
commit b553c7d6d5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 75 additions and 1 deletions

View File

@ -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 []
}

View File

@ -0,0 +1,37 @@
import { test as base, expect } from "vitest";
type Fixture = {
simple: string,
nested: string,
}
const test = base.extend<Fixture>({
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")
})

View File

@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";
export default defineConfig({
esbuild: {
supported: {
"async-await": false,
},
},
});

View File

@ -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
"
`)
})