Vitest cannot be imported in a CommonJS module using require()
Asked Answered
E

1

6

I am running vitest runner. Then I see that someone probably vitest is generating from my .ts files some .js files:

enter image description here

The error message I get seems to be originating from the uiElementsCreator.test.js:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var vitest_1 = require("vitest");
/*import { CompositeChangeResponse, ChangeResponseTreeItem, ConfigurationResult } from "../src/App/apis/models/compositeChangeResponse";
import { GlobalStore } from '../src/App/components/ConfigurationStepper/_shared/helper/globalStore';*/
(0, vitest_1.describe)("Test UiElementsCreator", function () {
    (0, vitest_1.describe)("test request composite on value change", function () {
        (0, vitest_1.test)("Given element is mandatory and active with specific id, expect them 

... rest of test removed for clarity

Error:

 FAIL  test/lib/test/uiElementsCreator.test.js [ test/lib/test/uiElementsCreator.test.js ]
Error: Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.

If you are using "import" in your source code, then it's possible it was bundled into require() automatically by your bundler. In that case, do not bundle CommonJS output since it will never work with Vitest, or use dynamic impo
rt() which is available in all CommonJS modules.

vitest seems to be using require() although it does complain about it!

   var vitest_1 = require("vitest");

What do I wrong here?

Entropy answered 21/2 at 16:44 Comment(0)
R
0

You can either choose to change the .ts file extension to .tsx or in your tsconfig you could exclude test files from your build with:

"exclude": ["**/*.test.*"],
Rhodesia answered 1/3 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.