首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jest遇到意外的令牌-导入'promise-polyfill/src/polyfill‘中断测试

Jest遇到意外的令牌-导入'promise-polyfill/src/polyfill‘中断测试
EN

Stack Overflow用户
提问于 2018-12-27 23:42:39
回答 1查看 408关注 0票数 1

当我将这段代码添加到代码中时,我的Jest测试就会中断:

import 'promise-polyfill/src/polyfill';

代码语言:javascript
复制
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

我的package.json是:

代码语言:javascript
复制
{
  "name": "fas-app",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "ajv": "^6.5.3",
    "babel": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "connected-react-router": "^4.4.1",
    "formik": "^1.1.1",
    "hello.js": "^1.0.0",
    "hellojs": "^2.0.0-4",
    "history": "^4.7.2",
    "immutability-helper": "^2.8.0",
    "loadjs": "^3.5.4",
    "moment": "^2.22.2",
    "promise-polyfill": "8.1.0",
    "react": "^16.4.2",
    "react-bs-notifier": "^5.0.0",
    "react-datepicker": "^1.6.0",
    "react-dom": "^16.4.2",
    "react-facebook-login": "^4.0.1",
    "react-ga": "^2.5.6",
    "react-google-oauth": "^1.0.0",
    "react-redux": "^5.0.7",
    "react-router": "^4.3.1",
    "react-router-dom": "^4.3.1",
    "react-scripts": "^2.1.1",
    "react-tooltip": "^3.9.0",
    "recharts": "^1.3.0",
    "redux": "^4.0.0",
    "redux-thunk": "^2.3.0",
    "whatwg-fetch": "^3.0.0",
    "yup": "^0.26.6"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "jest": "^23.6.0"
  },
  "browserslist": [
    ">0.2%",
    "not dead",
    "not ie <= 11",
    "not op_mini all"
  ]
}

我已经尝试设置了这里记录的配置内容:

https://jestjs.io/docs/en/configuration.html#modulenamemapper-object-string-string

我不知道该将它添加到哪个文件中,也不知道如何设置它。这个polyfill所做的就是提供IE支持,所以我不需要它(或者可以为Jest模拟它),我只是想不出如何让Jest在测试中模拟它。

我该怎么做?

EN

回答 1

Stack Overflow用户

发布于 2019-01-28 06:09:28

它看起来像你创建了一个React-Redux应用程序,并只使用酶来测试这一点。

如果您正在创建一个单页面应用程序,那么最佳实践是创建一个索引页面来设置环境并呈现您的App组件。

如果您在这里设置了您的环境(例如promise-polyfill),您可以保证它将出现在项目的其余部分中。

您还将使用类似以下内容呈现应用程序:

代码语言:javascript
复制
ReactDOM.Render(App/>, document.getElementById('root'))

然而,该过程的索引部分不应该定期进行测试。首先,你必须在jsdom中添加一个id为'root‘的元素。

相反,您应该测试App组件本身,而不应该导入索引页面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53947499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档