Home > Enterprise >  No tests found using react-scripts and jest
No tests found using react-scripts and jest

Time:01-15

Trying to run just tests.

I need to test reducers and storage

I read the jest tutorial-react manual and it's a little different from mine, but I also found information that "react-scripts test" should also work

My package.json

  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "dependencies": {
    "@testing-library/jest-dom": "^5.16.1",
    "@testing-library/react": "^12.1.2",
    "@testing-library/user-event": "^13.5.0",
    "classnames": "^2.3.1",
    "faker": "^4.1.0",
    "jest": "^27.4.7",
    "miragejs": "^0.1.43",
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-scripts": "5.0.0",
    "redux": "^4.1.2",
    "seedrandom": "^3.0.5",
    "web-vitals": "^2.1.3"
  },
  "devDependencies": {
    "jest-watch-typeahead": "^0.6.5",
    "prettier": "^2.5.1"
  }

I'm trying to use

npm test
npm test --config jest.config.js
npm test --setupFile ./src/setupTests.js
npm test --setupFiles ./src/setupTests.js

Output

Active Filters: filename .\\src\\setupTests.js/
 › Press c to clear filters.
 
Watch Usage
 › Press a to run all tests.
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.
 

Press A

No tests found, exiting with code 0

Watch Usage: Press w to show more.

./jest.config.js

// Sync object
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
    setupFiles: ['./src/setupTests.js'], 
    verbose: true,
};

module.exports = config;

./src/setupTests.js

import '@testing-library/jest-dom/extend-expect';

test('adds 1   2 to equal 3', () => {
    expect(1   2).toBe(3);
});

CodePudding user response:

This is what the Jest documentation says about how it finds test files:

By default it looks for .js, .jsx, .ts and .tsx files inside of __tests__ folders, as well as any files with a suffix of .test or .spec (e.g. Component.test.js or Component.spec.js). It will also find files called test.js or spec.js.

Since the setupTests.js filename does not meet this criteria, Jest does not recognize it as a test file. If you rename the file to end with .test.js or move it into a __tests__ folder, Jest should be able to find it and run the test. If you need to keep the filename as is, this default behavior can be changed by setting testRegex in jest.config.js (read more about this setting here).

The setupFiles property in jest.config.js is used for setting up the environment before the tests actually run. The documentation for that setting can be found here, but it doesn't appear to be necessary to do any setup for this case based code you provided.

  •  Tags:  
  • Related