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

Different Typescript treatment between vitest and vitest-browser #7395

Closed
6 tasks done
just-boris opened this issue Jan 31, 2025 · 4 comments
Closed
6 tasks done

Different Typescript treatment between vitest and vitest-browser #7395

just-boris opened this issue Jan 31, 2025 · 4 comments

Comments

@just-boris
Copy link

Describe the bug

This file runs successfully in standard vitest, but fails in vitest browser mode:

import { Something } from "./types";

export { Something };

export function getSomething(): Something {
  return {
    foo: 1,
    bar: "a",
  };
}

When running in browser mode, it prints this

 RUN  v3.0.4 /Users/serdiuk/coding/vitest-browser-typescript

 ❯  chromium  test/index.test.ts (0 test)

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL   chromium  test/index.test.ts [ test/index.test.ts ]
SyntaxError: The requested module '/src/types.ts' does not provide an export named 'Something'
⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed (1)
      Tests  no tests
   Start at  23:28:11
   Duration  786ms (transform 0ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 200ms)

Reproduction

Published a mini project here: https://github.com/just-boris/vitest-browser-typescript (not sure if browser mode can be used on StackBlitz)

System Info

System:
    OS: macOS 15.2
    CPU: (10) arm64 Apple M1 Pro
    Memory: 164.55 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.18.2 - ~/n/bin/node
    npm: 10.8.2 - ~/n/bin/npm
    pnpm: 8.15.4 - ~/n/bin/pnpm
  Browsers:
    Chrome: 132.0.6834.160
    Safari: 18.2
  npmPackages:
    @vitest/browser: ^3.0.4 => 3.0.4
    playwright: ^1.50.1 => 1.50.1
    vitest: ^3.0.4 => 3.0.4

Used Package Manager

npm

Validations

@hi-ogawa
Copy link
Contributor

hi-ogawa commented Feb 1, 2025

The issue is technically on non-browser side and ideally both should fail. For modern bundler tooling, it's recommended to mark type import/export, so they can be elided by bundler. https://stackblitz.com/edit/github-2cf9mdip?file=src%2Findex.ts

import type { Something } from "./types";
export type { Something };

This can be ensured by ts option such as isolatedModules and verbatimModuleSyntax (see also vite doc https://vite.dev/guide/features.html#typescript-compiler-options).

@hi-ogawa hi-ogawa closed this as completed Feb 1, 2025
@just-boris
Copy link
Author

Yes, I was aware of verbatimModuleSyntax, but the point of the issue is to show inconsistency between browser and non-browser mode.

Why does it work fine in non-browser mode, or it works in vite server by default, but only fails in vite browser mode?

@sheremet-va
Copy link
Member

Yes, I was aware of verbatimModuleSyntax, but the point of the issue is to show inconsistency between browser and non-browser mode.

Why does it work fine in non-browser mode, or it works in vite server by default, but only fails in vite browser mode?

It works in non-browser mode because Vitests runs tests in a relaxed sandbox environment where ESM rules are less strict then they should be. There will be a switch to run code in an actual environment in the future where your code will fail the same way: #7234

@just-boris
Copy link
Author

Thanks for the extra info, it helps. Adopting verbatimModuleSyntax that is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants