![](/img/trans.png)
[英]"SyntaxError: Cannot use import statement outside a module" when running a Jest test with Vue Js
[英]Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test
我的代码
App.test.js
import moment from "moment-timezone";
let result = moment().format();
describe("anything",()=>{
it("should return a fail..or at least something",()=>{
expect(result).toBe("wrong")
})
})
我跑:
npx jest或yarn 测试或npm 测试
我得到:
npx jest
FAIL src/App.test.js
● Test suite failed to run
/home/wktdev/Desktop/thing/workflow_magic_guest_app/src/App.test.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import moment from "moment-timezone";
^^^^^^
SyntaxError: Cannot use import statement outside a module
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 0.586s
Ran all test suites.
看起来和这个类似: https : //github.com/facebook/jest/issues/9292
当我执行 npm ls jest 时,它说缺少依赖项。 当我尝试手动安装它们时,它们不会安装并且问题未解决。
UNMET DEPENDENCY react-scripts@2.1.5
└── UNMET DEPENDENCY jest@23.6.0
我根据开玩笑输出的消息弄清楚了。 jest 给出了删除 package.lock 文件、yarn.lock 并通过 yarn install 重新安装的说明。 我之前尝试过,我认为它不起作用,只是去除了笑话,但我错了。 它似乎有效 - 现在。
如果您手动添加 jest,请从package.json
删除 jest 依赖项并运行npm install
或yarn install
。
或者您可以运行npm test
或yarn test
来检查指令。
要解决这个问题,您需要创建文件: ./__tests__/setup.js
并将其放入:
import { configure } from "enzyme";
import Adapter from "enzyme-adapter-react-16";
configure({ adapter: new Adapter() });
之后你只需要运行: npm run test
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.