Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform not applied to file in parent directory #8238

Closed
dandv opened this issue Mar 29, 2019 · 5 comments
Closed

transform not applied to file in parent directory #8238

dandv opened this issue Mar 29, 2019 · 5 comments

Comments

@dandv
Copy link
Contributor

dandv commented Mar 29, 2019

🐛 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 
@dandv
Copy link
Contributor Author

dandv commented Mar 29, 2019

I tried adding rootDir": "../", didn't help.

@dandv
Copy link
Contributor Author

dandv commented Apr 28, 2019

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.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Feb 17, 2023
@github-actions
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Mar 19, 2023
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 19, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants