Create-react-app: setupTests.js does not work in src/ path

Created on 24 Mar 2018  路  5Comments  路  Source: facebook/create-react-app

I'm working with some simple tests. My project was made with Create React App. I have everything in a separeted folder src/tests. Everything was working fine but with just one file. When I wanted to add a new test file, I wanted to have in a single file the adapter configuration to not boilerplate the code. I added a setupTests.js to the src path and I ran the test again, but this time didn't work, throwing me the Enzyme message about the adapter. The configuration is the next and it works fine if I add it to each file.
I installed everything using npm but later I had to use yarn to resolve some dependencies issues.

  • setupTests.js
import { configure } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() });

I've seen that some people added:

{
  "jest": {
    "setupTestFrameworkScriptFile": "<rootDir>/path/to/your/file.js",
  }
}

but it didn't work for me and I think is not a good practice to ovewrite the setup file like this way.

Environment

  1. node -v: v8.9.4
  2. npm -v: 5.8.0
  3. yarn --version (if you use Yarn): 1.5.1
  4. npm ls react-scripts (if you haven鈥檛 ejected): No
  5. Operating system: Windows 10

This is the link of my tests: https://github.com/DracotMolver/React-Practicing/tree/master/DragAndDrop/src/tests

By now I have the Enzyme adapter configuration on each file.
Any help I'll be thankful.

Most helpful comment

setupTests.js should be placed in src folder. I don't see it in your example

All 5 comments

setupTests.js should be placed in src folder. I don't see it in your example

I know that must be placed there and i did. But here as i said above "... By now I have the Enzyme adapter configuration on each file". I pushed it like that because it works like this, i can pushed it in the way that doesn't work for me.

Today I had to change my OS and I couldn't replicate the issue. but now It's working. Thanks a lot @YevheniiKravchenko and @tabrindle for your time guys :+1:

I had this issue as well. And this was my fix!

Adding this to your package.json won't work if you have a jest.config.js file:
{ "jest": { "setupTestFrameworkScriptFile": "<rootDir>/path/to/your/file.js", } }

Instead you have to add this code to your jest.config.js file like so:
module.exports = { setupTestFrameworkScriptFile: "<rootDir>/src/setupTests.js" }

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sgriffey picture sgriffey  路  113Comments

godmar picture godmar  路  130Comments

xiaoxiangmoe picture xiaoxiangmoe  路  89Comments

gabrielmicko picture gabrielmicko  路  70Comments

Timer picture Timer  路  144Comments