【发布时间】:2019-01-22 12:39:46
【问题描述】:
我希望这只是我的一个简单错误,但这就是我正在做的事情。我有一个 react native 项目,我正在通过 url 导入另一个 react native 项目,它将成为一个公共库。因为现在还很早,所以我没有在这个其他项目中编译代码。我可以从那里导入组件并运行 web 应用程序,但我在测试时遇到了问题(开玩笑)。 我挖了一下,发现你应该在你的 jest 配置中将非转译节点模块列入白名单,如下所示:
transformIgnorePatterns: [
"<rootDir>/node_modules/(?!(react-native|my-module))/"
],
但是,这对我在运行测试时遇到的错误没有影响:
FAIL src/App.test.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:
/path/to/my/workspace/node_modules/my-module/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import { AppRegistry } from "react-native";
^^^^^^
SyntaxError: Unexpected token import
1 | import {combineReducers} from "redux";
2 | import {reducer as formReducer} from "redux-form";
> 3 | import partialForm from "my-module/src/reducers/form";
| ^
4 | import progress from "my-module/src/reducers/progress";
5 | // import theme from "my-module/src/reducers/theme";
6 |
at ScriptTransformer._transformAndBuildScript (node_modules/jest-runtime/build/script_transformer.js:403:17)
at Object.<anonymous> (src/reducers/index.js:3:1)
谁能指出我的(可能)基本错误?
【问题讨论】:
标签: reactjs jestjs babel-jest