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

Jest unable to resolve node_modules at the root in Yarn workspace #5325

Closed
wan54 opened this issue Jan 16, 2018 · 24 comments
Closed

Jest unable to resolve node_modules at the root in Yarn workspace #5325

wan54 opened this issue Jan 16, 2018 · 24 comments

Comments

@wan54
Copy link

wan54 commented Jan 16, 2018

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.

@SimenB
Copy link
Member

SimenB commented Jan 16, 2018

Can you provide a reproduction repo?

@wan54
Copy link
Author

wan54 commented Jan 16, 2018

Possibly, but not immediately.

@wan54
Copy link
Author

wan54 commented Jan 16, 2018

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

@wan54
Copy link
Author

wan54 commented Jan 16, 2018

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
Copy link
Author

wan54 commented Jan 18, 2018

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

@egid
Copy link

egid commented Feb 2, 2018

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

@SimenB
Copy link
Member

SimenB commented Feb 20, 2018

I agree, that shouldn't be necessary

@SimenB SimenB reopened this Feb 20, 2018
@SimenB
Copy link
Member

SimenB commented Feb 20, 2018

We will need a reproduction repo, though

@kcjonson
Copy link

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.

@bvellacott
Copy link

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

@hfter789
Copy link

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)",.

@ematipico
Copy link

ematipico commented Feb 12, 2019

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

@rjhilgefort
Copy link

Having this issue as well

@leoyli
Copy link

leoyli commented Jul 16, 2019

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

@leoyli
Copy link

leoyli commented Jul 16, 2019

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

formatjs/formatjs#1371 (comment)

@SimenB
Copy link
Member

SimenB commented Jul 16, 2019

Can anyone out together a reproduction? The repo from @kcjonson should be fixed in RN 0.59 due to facebook/react-native#22972

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

@benderrrr
Copy link

benderrrr commented Sep 21, 2019

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

@tamazlykar
Copy link

Can anyone out together a reproduction?

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

@Snapu
Copy link

Snapu commented Jun 9, 2021

Any news on this?

@github-actions
Copy link

github-actions bot commented Jun 9, 2022

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 Jun 9, 2022
@roni-castro-db
Copy link

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.

@github-actions github-actions bot removed the Stale label Jun 28, 2022
Copy link

github-actions bot commented Jun 6, 2024

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 Jun 6, 2024
Copy link

github-actions bot commented Jul 6, 2024

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 Jul 6, 2024
Copy link

github-actions bot commented Aug 6, 2024

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 Aug 6, 2024
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