Search code examples
javascripttypescripttestingjestjsts-jest

No tests found, exiting with code 1, even if tests are found


I'm running jest command and got this output.

$ jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /Users/igormakowski/Documents/repositories/nestjs-template
  9 files checked.
  testMatch:  - 0 matches
  testPathIgnorePatterns: /Users/igormakowski/Documents/repositories/nestjs-template/node_modules/, /Users/igormakowski/Documents/repositories/nestjs-template/dist/, /Users/igormakowski/Documents/repositories/nestjs-template/.stryker-tmp/, .js - 0 matches
  testRegex: .*\.spec\.ts$ - 1 match
Pattern:  - 0 matches
error Command failed with exit code 1.

As you can see 1 test is found, but jest is not executing it anyway. I've got similar another project with pretty same config and everything works there.

jest.config.ts

import type { Config } from 'jest'

const configuration: Config = {
  moduleFileExtensions: ['js', 'json', 'ts'],
  rootDir: '.',
  testRegex: '.*\\.spec\\.ts$',
  // testMatch: ['<rootDir>/src/**/*.spec.ts'],
  transform: {
    '^.+\\.(t|j)s$': 'ts-jest',
  },
  verbose: true,
  collectCoverageFrom: ['**/*.(t|j)s'],
  testPathIgnorePatterns: [
    '<rootDir>/node_modules/',
    '<rootDir>/dist/',
    '<rootDir>/.stryker-tmp/',
    '.js',
  ],
  moduleNameMapper: {
    '^~/(.*)$': '<rootDir>/src/$1',
    '^@modules/app(.*)$': '<rootDir>/src/modules/$1',
    '^@common/filters(.*)$': '<rootDir>/src/common/$1',
  },
  coveragePathIgnorePatterns: [
    '.*\\.(interface|module|schema|entity|dto|enum|d).ts',
    '.*\\.e2e-spec.ts',
    'index.ts',
    'main.ts',
  ],
  coverageDirectory: './coverage',
  testEnvironment: 'node',
  roots: ['<rootDir>/src'],
}

export default configuration

My environment:

System:
    OS: macOS 13.2.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7820HQ CPU @ 2.90GHz
  Binaries:
    Node: 18.14.2 - ~/.nvm/versions/node/v18.14.2/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v18.14.2/bin/yarn
    npm: 9.5.0 - ~/.nvm/versions/node/v18.14.2/bin/npm
  npmPackages:
    jest: 29.5.0 => 29.5.0

Reproduction: https://github.com/Mnigos/nestjs-template

I tried newer version of jest(29.6.1): Tried to use testMatch instead of testRegex. Also tried to reinstall dependencies and reload vscode project and using jest flags like

jest --testMatch **/src/**/*.spec.ts
jest --config jest.config.ts
./node_modules/.bin/jest --config jest.config.ts

Solution

  • When run jest with custom configuration file, you need use option --config.

    jest --config jest.config.ts
    

    See more at here.

    Edit: You need remove '.js' in testPathIgnorePatterns.