Jest: transform not applied to file in parent directory

Created on 29 Mar 2019  路  2Comments  路  Source: facebook/jest

馃悰 Bug Report

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.

To Reproduce

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:

  1. git clone [email protected]:dandv/jest-parent-dir-transform.git
  2. cd jest-parent-dir-transform/backend
  3. git checkout 50a49bc
  4. npm install
  5. npm test

Expected behavior

Both tests pass

Link to repl or repo (highly encouraged)

https://github.com/dandv/jest-parent-dir-transform at commit #50a49bc

Run 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 
Bug Report Needs Repro Needs Triage

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 .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.

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings