我正在尝试设置Jest,以便使用带有ES6预置的Babel来处理我的env项目。该代码与Webpack一起编译和工作,但与Jest无关。问题似乎是从node_modules内部转出的代码。
package.json (纱线):
{
"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:
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的定义:
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:
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)
发布于 2018-05-08 14:04:01
因此,由于我想从ES6内部扩展node_modules类,所以我不得不在jest中显式地排除它:
"jest": {
"transformIgnorePatterns": [
"/node_modules/(?!event-pubsub).+\\.js$"
]
}这样,模块就会被导入到我的测试中(而不是转移的)。
https://stackoverflow.com/questions/50174146
复制相似问题