首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用使用node_modules的es6设置玩笑

如何使用使用node_modules的es6设置玩笑
EN

Stack Overflow用户
提问于 2020-03-16 22:40:33
回答 2查看 19.6K关注 0票数 15

我有一个非常简单的测试:

代码语言:javascript
复制
describe('sanity', () => {
  it('sanity', () => {
    expect(true).toBeTruthy()
  })
})

我收到了以下错误:

代码语言:javascript
复制
 FAIL  spec/javascript/sanity_test.js
  ● Test suite failed to run

    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:

    /Users/piousbox/projects/ruby/<project>/node_modules/@atlaskit/tooltip/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){export { default } from './components/Tooltip';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token export

      3 | import update from "immutability-helper";
      4 | import {components} from "react-select-2";
    > 5 | import Tooltip from "@atlaskit/tooltip";
        | ^
      6 | const isEqual = require("react-fast-compare");
      7 | import _, {replace} from "lodash";
      8 | import { get } from "$shared/request";

      at ScriptTransformer._transformAndBuildScript (node_modules/@jest/transform/build/ScriptTransformer.js:537:17)
      at ScriptTransformer.transform (node_modules/@jest/transform/build/ScriptTransformer.js:579:25)
      at Object.<anonymous> (app/javascript/customer2/components/fob/fob_utils.js:5:1)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.593s

我有一个.babelrc:

代码语言:javascript
复制
{
  "presets": ["@babel/react", "@babel/env"]
}

我如何使琐碎的考试通过?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-03-17 02:59:58

有两种方法可以通过这个测试:

选项1.)通过添加测试env选项( testing环境标志将在package.json脚本中定义,例如:"test": "NODE_ENV=testing jest""test": "BABEL_ENV=testing jest")来处理ES6导入.

babel.config.js

代码语言:javascript
复制
module.exports = api => {
  api.cache(true);

  return {
    presets: ["@babel/preset-env", "@babel/preset-react"],
    plugins: [
      "@babel/plugin-transform-runtime",
      ["@babel/plugin-proposal-class-properties", { loose: true }],
    ],
    env: {
      testing: {
        presets: [
          [ "@babel/preset-env", { targets: { node: "current" }}],
        ],
      },
    },
  };
};

选项2.)将ES6模块转换为webpack.config.js配置中的ES5语法:

webpack.config.js

代码语言:javascript
复制
const { NODE_ENV } = process.env
const inDevelopment = NODE_ENV === "development";

module.exports = {
  ...
  module: {
    rules: [
      ...
      {
        test: /\.(js|jsx)$/,
        loader: "babel-loader",
        exclude: !inDevelopment ? /node_modules\/(?!(@atlaskit\/tooltip))/ : /(node_modules)/,
        options: {
          cacheDirectory: inDevelopment,
          cacheCompression: false,
        },
      },
      ...
    ],
  }
  ...
}

这两个选项之间的主要区别是,第一个选项只能在测试环境中工作。如果您试图在开发/生产环境中使用它,它可能会影响其他第三方包并导致编译错误。因此,如果计划将其迁移到支持IE11及以下的生产环境中,则建议使用第二个选项。但是,请记住,这将在每次创建产品构建和/或运行测试套件时传递包。因此,如果您正在处理一个非常大的项目(或者转移多个ES6包),那么它的资源就会非常多。因此,我建议将第三方包从ES6编译到ES5,并在本地或私下(通过NPM包)安装它/它们。

工作示例(此示例包括第二个选项):https://github.com/mattcarlotta/transpile-es6-module

安装:

  1. cd ~/Desktop && git clone git@github.com:mattcarlotta/transpile-es6-module.git
  2. cd transpile-es6-module
  3. yarn install
  4. yarn dev运行demo
  5. yarn test以运行测试套件
票数 3
EN

Stack Overflow用户

发布于 2020-03-17 21:31:47

马特的回答是接受提单的,这是很有见地的。为我所做的改变是在package.json中添加:

代码语言:javascript
复制
  "jest": {
    ...
    "transformIgnorePatterns": [
      "node_modules/(?!@atlaskit)"
    ],

您可以同时添加对多个包的支持,方法是使用|将它们分隔开。

代码语言:javascript
复制
  "jest": {
    ...
    "transformIgnorePatterns": [
      "node_modules/(?!module1|module2|etc)"
    ],
票数 30
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60714101

复制
相关文章

相似问题

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