Skip to content

Commit

Permalink
fix(runner): support fixture parsing of lowered async syntax (#6531)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Sep 25, 2024
1 parent fb79792 commit b553c7d
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
9 changes: 8 additions & 1 deletion packages/runner/src/fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
}
Expand Down
37 changes: 37 additions & 0 deletions test/config/fixtures/fixture-no-async/basic.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})
9 changes: 9 additions & 0 deletions test/config/fixtures/fixture-no-async/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from "vitest/config";

export default defineConfig({
esbuild: {
supported: {
"async-await": false,
},
},
});
21 changes: 21 additions & 0 deletions test/config/test/fixture-no-async.test.ts
Original file line number Diff line number Diff line change
@@ -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
"
`)
})

0 comments on commit b553c7d

Please sign in to comment.