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

npm test fails with assets imported from node_modules folder #579

Closed
robcaldecott opened this issue Sep 5, 2016 · 7 comments
Closed

npm test fails with assets imported from node_modules folder #579

robcaldecott opened this issue Sep 5, 2016 · 7 comments

Comments

@robcaldecott
Copy link

I have published a number of in-house components that package up CSS and images so they can be included in a Webpack project like this:

import MyComponent from 'my-component'
import 'my-component/css/Styles.css'
import logo from 'my-component/img/CorporateLogo.png'

This has worked great with CRA and my apps run and build correctly.

However, any attempt to test code that includes CSS/images in this way with CRA 0.4.1 will fail - almost as if testing treats anything imported from node_modules as JavaScript.

e.g. when running a test of a component that imports a PNG from node_modules/my-component/img:

 FAIL  src\App.test.js
  ● Test suite failed to run

    C:\Users\<user>\Projects\my-app\node_modules\my-component\img\logo.png:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){�PNG
                                                                                             ^
    SyntaxError: Unexpected token ILLEGAL

      at transformAndBuildScript (node_modules\react-scripts\node_modules\jest-runtime\build\transform.js:284:10)
      at Object.<anonymous> (src\App.js:3:38)
      at Object.<anonymous> (src\App.test.js:3:38)

Any ideas or workarounds would be welcome.

@robcaldecott robcaldecott changed the title npm test fails with asset imports from node_modules folder npm test fails with assets imported from node_modules folder Sep 5, 2016
@robcaldecott
Copy link
Author

On further investigation this appears to be a problem with scopes. We are publishing packages to a private npm server and use scoping to avoid any clashes and to indicate this is a special package. So a sample package name may be:

@mycompany/my-component

https://docs.npmjs.com/private-modules/intro#setting-up-your-package

When you npm install the above it will end up in node_modules/@mycompany/my-component. This seems to be breaking the Jest config which then tries to actually import the assets as JS.

Further tests show it's that pesky @ sign that seems to break Jest. If I remove it from the folder name in node_modules then it will work OK.

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

Can you please file this in Jest repo and link to this issue? Thank you!

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

cc @cpojer

@fson
Copy link
Contributor

fson commented Sep 5, 2016

The problem seems to be in the moduleNameMapper Jest configuration used in create-react-app. It only matches paths consisting of alphanumeric characters, so files with special characters like @ are not correctly stubbed in the tests. I'm making a pull request to fix this.

@gaearon
Copy link
Contributor

gaearon commented Sep 5, 2016

Thanks @fson!

@fson fson closed this as completed in #584 Sep 10, 2016
@gaearon
Copy link
Contributor

gaearon commented Sep 18, 2016

This should be fixed in 0.4.2.
Please verify!

@llamamoray
Copy link

Hi, apologies for commenting on such an old issue, but I seem to be having a similar issue as I reported in #4262.

Is this related and a regression? The moduleNameMapper in scripts/utils/createJestConfig.js seems to have changed a great deal since this was fixed here.

@lock lock bot locked and limited conversation to collaborators Jan 19, 2019
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

5 participants