【发布时间】:2020-06-11 20:02:05
【问题描述】:
我正在尝试使用测试驱动开发 (TDD) 实现一些功能
我正在用 javascript 编写代码。
checkTransparency(urlString)
使透明(urlString)
是我正在尝试测试和开发的两个功能,它们位于一个名为 transcript.js 的文件中。
这些使用 inkscape 和 graphicsmagick npm。我检查了我的其他项目中的 checkTransparent 工作,但我试图确保我可以将这个 transparent.js 复制粘贴到另一个项目中并在其他地方使用它。
我的项目文件夹结构如下:
+ node_modules
+ src
--- transparent.js
+ test
--- transparent.spec.js
+ package.json
+ package-lock.json
+ jest.config.js
我使用 jest 作为我的测试框架。
问题是当我运行 jest(或 npm 测试)
我得到以下信息:
失败测试/transparent.spec.js
● 测试套件无法运行
Jest encountered an unexpected token
This usually means that you are trying to import a file which Jest cannot parse, e.g. it's not plain JavaScript.
By default, if Jest sees a Babel config, it will use that to transform your files, ignoring "node_modules".
Here's what you can do:
• To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
• If you need a custom transformation specify a "transform" option in your config.
• If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.
You'll find more details and examples of these config options in the docs:
https://jestjs.io/docs/en/configuration.html
Details:
\\..............\transparent\test\transparent.spec.js:4 <FEW DETAILS OMITTED HERE DELIBERATELY>
import { checkTransparency, makeTransparent } from "../src/transparent"; // const transparent = require("../src/transparent");
^^^^^^
SyntaxError: Cannot use import statement outside a module
at Runtime._execModule (C:/Users/Kjeong/AppData/Local/Yarn/Data/global/node_modules/jest-runtime/build/index.js:988:58)
测试套件:1 个失败,总共 1 个 测试:共 0 快照:共 0 个 时间:0.862s 运行所有测试套件。
我的 jest.config.js:
module.exports = {
testEnvironment: "node",
moduleDirectories: ["node_modules", "src", "transparent"],
moduleFileExtensions: [
"js",
"json",
"jsx",
"ts",
"tsx",
"node"
],
clearMocks: true,
}
我已经尝试了以下导出来让这个东西正常工作:
export function checkTransparency(urlString) { ... }
export function makeTransparent(urlString) {... }
module.exports = {
checkTransparency: checkTransparency,
makeTransparent: makeTransparent,
};
【问题讨论】:
标签: javascript import jestjs