Skip to content

Commit b553c7d

Browse files
authoredSep 25, 2024··
fix(runner): support fixture parsing of lowered async syntax (#6531)
1 parent fb79792 commit b553c7d

File tree

4 files changed

+75
-1
lines changed

4 files changed

+75
-1
lines changed
 

‎packages/runner/src/fixture.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,14 @@ function resolveDeps(
212212
}
213213

214214
function getUsedProps(fn: Function) {
215-
const match = fn.toString().match(/[^(]*\(([^)]*)/)
215+
let fnString = fn.toString()
216+
// match lowered async function and strip it off
217+
// (_0) => __async(this, [_0], function* (x) { ... })
218+
// (_0, _1) => __async(this, [_0, _1], function* (x, y) { ... })
219+
if (/^\([_0-9, ]*\)\s*=>\s*__async\(this,/.test(fnString)) {
220+
fnString = fnString.split('__async(this,')[1]
221+
}
222+
const match = fnString.match(/[^(]*\(([^)]*)/)
216223
if (!match) {
217224
return []
218225
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { test as base, expect } from "vitest";
2+
3+
type Fixture = {
4+
simple: string,
5+
nested: string,
6+
}
7+
8+
const test = base.extend<Fixture>({
9+
simple: async ({}, use) => {
10+
await use("simple");
11+
},
12+
nested: async ({ simple }, use) => {
13+
await use("nested:" + simple);
14+
},
15+
});
16+
17+
test("test sync", ({ simple, nested }) => {
18+
expect(simple).toBe("simple");
19+
expect(nested).toBe("nested:simple")
20+
});
21+
22+
test("test async", async ({ simple, nested }) => {
23+
expect(simple).toBe("simple");
24+
expect(nested).toBe("nested:simple")
25+
});
26+
27+
test.for([1, 2])("test.for sync %i", (i, { expect, simple, nested }) => {
28+
expect(i).toBeTypeOf("number")
29+
expect(simple).toBe("simple");
30+
expect(nested).toBe("nested:simple")
31+
})
32+
33+
test.for([1, 2])("test.for async %i", async (i, { expect, simple, nested }) => {
34+
expect(i).toBeTypeOf("number")
35+
expect(simple).toBe("simple");
36+
expect(nested).toBe("nested:simple")
37+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
esbuild: {
5+
supported: {
6+
"async-await": false,
7+
},
8+
},
9+
});
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import path from 'node:path'
2+
import { expect, test } from 'vitest'
3+
import { runVitest } from '../../test-utils'
4+
5+
test('fixture parsing works for lowered async syntax', async () => {
6+
const { stdout } = await runVitest({
7+
root: path.resolve('fixtures/fixture-no-async'),
8+
reporters: ['tap-flat'],
9+
})
10+
expect(stdout.replaceAll(/\s*# time=.*/g, '')).toMatchInlineSnapshot(`
11+
"TAP version 13
12+
1..6
13+
ok 1 - basic.test.ts > test sync
14+
ok 2 - basic.test.ts > test async
15+
ok 3 - basic.test.ts > test.for sync 1
16+
ok 4 - basic.test.ts > test.for sync 2
17+
ok 5 - basic.test.ts > test.for async 1
18+
ok 6 - basic.test.ts > test.for async 2
19+
"
20+
`)
21+
})

0 commit comments

Comments
 (0)
Please sign in to comment.