Skip to content

SyntaxError for await vi.hoisted(async () => {}) without variable declaration #4913

@hi-ogawa

Description

@hi-ogawa

Describe the bug

I wasn't sure if this is an intended usage of vi.hoisted, but I suppose SyntaxError is not meant to surface to users regardless.

For this test file input:

import { test, vi } from 'vitest';

test('ok', () => {});

vi.mock('./dummy.ts');

await vi.hoisted(async () => {});

VITE_NODE_DEBUG_DUMP=1 vitest shows this transformed output:

const { test, vi } = await __vite_ssr_dynamic_import__('vitest')
vi.mock("./dummy.ts");
vi.hoisted(async () => {
});

test("ok", () => {
});
await

with the test error:

 FAIL  test/repro.test.ts [ test/repro.test.ts ]
SyntaxError: Unexpected token '}'
 ❯ __global_eval__ ../../../blitz.f7aa08df.js:1:263
 ❯ _0x5dcd1e ../../../blitz.f7aa08df.js:352:200671
 ❯ Script.runInContext ../../../blitz.f7aa08df.js:352:199875
 ❯ Script.runInThisContext node:vm:74:1318
 ❯ Object.runInThisContext node:vm:74:3926

Reproduction

https://stackblitz.com/edit/vitest-dev-vitest-gms3di?file=test%2Frepro.test.ts

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.14.0 - /usr/local/bin/pnpm
  npmPackages:
    @vitest/ui: latest => 1.1.3 
    vite: latest => 5.0.11 
    vitest: latest => 1.1.3

Used Package Manager

npm

Validations

Metadata

Metadata

Assignees

No one assigned

    Labels

    p3-minor-bugAn edge case that only affects very specific usage (priority)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions