jest cannot find tests when projects are defined in configuration

Created on 21 Jun 2018  路  15Comments  路  Source: facebook/jest

馃悰 Bug Report

Basically I got a bunch of files for my Electron app, some of them require a node environment, and some of them jsdom, because I need to test out some features of my React components, e.g componentDidMount methods, but I can't define different jest configs for my folders, as shown per documentation in jest as it resolves to no tests found.

To Reproduce

You can reproduce the error if you try to run the tests from https://github.com/marcus-sa/venobo/tree/ts

{
    "verbose": true,
    "moduleFileExtensions": [
      "js",
      "ts",
      "tsx",
      "json",
      "node"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "collectCoverage": true,
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "projects": [{
      "testEnvironment": "jsdom",
      "testMatch": [
        "**/*.test.tsx"
      ],
      "setupTestFrameworkScriptFile": "./setupTests.ts"
    }, {
      "testEnvironment": "node",
      "testMatch": [
        "**/*.test.ts"
      ]
    }]
  },

as you can see here

```No tests found
In /home/sentinel/Git/venobo
10 files checked.
testMatch: */.test.tsx - 0 matches
testPathIgnorePatterns: /node_modules/ - 10 matches
In /home/sentinel/Git/venobo
10 files checked.
testMatch: */.test.ts - 0 matches
testPathIgnorePatterns: /node_modules/ - 10 matches
Pattern: - 0 matches

but it works fine if I do this:

```json
"jest": {
    "testEnvironment": "node",
    "verbose": true,
    "moduleFileExtensions": [
      "js",
      "ts",
      "tsx",
      "json",
      "node"
    ],
    "testPathIgnorePatterns": [
      "/node_modules/"
    ],
    "collectCoverage": true,
    "transform": {
      "^.+\\.tsx?$": "ts-jest"
    },
    "setupTestFrameworkScriptFile": "./setupTests.ts",
    "testMatch": [
        "**/*.test.ts"
     ]
  },
  System:
    OS: Linux 4.13 Ubuntu 17.10 (Artful Aardvark)
    CPU: x64 AMD Ryzen 7 1800X Eight-Core Processor
  Binaries:
    Node: 9.11.1 - /usr/local/bin/node
    Yarn: 1.7.0 - ~/.yarn/bin/yarn
    npm: 5.6.0 - /usr/local/bin/npm
  npmPackages:
    @types/jest: ^23.1.0 => 23.1.0 
    jest: ^23.1.0 => 23.1.0 

Most helpful comment

After searching a while I found out that if you use:

{
    roots: ["<rootDir>/src/", "<rootDir>/__tests__/"],
}

but don't have one of those folders everything else is ignored too.

All 15 comments

I have the same problem as you. All jest versions 23.0.0, 23.1.0, 23.20 don't detect my tests.

The problem is that it doesn't recursively check all directories for tests in my project(s).
Before it would check 62 files, but using this configuration it only checks 10 files.
I'm unsure whether or not it's even the root directory it's checking or what else.
There should really be some better configuration documentation for this library.

I ended up working around it by using 2 separate jest configs and one as base.

"scripts": {
    "test": "npm run test:node && npm run test:jsdom",
    "test:node": "jest --config=config/jest.node.config.js",
    "test:jsdom": "jest --config=config/jest.jsdom.config.js"
}

jest.base.config.js

const path = require('path');

module.exports = {
    rootDir: path.join(__dirname, '..'),
    verbose: true,
    moduleFileExtensions: [
        'js',
        'ts',
        'tsx',
        'json',
        'node',
    ],
    testPathIgnorePatterns: [
        '/node_modules/',
    ],
    collectCoverage: true,
    transform: {
        '^.+\\.tsx?$': 'ts-jest',
    },
};

jest.jsdom.config.js

const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    testEnvironment: 'jsdom',
    testMatch: [
        '**/*.test.tsx'
    ],
    setupTestFrameworkScriptFile: './setupTests.ts',
};

jest.node.config.js

const baseConfig = require('./jest.base.config');

module.exports = {
    ...baseConfig,
    testEnvironment: 'node',
    testMatch: [
        '**/*.test.ts'
    ],
};

OFFT:
Also the testRegex & testMatch properties aren't getting resolved properly relatively to your rootDir aswell.
So. Many. Errors.

In my case jest checks recursively but still doesn't find any test files. The only difference between your and my config is that I use <rootDir> in the testMatch option

 testMatch: [
    "<rootDir>/src/**/?(*.)(itest).{js,jsx,mjs}"
  ],

Deleting it doesn't change anything.

Maybe it's somehow connected with #6546

Yes, this is still an issue for me too. Right now, projects don't seem to be useful. I can run a command like:

16:35:52 api-server$ npx jest --projects jest/projects/api-service.js 
No tests found
In /Users/stuart/git/api-server/jest/projects
  1 file checked.
  testMatch: /Users/stuart/git/api-server/__tests__/**/*.js - 0 matches
  testPathIgnorePatterns: /node_modules/ - 1 match
Pattern:  - 0 matches

