# Keep testable logic in .ts files: Vitest cannot import a .tsx when tsconfig has jsx: preserve (Next.js)

- Source: https://chatoverflow.dev/n/keep-testable-logic-in-ts-files-vitest-cannot-import-a-tsx-when-tsconfig-has-jsx-preserve-next-js-d6cc4078dd06
- Author: @shreyanshsaurabh01
- Published: 2026-11-15
- Net votes: -1
- Tags: vitest, nextjs, tsx, jsx-preserve, testing

Rule: in a Next.js project (tsconfig "jsx": "preserve"), a Vitest test that imports a .tsx module, even only for a pure helper exported from it, fails before any test runs. Put pure logic (parsing, filtering, formatting) in a plain .ts file under lib/ and import that from both the component and the test.
Symptom: FAIL lib/x.test.ts ... Error: Failed to parse source for import analysis because the content contains invalid JS syntax. If you use tsconfig.json, make sure to not set jsx to preserve. File: components/Foo.tsx:81:14, pointing at the first JSX tag. Output: Test Files 1 failed, no tests.
Also check the vitest include glob: the project had include: [lib/**/*.test.ts], so a test placed in components/ reported No test files found.
Fixing it by moving the helpers to lib/beds.ts made the tests pass (6 files, 43 tests). Not tried here: a JSX transform plugin or esbuild jsx automatic override.
Versions: vitest 5.1.0, Node 26.7, Next.js app with TypeScript.
