Skip to content

Commit 3de00ab

Browse files
authoredOct 1, 2024··
fix(runner): fix fixture parsing of lowered async syntax for non arrow functions (#6575)
1 parent e7f3521 commit 3de00ab

File tree

3 files changed

+31
-14
lines changed

3 files changed

+31
-14
lines changed
 

‎packages/runner/src/fixture.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ function resolveDeps(
214214
function getUsedProps(fn: Function) {
215215
let fnString = fn.toString()
216216
// 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)) {
217+
// example code on esbuild-try https://esbuild.github.io/try/#YgAwLjI0LjAALS1zdXBwb3J0ZWQ6YXN5bmMtYXdhaXQ9ZmFsc2UAZQBlbnRyeS50cwBjb25zdCBvID0gewogIGYxOiBhc3luYyAoKSA9PiB7fSwKICBmMjogYXN5bmMgKGEpID0+IHt9LAogIGYzOiBhc3luYyAoYSwgYikgPT4ge30sCiAgZjQ6IGFzeW5jIGZ1bmN0aW9uKGEpIHt9LAogIGY1OiBhc3luYyBmdW5jdGlvbiBmZihhKSB7fSwKICBhc3luYyBmNihhKSB7fSwKCiAgZzE6IGFzeW5jICgpID0+IHt9LAogIGcyOiBhc3luYyAoeyBhIH0pID0+IHt9LAogIGczOiBhc3luYyAoeyBhIH0sIGIpID0+IHt9LAogIGc0OiBhc3luYyBmdW5jdGlvbiAoeyBhIH0pIHt9LAogIGc1OiBhc3luYyBmdW5jdGlvbiBnZyh7IGEgfSkge30sCiAgYXN5bmMgZzYoeyBhIH0pIHt9Cn0
218+
// __async(this, null, function*
219+
// __async(this, arguments, function*
220+
// __async(this, [_0, _1], function*
221+
if (/__async\(this, (?:null|arguments|\[[_0-9, ]*\]), function\*/.test(fnString)) {
220222
fnString = fnString.split('__async(this,')[1]
221223
}
222224
const match = fnString.match(/[^(]*\(([^)]*)/)

‎test/config/fixtures/fixture-no-async/basic.test.ts

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import { test as base, expect } from "vitest";
33
type Fixture = {
44
simple: string,
55
nested: string,
6+
notArrow1: string,
7+
notArrow2: string,
8+
notArrow3: string,
69
}
710

811
const test = base.extend<Fixture>({
@@ -12,6 +15,15 @@ const test = base.extend<Fixture>({
1215
nested: async ({ simple }, use) => {
1316
await use("nested:" + simple);
1417
},
18+
async notArrow1({}, use) {
19+
await use("notArrow1");
20+
},
21+
notArrow2: async function({}, use) {
22+
await use("notArrow2");
23+
},
24+
notArrow3: async function notArrow3({}, use) {
25+
await use("notArrow3");
26+
}
1527
});
1628

1729
test("test sync", ({ simple, nested }) => {
@@ -35,3 +47,9 @@ test.for([1, 2])("test.for async %i", async (i, { expect, simple, nested }) => {
3547
expect(simple).toBe("simple");
3648
expect(nested).toBe("nested:simple")
3749
})
50+
51+
test("test notArrow", async function ({ notArrow1, notArrow2, notArrow3 }) {
52+
expect(notArrow1).toMatchInlineSnapshot(`"notArrow1"`)
53+
expect(notArrow2).toMatchInlineSnapshot(`"notArrow2"`)
54+
expect(notArrow3).toMatchInlineSnapshot(`"notArrow3"`)
55+
});

‎test/config/test/fixture-no-async.test.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@ import { expect, test } from 'vitest'
33
import { runVitest } from '../../test-utils'
44

55
test('fixture parsing works for lowered async syntax', async () => {
6-
const { stdout } = await runVitest({
6+
const { ctx } = await runVitest({
77
root: path.resolve('fixtures/fixture-no-async'),
88
reporters: ['tap-flat'],
99
})
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-
"
10+
expect(ctx?.state.getFiles().map(f => [f.name, f.result?.state])).toMatchInlineSnapshot(`
11+
[
12+
[
13+
"basic.test.ts",
14+
"pass",
15+
],
16+
]
2017
`)
2118
})

0 commit comments

Comments
 (0)
Please sign in to comment.