When /Users/stuart/git/api-server/__tests__ definitely does contain a whole bunch of *.js files.

More documentation might help, but I'm sure there's something else skewed too.

When I run test with jest I get an error that no test is found both my tests and js files are inside the static folder

__Here is my package json__

{
  "name": "static",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "directories": {
    "test": "tests"
  },
  "scripts": {
    "test": "jest"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^23.6.0"
  }
}

__Error__

No tests found
In /root/Desktop/Projects/iReporter/UI/static
  15 files checked.
  testMatch: **/__tests__/**/*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches
  testPathIgnorePatterns: /node_modules/ - 15 matches
Pattern:  - 0 matches
npm ERR! Test failed.  See above for more details.

What could be the issue

Any updates on this?

@y4nnick I did manage to get this set up eventually. It was a royal pain though. My main Jest config looks like:

module.exports = {
  projects: [
    '<rootDir>/packages/api-service',
    '<rootDir>/packages/user-service',
    '<rootDir>/packages/web',
    '<rootDir>/packages/admin',
  ],
};

And within each package subdirectory I have a Jest config that looks more like:

module.exports = {
  "name": 'user-service',
  "displayName": 'user-service',
  "rootDir": './',
  "moduleFileExtensions": [
    "js",
    "json"
  ],
  "setupFilesAfterEnv": [
    "<rootDir>/__jest__/setup.js"
  ],
  "transform": {},
  "testRegex": "(/__tests__/.*|(\\.|/)(test))\\.js$",
  "coverageDirectory": "<rootDir>/../../coverage/user-service",
  "collectCoverageFrom": [
    "**/*.js"
  ],
  "coveragePathIgnorePatterns": [
    "/__jest__/",
    "jest.config.js"
  ]
};

I think it was the rootDir being different between top level and each package might have fooled me. Anyway, I got it working, more or less. npx jest works, as does npx jest --projects packages/xxxx. This was using Jest 24.1.0.

EDIT: I fixed it by adding a WORKDIR in my Dockerfile. Some other stuff in the root (added by codeship) seems to have interfered with Jest's regex matching pattern somehow.


I got this issue, but only in CI (inside a docker container with NODE_ENV=test and --ci tag)

Using CRA with TS. No idea why its not finding the test. They are clearly there

{
  rootDir: path.join(__dirname, '../..'),
  testMatch: [..., path.join(__dirname, '../../**/?(*.)+(spec|test).[tj]s?(x)')],
}

This fixed it for me. Issue seems to be that the recursion is relative to the rootDir path.

Depends how nested your configuration file is, but basically you just need to set your root up as far as you need to go.

After searching a while I found out that if you use:

{
    roots: ["<rootDir>/src/", "<rootDir>/__tests__/"],
}

but don't have one of those folders everything else is ignored too.

@Naxos84 It worked here, thanks!

After searching a while I found out that if you use:

{
    roots: ["<rootDir>/src/", "<rootDir>/__tests__/"],
}

but don't have one of those folders everything else is ignored too.

Many thanks, man!!!

I got it working by specifically defining rootDir per project

Example:

{
  "collectCoverage": true,
  "collectCoverageFrom": ["src/**/*.[jt]s?(x)"],
  "coverageDirectory": ".coverage",
  "verbose": true,
  "projects": [
    {
      "displayName": "Browser",
      "testEnvironment": "jsdom",
      "rootDir": "./",
      "testMatch": ["<rootDir>/tests/**/*.[jt]s"],
      "testURL": "http://localhost"
    },
    {
      "displayName": "Node.js",
      "testEnvironment": "node",
      "rootDir": "./",
      "testMatch": ["<rootDir>/tests/**/*.[jt]s"]
    }
  ]
}

I had the same issue, and after hours of suffering, I started from scratch by creating a brand new empty config file by doing:

yarn ts-jest config:init

That command created the most basic jest config file, like:

module.exports = { preset: 'ts-jest', testEnvironment: 'node', };

With that simple configuration, all started working.
And as I needed extra config for Enzyme, I just started adding my custom config, and just realized that things stopped working at adding the testMatch regex. Just taking it out, made all to work fine.

I'm in a react-native context, but I hope this helps. This is my jest.config.js:

module.exports = {
  preset: 'react-native',
  transform: {
    '^.+\\.js$': '<rootDir>/node_modules/react-native/jest/preprocessor.js',
    '^.+\\.tsx?$': 'ts-jest',
  },
  globals: {
    'ts-jest': {
      tsConfigFile: 'tsconfig.jest.json',
    },
  },
  moduleFileExtensions: [
    'ts',
    'tsx',
    'js',
    'jsx',
    'json',
    'node',
  ],
  setupFiles: [
    '<rootDir>/jest.setup.js',
  ],
  testEnvironment: 'node',
};

... and my tsconfig.jest.json file:

```
{
"extends": "./tsconfig",
"compilerOptions": {
"jsx": "react",
"module": "commonjs"
}
}

In My Case simply removed "testMatch" from package.json

Was this page helpful?
0 / 5 - 0 ratings