Create-react-app: Jest ignoring tests inside src/node_modules

Created on 15 Nov 2016  路  9Comments  路  Source: facebook/create-react-app

I created a src/node_modules directory so I can have shared code I re-use throughout my project.

So rather than doing

import MyComponent from '../../../../shared/MyComponent';

I could write

import MyComponent from 'shared/MyComponent';

This works great, except when I add a test MyComponent.test.js. It isn't "seen" by Jest.

Not sure if this is a supported use case, it might just be a nice-to-have.

bug

Most helpful comment

Closing in favor of https://github.com/facebookincubator/create-react-app/issues/1333.

We'll likely support Yarn Workspaces and/or Lerna with a top-level packages directory instead of src/node_modules.

All 9 comments

i suspect this is expected behavior, since 'node_modules' is actually one of the folders jest is configured to ignore since

  1. you don't need to test dependencies installed in node_modules
  2. there are too many files in node_modules and reading all of them would hurt performance

It makes sense if this is intended behavior, but to be clear, the node_modules directory I created is under src so it only has files I've added.

there is however another node_modules in the root of your project, and that name is usually ignored by most tools for testing/linting/indexing because of the large number of dependencies usually installed in there. webpack has a configuration option for specifying the 'root' folder of your project so that all imports from / start at src/, i'll investigate and make a PR because i also need this but have been lazy to add it :)

Just submitted a PR so that we can do import MyComponent from 'components/MyComponet' or import MyComponent from 'MyComponent' if MyComponent is in src :D crossing fingers hoping its accepted

Let's do this (support for src/node_modules). A PR would be welcome.

544 should take care of supporting this case

I have a similar issue. I'm attempting to run jest from inside the src/node_modules directory.
Eg.

cd ./src/node_modules/my-package
jest

I'm able to run tests after I move my-package to somewhere else.

I think one key thing to keep in mind is that whenever there is a node_modules folder without a package.json at the same-level, it is most likely user-generated.

At our organization, the "top-level" of our git package source is located under a src/ directory during our build process, so this would break testing during the build phase since the third-party node_modules would also be installed under this src/ directory from package.json at that same level.

Closing in favor of https://github.com/facebookincubator/create-react-app/issues/1333.

We'll likely support Yarn Workspaces and/or Lerna with a top-level packages directory instead of src/node_modules.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

rdamian3 picture rdamian3  路  3Comments

dualcnhq picture dualcnhq  路  3Comments

fson picture fson  路  3Comments

Aranir picture Aranir  路  3Comments

fson picture fson  路  3Comments