Jest "No tests found, exiting with code 1" error on Windows 10 in React Redux application
Asked Answered
S

12

47

I am attempting to run Jest on my project. I am on a Windows 10. I only have one test in one test file.

In package.json:

    "test": "jest"

My directory structure:

    src/
        app/
            routeName/
                redux/
                    action.tests.js

My output:

No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In C:\Users\myUsername\Documents\myApp
  47 files checked.
  testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.)+(spec|test).[tj]s?(x) - 0 matches
  testPathIgnorePatterns: \\node_modules\\ - 47 matches
  testRegex:  - 0 matches
Pattern:  - 0 matches
npm ERR! Test failed.  See above for more details.

According to the documentation here, Jest should look for anything that has test.js in the name.

I have also tried tests.js and that didn't work either.

I created a folder in the root of the project and put a test in there as __tests__/tests.js and that did work, but I do not want it placed there.

I have seen several tickets and questions about questions that are superficially similar, but all of those involve more complex configurations, or bugs that were supposedly patched already. I have no special configurations set. The documentation says this should work. Tutorials I have read for Jest include the exact setup I am currently using.

I am using Babel and Webpack. I installed the jest and babel-jest packages. I also added jest to the ESLint environment.

[edit] updated to properly document my problem, which I answered below. I lost track of my file naming.

Speculator answered 28/5, 2019 at 18:28 Comment(1)
I was getting the same error when running the test from IntelliJ. In my case, the run configuration was not set properly. I had to correctly define the 'Jest package' field in the configuration with '~you-project/node_modules/jest'. There was 'node_modules/react-scripts' originally. I stumbled over this topic here while searching for my solution, so maybe it will help someone.Farseeing
S
48

I am a bloody idiot and I didn't pay attention to the details.

Jest is looking for test.js specifically. Not testS.js. My files were labeled as tests.js. It worked inside __tests__/ because Jest just looks for anything inside there.

I got it working just fine now, and it shall remain up as a testament to me not looking at the specifics of the regex matches.

Speculator answered 28/5, 2019 at 20:35 Comment(2)
In my case, I had forgotten to locate the test in a __tests__/ directory.Ssm
Oh the hours of my life that have gone to off-by-one errors with s characters...Trumpeter
H
22

I had the same Problem with a React-App. Make sure, that the jest config file has the correct file pattern to find the tests:

// jest.config.js
module.exports = {
    testMatch: [
        '<rootDir>/src/**/*.test.js',
        '<rootDir>/src/**/*.test.jsx',
    ],
    ...
}
Herold answered 8/3, 2021 at 8:6 Comment(1)
OMG! Thank you!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!Miscellanea
A
7

As an alternative scenario, assuming you don't have tests in your project,

To turn this behavior off, you can add a --passWithNoTests flag in the scripts of your package.json file, i.e.:

{
  "scripts": {
    "test": "react-scripts test --passWithNoTests"
  }
}
Aryan answered 11/5, 2023 at 7:30 Comment(0)
D
1
No tests found, exiting with code 1


All I had to do was npm install.

Then I hit the Debug link at the top of my test. The test was found and execution stopped at the breakpoint. A simple solution to a very frustrating problem.

Daugherty answered 15/6, 2019 at 2:42 Comment(0)
W
1

From my side, I was getting this error because I had placed myself (cd) in the directory where the chromeDriver was installed, so as not to have to add its path to $Path. After I added chromeDriver to $Path, and placed myself in the directory of my project, everything went fine.

Whoa answered 23/6, 2020 at 10:17 Comment(2)
What is the path of chromeDriver?Welford
Leopold can you please share what you entered into your PATH ?Dispatcher
A
0

I had the same problem, I wanted to run my tests inside drone CI pipeline and had the same error what solved my problem was simply adding workspace to my project.

Antabuse answered 23/2, 2021 at 6:59 Comment(0)
B
0

You just need to change the name of the file. For example if you have a customer.js or customer.ts file and you want to test it. Create a new file name is customer.test.js or customer.test.ts after npm test it will test the file which just for testing .

Beasley answered 16/8, 2021 at 15:45 Comment(0)
W
0

check if it is small case like filename.test.js. I made a mistake ComponentName.Test.js and got error, fixed it by ComponetName.test.js

