Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runner): fix fixture parsing of lowered async syntax for non arrow functions #6575

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,11 @@ function resolveDeps(
function getUsedProps(fn: Function) {
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)) {
// example code on esbuild-try https://esbuild.github.io/try/#YgAwLjI0LjAALS1zdXBwb3J0ZWQ6YXN5bmMtYXdhaXQ9ZmFsc2UAZQBlbnRyeS50cwBjb25zdCBvID0gewogIGYxOiBhc3luYyAoKSA9PiB7fSwKICBmMjogYXN5bmMgKGEpID0+IHt9LAogIGYzOiBhc3luYyAoYSwgYikgPT4ge30sCiAgZjQ6IGFzeW5jIGZ1bmN0aW9uKGEpIHt9LAogIGY1OiBhc3luYyBmdW5jdGlvbiBmZihhKSB7fSwKICBhc3luYyBmNihhKSB7fSwKCiAgZzE6IGFzeW5jICgpID0+IHt9LAogIGcyOiBhc3luYyAoeyBhIH0pID0+IHt9LAogIGczOiBhc3luYyAoeyBhIH0sIGIpID0+IHt9LAogIGc0OiBhc3luYyBmdW5jdGlvbiAoeyBhIH0pIHt9LAogIGc1OiBhc3luYyBmdW5jdGlvbiBnZyh7IGEgfSkge30sCiAgYXN5bmMgZzYoeyBhIH0pIHt9Cn0
// __async(this, null, function*
// __async(this, arguments, function*
// __async(this, [_0, _1], function*
if (/__async\(this, (?:null|arguments|\[[_0-9, ]*\]), function\*/.test(fnString)) {
fnString = fnString.split('__async(this,')[1]
}
const match = fnString.match(/[^(]*\(([^)]*)/)
Expand Down
18 changes: 18 additions & 0 deletions test/config/fixtures/fixture-no-async/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { test as base, expect } from "vitest";
type Fixture = {
simple: string,
nested: string,
notArrow1: string,
notArrow2: string,
notArrow3: string,
}

const test = base.extend<Fixture>({
Expand All @@ -12,6 +15,15 @@ const test = base.extend<Fixture>({
nested: async ({ simple }, use) => {
await use("nested:" + simple);
},
async notArrow1({}, use) {
await use("notArrow1");
},
notArrow2: async function({}, use) {
await use("notArrow2");
},
notArrow3: async function notArrow3({}, use) {
await use("notArrow3");
}
});

test("test sync", ({ simple, nested }) => {
Expand All @@ -35,3 +47,9 @@ test.for([1, 2])("test.for async %i", async (i, { expect, simple, nested }) => {
expect(simple).toBe("simple");
expect(nested).toBe("nested:simple")
})

test("test notArrow", async function ({ notArrow1, notArrow2, notArrow3 }) {
expect(notArrow1).toMatchInlineSnapshot(`"notArrow1"`)
expect(notArrow2).toMatchInlineSnapshot(`"notArrow2"`)
expect(notArrow3).toMatchInlineSnapshot(`"notArrow3"`)
});
19 changes: 8 additions & 11 deletions test/config/test/fixture-no-async.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

test('fixture parsing works for lowered async syntax', async () => {
const { stdout } = await runVitest({
const { ctx } = 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
"
expect(ctx?.state.getFiles().map(f => [f.name, f.result?.state])).toMatchInlineSnapshot(`
[
[
"basic.test.ts",
"pass",
],
]
`)
})
Loading