首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >巴贝尔是和Webpack一起编译的,而不是用Jest编译的

巴贝尔是和Webpack一起编译的,而不是用Jest编译的
EN

Stack Overflow用户
提问于 2018-05-04 11:44:20
回答 1查看 678关注 0票数 0

我正在尝试设置Jest,以便使用带有ES6预置的Babel来处理我的env项目。该代码与Webpack一起编译和工作,但与Jest无关。问题似乎是从node_modules内部转出的代码。

package.json (纱线):

代码语言:javascript
复制
{
  "name": "generative-toolbox",
  "version": "0.0.1",
  "main": "toolbox.js",
  "repository": "git@bitbucket.org:yojeek/generative-toolbox.git",
  "author": "msamoylov <antiplaka@gmail.com>",
  "license": "MIT",
  "private": true,
  "scripts": {
    "build": "yarn webpack --config webpack.config.js",
    "test": "jest --debug"
  },
  "devDependencies": {
    "babel-jest": "^22.4.3",
    "jest": "^22.4.3",
    "regenerator-runtime": "^0.11.1",
    "webpack-cli": "^2.1.2"
  },
  "dependencies": {
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.4",
    "babel-preset-env": "^1.6.1",
    "babel-register": "^6.26.0",
    "dat.gui": "^0.7.1",
    "enumify": "^1.0.4",
    "event-pubsub": "^4.3.0",
    "webpack": "^4.6.0"
  },
  "babel": {
    "presets": [
      ["env"],
      "stage-2"
    ],
    "env": {
      "test": {
        "presets": [
          ["env", {
            "targets": {
              "node": "9.4.0"
            },
            "debug": true
          }],
          "stage-2"
        ]
      }
    }
  }
}

webpack.config.js:

代码语言:javascript
复制
var path = require("path");

module.exports = {
  entry: "./toolbox.js",
  mode: 'development',
  output: {
    path: path.resolve(__dirname, "dist"),
    filename: "generative.toolbox.js",
    publicPath: '/dist/',
    library : "GenT"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader'
        }
      }
    ]
  }
}

来自toolbox.js的定义:

代码语言:javascript
复制
import EventPubSub from 'event-pubsub'

class GUI {
  gui
  config
  values

  constructor() {
    // some valid code
  }
}


class MIDI extends EventPubSub {
  midi

  constructor() {
    super()

    // some valid code down there
  }
}

test.js:

代码语言:javascript
复制
import {GUI, MIDI} from './toolbox.js'

test('gui sanity', () => { // <--- pass
  let gui = new GUI()
  expect(gui).toBeTruthy()
});

test('midi sanity', () => { // <--- fail
  let midi = new MIDI()
  expect(midi).toBeTruthy()
});

测试失败,结果如下:

TypeError:类构造函数EventPubSub不能在对象处的新midi (toolbox.js:101:17)处调用' new‘99 \ MIDI 100 >101个(){102-SuperSuper()103比对104 //请求MIDI访问。(test.js:15:14)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-05-08 14:04:01

因此,由于我想从ES6内部扩展node_modules类,所以我不得不在jest中显式地排除它:

代码语言:javascript
复制
  "jest": {
    "transformIgnorePatterns": [
      "/node_modules/(?!event-pubsub).+\\.js$"
    ]
  }

这样,模块就会被导入到我的测试中(而不是转移的)。

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

https://stackoverflow.com/questions/50174146

复制
相关文章

相似问题

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