I’m using an index.js file to export several files from the same folder (objects/Rectangle) :
// index.js
export { default } from './RectangleBuilder'
export { default as Rectangle } from './Rectangle'
export { default as MovableRectangle } from './MovableRectangle'
everything works fine, expect when I’m running tests: Jest is trying to run this piece of code
import RectangleBuilder from 'objects/Rectangle'
import Point from 'objects/Point'
import TrapSystem from 'objects/TrapSystem'
import Interval from 'objects/Interval'
import Color from 'effects/Color'
const spawn = new RectangleBuilder(20 * 2, 100 * 2, 80 * 2, 80 * 2)
.withColor(new Color('lightblue'))
.build()
and It’s giving me this error :
TypeError: _Rectangle2.default is not a constructor
6 |
7 | console.log(RectangleBuilder)
> 8 | const spawn = new RectangleBuilder(20 * 2, 100 * 2, 80 * 2, 80 * 2)
| ^
9 | .withColor(new Color('lightblue'))
10 | .build()
11 |
RectangleBuilder
is indeed undefined
.
Is there a way to get Jest to import that correctly, just as when I’m running the code in the browser ?
EDIT :
Jest needs es6 imports to be transpiled into commonJS imports as it is running in node. Could this be coming from my config?
// .babelrc
{
"plugins": [
[
"module-resolver",
{
"root": ["./src"],
"alias": {
"test": "./test",
"underscore": "lodash"
}
}
]
],
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }]],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
// webpack.config.babel.js
module.exports = {
entry: './src/sketch.js',
output: {
path: __dirname,
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader'
}
}
]
}
}