Skip to content

require-ing a json file breaks vitest #9822

Open
@threepointone

Description

@threepointone

What versions & operating system are you using?

[email protected], @cloudflare/vitest-pool-workers

Please provide a link to a minimal reproduction

https://github.com/threepointone/test-vitest-workers-json-require

Describe the Bug

A require of a JSON file (NOT an import) will fail to run with @cloudflare/vitest-pool-workers, with a cryptic error message SyntaxError: Unexpected token ':'.

Why is this important? Sure, developers might use import to import JSON files, but third party dependencies (like the very popular ajv) do cjs builds, which end up requireing JSON files.

(like here: https://unpkg.com/[email protected]/dist/core.js)

This works when we do wrangler dev or vite dev, but fails when we run vitest.

Repro

npm install && npm test

Either we should make it work with vitest/vitest-pool-workers, or we should have a better error.

Workaround:

add it to the deps section of vitest.config.ts

Please provide any relevant error logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething that isn't workingvitestRelating to the Workers Vitest integration

    Type

    Projects

    Status

    Untriaged

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions