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

Vitest fails when importing and using a typescript file in a test #6091

Closed
6 tasks done
Wizzel1 opened this issue Jul 11, 2024 · 1 comment
Closed
6 tasks done

Vitest fails when importing and using a typescript file in a test #6091

Wizzel1 opened this issue Jul 11, 2024 · 1 comment

Comments

@Wizzel1
Copy link

Wizzel1 commented Jul 11, 2024

Describe the bug

So I have a turbo monorepo with a package called "types" where I export all types and constants from an index.ts file.

This is its package.json:

{
  "name": "@podsuite/types",
  "version": "0.0.1",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "test": "vitest",
    "test:watch": "vitest watch",
    "test:coverage": "vitest run --coverage",
    "format": "biome format . --write"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@podsuite/components": "workspace:*",
    "svelte": "^4.2.18"
  },
  "devDependencies": {
    "vitest": "^2.0.2"
  }
}

In this package I have installed vitest and I want to test this file:

src/marketplaces.ts

import { z } from 'zod';
export const marketplaces = z.enum([
  'unitedStates',
  'unitedKingdom'
]);

but just a basic test like this fails:

import { expect, test } from 'vitest';
import { marketplaces } from '../index';

test('should work', () => {
  expect(marketplaces).toBeDefined();
});

with the error:

FAIL test/marketplaces.test.ts [ test/marketplaces.test.ts ]
Error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you are using JSX, make sure to name the file with the .jsx or .tsx extension.
❯ TransformPluginContext.formatError ../../node_modules/.pnpm/[email protected]@types[email protected]/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:49746:41
❯ TransformPluginContext.error ../../node_modules/.pnpm/vite@5.3.3_@types[email protected]/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:49741:16
❯ TransformPluginContext.transform ../../node_modules/.pnpm/vite@5.3.3_@types[email protected]/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:64283:14
❯ PluginContainer.transform ../../node_modules/.pnpm/vite@5.3.3_@types[email protected]/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:49587:18
❯ loadAndTransform ../../node_modules/.pnpm/vite@5.3.3_@types[email protected]/node_modules/vite/dist/node/chunks/dep-CzJTQ5q7.js:52409:27

This is my vitest.config.ts:

import { defineConfig } from 'vitest/config';

export default defineConfig({
  test: {
    include: ['src/**/*.{test,spec}.{js,ts}'],
  },
});

At this point I have basically copy pasted this example (without the tsconfig):
https://stackblitz.com/edit/vitest-dev-vitest-racxw9?file=tsconfig.json&initialPath=__vitest__/

Reproduction

N/A

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (16) x64 AMD Ryzen 7 5800X 8-Core Processor
    Memory: 13.18 GB / 31.93 GB
  Binaries:
    Node: 20.15.0 - C:\Program Files\nodejs\node.EXE
    npm: 10.8.1 - C:\Program Files\nodejs\npm.CMD
    pnpm: 9.4.0 - ~\AppData\Roaming\npm\pnpm.CMD
  Browsers:
    Edge: Chromium (126.0.2592.87)
    Internet Explorer: 11.0.19041.4355
  npmPackages:
    vitest: ^2.0.2 => 2.0.2

Used Package Manager

pnpm

Validations

Copy link

Hello @Wizzel1. Please provide a minimal reproduction using a GitHub repository or StackBlitz (you can also use examples). Issues marked with needs reproduction will be closed if they have no activity within 3 days.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 30, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants