I have an app inside my nx project, which uses array-move.
When writing a test for this app, jest crashes with:
Details:
.../node_modules/array-move/index.js:1
({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){export function arrayMoveMutable(array, fromIndex, toIndex) {
^^^^^^
SyntaxError: Unexpected token 'export'
Updating the jest configuration
Therefore, I've modified the jest config like this:
+ // Add modules to transform
+ const esModules = ['array-move'].join('|')
module.exports = {
displayName: 'project',
preset: '../../jest.preset.js',
+ transformIgnorePatterns: [`node_modules/(?!${esModules})/`],
transform: {
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nrwl/react/plugins/jest',
'^.+\\.[tj]sx?$': 'babel-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/apps/project,
collectCoverageFrom: [
'**/*.{ts,js,jsx,tsx,mjs}',
'!.storybook/**/*',
'!**/*.stories.tsx',
'!src/environments/**/*',
],
}
Follow up error (where I'm stuck at)
However, I ended up with the following error:
Cannot find module './.config/babel.config'
Require stack:
- .../node_modules/@babel/core/lib/config/files/configuration.js
- .../node_modules/@babel/core/lib/config/files/index.js
- .../node_modules/@babel/core/lib/index.js
- .../node_modules/jest-snapshot/build/InlineSnapshots.js
- .../node_modules/jest-snapshot/build/State.js
- .../node_modules/jest-snapshot/build/index.js
- .../node_modules/jest-runtime/build/index.js
- .../node_modules/@jest/core/build/cli/index.js
- .../node_modules/@jest/core/build/jest.js
- .../node_modules/jest/build/jest.js
- .../node_modules/@nrwl/jest/src/executors/jest/jest.impl.js
- .../node_modules/nx/src/config/workspaces.js
- .../node_modules/nx/src/command-line/run.js
- .../node_modules/nx/bin/run-executor.js
Require stack:
.../node_modules/@babel/core/lib/config/files/configuration.js
.../node_modules/@babel/core/lib/config/files/index.js
.../node_modules/@babel/core/lib/index.js
.../node_modules/jest-snapshot/build/InlineSnapshots.js
.../node_modules/jest-snapshot/build/State.js
.../node_modules/jest-snapshot/build/index.js
.../node_modules/jest-runtime/build/index.js
.../node_modules/@jest/core/build/cli/index.js
.../node_modules/@jest/core/build/jest.js
.../node_modules/jest/build/jest.js
.../node_modules/@nrwl/jest/src/executors/jest/jest.impl.js
.../node_modules/nx/src/config/workspaces.js
.../node_modules/nx/src/command-line/run.js
.../node_modules/nx/bin/run-executor.js
at loadConfig (../../node_modules/@babel/core/lib/config/files/configuration.js:172:5)
at loadConfig.next (<anonymous>)
at mergeExtendsChain (../../node_modules/@babel/core/lib/config/config-chain.js:387:45)
at mergeExtendsChain.next (<anonymous>)
at ../../node_modules/@babel/core/lib/config/config-chain.js:373:20
at loadFileChain (../../node_modules/@babel/core/lib/config/config-chain.js:232:24)
at loadFileChain.next (<anonymous>)
I have a .babelrc in the root of the nx app, and there is a babel.config.json in the project root.