jest icon indicating copy to clipboard operation
jest copied to clipboard

Jest unable to resolve node_modules at the root in Yarn workspace

Open wan54 opened this issue 7 years ago • 22 comments

Jest v22.1.1

To enhance lerna in my project I have enabled yarn workspaces. Since then transformIgnorePatterns does not seem to be able to resolve node_modules at the root.

The same jest config used before switching to yarn workspace:

"jest": {
    "modulePaths": [
        "<rootDir>/src",
        "<rootDir>/node_modules"
      ],
    "setupTestFrameworkScriptFile": "./jest-setup.js",
    "transformIgnorePatterns": [
      "node_modules/(?!lodash-es|react-virtualized|redux-form)"
    ],
    "testPathIgnorePatterns": [
      "/node_modules",
      "/lib/",
      "/es/"
    ],
    "testRegex": "src/.*\\.spec\\.js$",
    "testEnvironment": "jsdom"
  }

This causes Unexpected token import error.

/path/to/project/node_modules/redux-form/es/Field.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){import createField from './createField';
                                                                                             ^^^^^^

    SyntaxError: Unexpected token import

      1 | import React from 'react';
      2 | import PropTypes from 'prop-types';
    > 3 | import ReduxFormField from 'redux-form/es/Field';
      4 | import isEmpty from 'lodash/isEmpty';
      5 | import reduce from 'lodash/reduce';
      6 | import keys from 'lodash/keys';

Is there workaround or proper configuration for allowing jest to play nice in yarn workspaces?

Thanks.

wan54 avatar Jan 16 '18 08:01 wan54

Can you provide a reproduction repo?

SimenB avatar Jan 16 '18 08:01 SimenB

Possibly, but not immediately.

wan54 avatar Jan 16 '18 10:01 wan54

After looking at the error again. Jest seemed like able to resolve it but didn't transform the module.

wan54 avatar Jan 16 '18 10:01 wan54

Related to #5108. The difference in my case is they are 3rd party packages - redux-form, lodash-es, etc. And they're not symlinked. They're hoisted up by yarn workspaces.

Just to test it out I manually symlinked .babelrc from my package folder to each of these 3rd party module and it worked.

wan54 avatar Jan 16 '18 11:01 wan54

My workaround:

const { exec } = require('child_process');

module.exports = () => {
    const modules = [
        'lodash-es',
        'redux-form',
        'react-virtualized'
    ];
    modules.forEach((module) => {
        exec(`ln -vsf ../../src/my-package/.babelrc ../../node_modules/${module}`);
    });
};

And run it once via globalSetup

wan54 avatar Jan 18 '18 04:01 wan54

I'm not convinced that symlinking everything is the right solution to what is definitely a real issue. It might be worth reopening this.

egid avatar Feb 02 '18 01:02 egid

I agree, that shouldn't be necessary

SimenB avatar Feb 20 '18 07:02 SimenB

We will need a reproduction repo, though

SimenB avatar Feb 20 '18 07:02 SimenB

Full reproduction and steps here: https://github.com/kcjonson/jest-hoisting-bug-demo thats the most simple case I could whip up, I'd love to hear that I missed some simple config.

kcjonson avatar Feb 20 '18 22:02 kcjonson

Webpack isn't running babel on your packages. You need to add your packages path to the babel step in your webpack config.

bvellacott avatar Mar 24 '18 03:03 bvellacott

Here is a workaround that i found. If you know which libraries will be hoisted, you can change the transformIgnorePatterns path to point at the different node_modules

"transformIgnorePatterns": [
      "../node_modules/(?!redux-form)",
      "node_modules/(?!lodash-es|react-virtualized)"
    ],

in my case, the package folders are right under the root workspace. If you put the packages under the package folder, you might need to change to "../../node_modules/(?!redux-form)",.

hfter789 avatar May 11 '18 18:05 hfter789

I have the same issue and I can't find a workaround

ematipico avatar Feb 12 '19 12:02 ematipico

Having this issue as well

rjhilgefort avatar Apr 10 '19 22:04 rjhilgefort

none of the above mentioned fix this... and I still can not find a workaround :'(

leoyli avatar Jul 16 '19 00:07 leoyli

Okay, I somehow find my way to work around this, there are server tiny caveats:

https://github.com/formatjs/react-intl/issues/1371#issuecomment-511621530

leoyli avatar Jul 16 '19 01:07 leoyli

Can anyone out together a reproduction? The repo from @kcjonson should be fixed in RN 0.59 due to https://github.com/facebook/react-native/pull/22972

You shouldn't need to do .. etc in your config, so I'd love to get this fixed

SimenB avatar Jul 16 '19 06:07 SimenB

change import from import ReduxFormField from 'redux-form/es/Field'; to import {Field} from 'redux-form'; Use Field instead ReduxFormField

benderrrr avatar Sep 21 '19 09:09 benderrrr

Can anyone out together a reproduction?

@SimenB I did a reproduction repo. You can find it here jest-resolve-issue

tamazlykar avatar Jul 04 '20 06:07 tamazlykar

Any news on this?

Snapu avatar Jun 09 '21 08:06 Snapu

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[bot] avatar Jun 09 '22 08:06 github-actions[bot]

I solved this by using moduleNameMapper, where @fta/components-token is the name of a package of my workspace and <rootDir>../token/src is the path to its index:

moduleNameMapper: {
    '@fta/components-token': '<rootDir>../token/src',
},

or '@fta/components-(.*)$': '<rootDir>../$1/src', in case the package sufix name is the same of the folder.

roni-castro-db avatar Jun 28 '22 22:06 roni-castro-db

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[bot] avatar Jun 06 '24 02:06 github-actions[bot]

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[bot] avatar Jul 06 '24 03:07 github-actions[bot]

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[bot] avatar Aug 06 '24 00:08 github-actions[bot]