我想在Jest 26.1.0中使用ES6模块的导入/导出功能(因为我的整个项目都使用它)。
我已经为我的测试用例创建了一个目录,名为testCases/
,其中包含一个math.mjs
文件。 现在我正在尝试在math.test.js
(用于Jest)中导入此文件。 每当我运行npm run test
时,就会抛出以下错误。
>Details:
/home/jatin/Downloads/Node.js/task-manager-app/TestCases/math.test.js:1
import calc from "../src/math.mjs";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime._execModule (node_modules/jest-runtime/build/index.js:1179:56)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.556 s
Ran all test suites.
npm ERR! Test failed. See above for more details.
我甚至尝试根据文档和其他 GitHub 问题更改 Jest 配置,但迄今为止没有成功。
以下是我的 math.mjs 测试文件
const calc = (total, tippercent) => {
const tip = total * tippercent;
return total + tip;
}
export default calc
这是我的math.test.js文件
import calc from "../src/math.mjs";
test("to calculate tipercent", () => {});
我们如何配置Jest来解析 .mjs 模块?