Wilie answered 1/12, 2021 at 9:5 Comment(0)
P
0

I have --passWithNoTests in my package.json file. Still facing the issues. because of my test case file named without test. Try checking your test file name once.

Preface answered 27/6, 2023 at 9:56 Comment(0)
N
0

1/ Modify your package.json add --testMatch /tests//*.test.js in package.json file. Example:

scripts": {
    "start": "react-scripts --openssl-legacy-provider start",
    "build": "react-scripts --openssl-legacy-provider build",
    "test": "cross-env JEST_TEST=false jest",
    "eject": "react-scripts eject",
    "test:ci": "npm run test -- --testResultsProcessor=\"jest-junit\" --watchAll=false --ci --coverage --testMatch **/tests/**/*.test.js cross-env JEST_TEST=false"
  },
...

2/ Execute

run: npm run test:ci
Nardone answered 14/2 at 11:13 Comment(0)
G
0

Restarting my pc helped solving the issue... here is my package.json file:-

{
  "name": "xyz",
  "scripts": {
    "lint": "npm run lint:js && npm run lint:style",
    "lint:eslint": "eslint --ignore-path .gitignore",
    "lint:eslint:fix": "eslint --ignore-path .gitignore --fix",
    "lint:stylelint": "stylelint",
    "lint:stylelint:fix": "stylelint --fix",
    "lint:js": "npm run lint:eslint ./core ./src-override",
    "lint:css": "npm run lint:stylelint ./**/*.scss",
    "test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
  },
  "lint-staged": {
    "**/*.scss": "npm run lint:stylelint -s",
    "**/*.js": "npm run lint:eslint -s",
    "**/*.{js,tsx}": "npm run lint:eslint -s"
  },
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "devDependencies": {
    "@babel/plugin-proposal-nullish-coalescing-operator": "^7.16.5",
    "@babel/plugin-proposal-optional-chaining": "^7.2.0",
    "@babel/plugin-transform-runtime": "^7.17.10",
    "@babel/preset-env": "^7.8.4",
    "@babel/preset-react": "^7.8.3",
    "@babel/preset-typescript": "^7.24.1",
    "@testing-library/dom": "^10.0.0",
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^11.2.7",
    "@types/jest": "^26.0.16",
    "@types/lodash": "^4.14.165",
    "@types/react": "^16.9.19",
    "@types/react-dom": "^16.9.24",
    "@types/react-router-dom": "^5.1.5",
    "@types/testing-library__jest-dom": "^6.0.0",
    "@typescript-eslint/eslint-plugin": "6.0",
    "@typescript-eslint/parser": "6.0",
    "babel-core": "^7.0.0-bridge.0",
    "babel-loader": "^8.2.2",
    "dot-properties": "^0.3.2",
    "eslint": "^9.1.1",
    "eslint-config-airbnb-base": "13.1.0",
    "eslint-plugin-import": "2.17.2",
    "eslint-plugin-react": "^7.18.3",
    "eslint-plugin-react-hooks": "^4.2.0",
    "fs-extra": "9.0.1",
    "growl": "1.9.2",
    "grunt": "^1.0.3",
    "grunt-cli": "^1.3.2",
    "grunt-contrib-clean": "^2.0.0",
    "grunt-contrib-concat": "^0.5.1",
    "grunt-contrib-copy": "^0.8.1",
    "grunt-contrib-handlebars": "~0.10.2",
    "grunt-contrib-htmlmin": "2.0.0",
    "grunt-contrib-requirejs": "^1.0.0",
    "grunt-contrib-sass": "^1.0.0",
    "grunt-contrib-uglify": "^4.0.0",
    "grunt-contrib-watch": "^1.1.0",
    "grunt-eslint": "^21.0.0",
    "grunt-purifycss": "^0.1.2",
    "grunt-shell": "^4.0.0",
    "grunt-shopify": "1.0.0",
    "grunt-stylelint": "0.11.0",
    "grunt-sync": "^0.8.2",
    "grunt-webpack": "^3.1.3",
    "handlebars": "^3.0.3",
    "html-react-parser": "^0.13.0",
    "husky": "3.0.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^26.6.3",
    "jest-junit": "^12.0.0",
    "json-loader": "^0.5.7",
    "lint-staged": "8.1.7",
    "load-grunt-tasks": "^4.0.0",
    "nock": "^13.0.5",
    "react-hammerjs": "1.0.1",
    "react-test-renderer": "^17.0.2",
    "rjs-build-analysis": "~0.0.3",
    "source-map-loader": "^0.2.4",
    "string.prototype.replaceall": "^1.0.6",
    "stylelint": "10.0.1",
    "stylelint-config-standard": "18.3.0",
    "stylelint-order": "3.0.0",
    "stylelint-scss": "3.6.1",
    "ts-jest": "^26.5.6",
    "ts-loader": "^6.2.1",
    "typescript": "^3.9.7",
    "uglify-js": "~2.3",
    "uglifyjs-webpack-plugin": "^2.2.0",
    "url-loader": "^4.1.1",
    "walk-sync": "^0.3.2",
    "webpack": "^4.47.0",
    "webpack-bundle-analyzer": "^4.4.2"
  },
  "dependencies": {
    "@babel/core": "^7.24.4",
    "@babel/plugin-proposal-class-properties": "^7.18.6",
    "@handlebars/allow-prototype-access": "^1.0.5",
    "@testing-library/react-hooks": "^3.7.0",
    "@testing-library/react-native": "^12.4.5",
    "axios": "^0.20.0",
    "css-loader": "^5.2.7",
    "jest-cli": "^19.0.2",
    "jest-junit": "^16.0.0",
    "react": "^16.12.0",
    "react-async-hook": "^3.6.1",
    "react-dom": "^16.14.0",
    "react-helmet": "^6.1.0",
    "react-helmet-async": "^1.3.0",
    "react-image-magnifiers": "^1.4.0",
    "react-query": "^2.23.0",
    "react-router-dom": "^5.2.0",
    "string.prototype.replaceall": "^1.0.10",
    "style-loader": "^2.0.0",
    "swiper": "^6.8.4"
  }
}

