I'm using Jest with .mjs files and babel-plugin-transform-es2015-modules-commonjs in a monorepo with several projects: backend, client etc. The projects share a common lib folder.
In test files located in backend, importing modules from backend or a subdirectory of it works fine. Importing from the ../lib/ directory however, causes Jest to fail with Jest encountered an unexpected token / SyntaxError: Unexpected token export.
monorepo/backend/package.json
{
"name": "backend",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"devDependencies": {
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
"jest": "^24.5.0"
},
"jest": {
"testEnvironment": "node",
"testMatch": [
"**/?(*.)(spec|test).?(m)js?(x)"
],
"moduleFileExtensions": [
"js",
"mjs"
],
"transform": {
"^.+\\.m?js$": "babel-jest"
}
},
"babel": {
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
}
monorepo/backend/lib/backend-lib.mjs
export function backend() {
return 'backend';
}
monorepo/backend/use-backend-lib.test.mjs
// Works fine
import { backend } from './lib/backend-lib';
test('test using backend library', () => {
expect(backend()).toBeTruthy();
});
*monorepo/lib/project-lib.mjs
export function foo() {
return 'foo';
}
monorepo/backend/use-project-lib.test.mjs
// Jest encountered an unexpected token
import { foo } from '../lib/project-lib';
test('test using project-level library', () => {
expect(foo()).toBeTruthy();
});
Steps to reproduce the behavior:
git clone [email protected]:dandv/jest-parent-dir-transform.gitcd jest-parent-dir-transform/backendgit checkout 50a49bcnpm installnpm testBoth tests pass
https://github.com/dandv/jest-parent-dir-transform at commit #50a49bc
npx envinfo --preset jest System:
OS: Linux 4.15 Ubuntu 18.04.2 LTS (Bionic Beaver)
CPU: (8) x64 Intel(R) Core(TM) i5-8350U CPU @ 1.70GHz
Binaries:
Node: 11.12.0 - /usr/bin/node
Yarn: 1.15.2 - /usr/bin/yarn
npm: 6.9.0 - /usr/bin/npm
npmPackages:
jest: ^24.5.0 => 24.5.0
I tried adding rootDir": "../", didn't help.
Any updates on this? Sharing libraries in a monorepo is a pretty common scenario, and Node v12 has also re-affirmed that .mjs files are here to stay.
I've copied package.json into the lib and root folders, and ran npm install, but having babel-plugin-transform-es2015-modules-commonjs installed in all directories didn't help.
Installing the lib dependency with a relative path in backend/package.json didn't work either.
No amount of tweaking rootDirs or the confusingly documented transformIgnorePatterns helped either.
I did find a workaround, but I don't understand why the original babel config from package.json doesn't work for the test that imports a file from ../lib/project-lib.
Most helpful comment
Any updates on this? Sharing libraries in a monorepo is a pretty common scenario, and Node v12 has also re-affirmed that
.mjsfiles are here to stay.I've copied
package.jsoninto theliband root folders, and rannpm install, but havingbabel-plugin-transform-es2015-modules-commonjsinstalled in all directories didn't help.Installing the
libdependency with a relative path inbackend/package.jsondidn't work either.No amount of tweaking
rootDirsor the confusingly documentedtransformIgnorePatternshelped either.I did find a workaround, but I don't understand why the original babel config from
package.jsondoesn't work for the test that imports a file from../lib/project-lib.