Skip to content

npm test fails with assets imported from node_modules folder #579

@robcaldecott

Description

@robcaldecott

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions