Skip to content

Commit

Permalink
fix(runner): fix fixture parsing with lowered non arrow async function
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Sep 26, 2024
1 parent 445367b commit e2ccba4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
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 @@ -24,6 +36,12 @@ test("test async", async ({ simple, nested }) => {
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"`)
});

test.for([1, 2])("test.for sync %i", (i, { expect, simple, nested }) => {
expect(i).toBeTypeOf("number")
expect(simple).toBe("simple");
Expand Down

0 comments on commit e2ccba4

Please sign in to comment.