jest.config.js:-

module.exports = {
  // The root of your source code, typically /src
  // `<rootDir>` is a token Jest substitutes
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$':
      '<rootDir>/__mocks__/fileMock.js',
    '\\.(css|less)$': 'identity-obj-proxy',
  },
  preset: 'ts-jest/presets/js-with-ts',
  testEnvironment: 'jsdom',
  roots: ['<rootDir>'],
  bail: 0,
  verbose: true,
  coveragePathIgnorePatterns: ['<rootDir>/target/', '<rootDir>/node_modules/'],
  // Jest transformations -- this adds support for TypeScript
  // using ts-jest
  transform: {
    '^.+\\.tsx?$': 'ts-jest'
  },
  // reporters: ['<rootDir>/my-custom-reporter.js'],
  testPathIgnorePatterns: ['<rootDir>/target/', '<rootDir>/node_modules/'],
  // Runs special logic, such as cleaning up components
  // when using React Testing Library and adds special
  // extended assertions to Jest
  setupFilesAfterEnv: [
    '@testing-library/jest-dom/extend-expect',
    '<rootDir>/jestSetup.ts'
  ],
  // Test spec file resolution pattern
  // Matches parent folder `__tests__` and filename
  // should contain `test` or `spec`.
  testRegex: '(/__tests__/.*|(\\.|/))\\.test\\.tsx?$',
  // Module file extensions for importing
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
  // For bamboo reports
  reporters: ['default', ['jest-junit', { outputDirectory: 'target' }]],
  collectCoverage: true,
  coverageDirectory: 'target/coverage'
};

and tsconfig.json file:-

{
  "compilerOptions": {
    "jsx": "react",
    "module": "commonjs",
    "outDir": "./dist/",
    "removeComments": false,
    "sourceMap": true,
    "target": "es6",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "types": ["jest", "node"]
  },
  "include": [
    "./src/react/**/**/*"
  ]
}
Gateway answered 3/5 at 15:51 Comment(0)
T
0

i add this and work for me

 sourceExts: [...sourceExts, 'svg', 'jsx', 'js', 'ts', 'tsx', 'cjs', 'json', 'mjs', 'd.ts', 'types.ts'],
Thermomagnetic answered 21/5 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.