Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest cannot find tests when projects are defined in configuration #6509

Closed
marcus-sa opened this issue Jun 21, 2018 · 22 comments
Closed

jest cannot find tests when projects are defined in configuration #6509

marcus-sa opened this issue Jun 21, 2018 · 22 comments
Labels

Comments

@marcus-sa
Copy link

marcus-sa commented Jun 21, 2018

🐛 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

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:

"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 
@niba
Copy link

niba commented Jun 26, 2018

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

@marcus-sa
Copy link
Author

marcus-sa commented Jun 26, 2018

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.

@niba
Copy link

niba commented Jun 27, 2018

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

@morungos
Copy link

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.

@LarryKarani
Copy link

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

@y4nnick
Copy link

y4nnick commented Apr 8, 2019

Any updates on this?

@morungos
Copy link

morungos commented Apr 8, 2019

@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.

@cyrus-za
Copy link

cyrus-za commented Jun 25, 2019

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

@djizco
Copy link

djizco commented Aug 18, 2019

{
  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.

@Naxos84
Copy link

Naxos84 commented Feb 26, 2020

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.

@jeangatto
Copy link

@Naxos84 It worked here, thanks!

@vooda--
Copy link

vooda-- commented Apr 16, 2020

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!!!

@politician
Copy link

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"]
    }
  ]
}

@gusrodriguez
Copy link

gusrodriguez commented Aug 28, 2020

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"
  }
}

@abduliOS
Copy link

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

@priyanka-naveen-github
Copy link

"rootDir": "./",

Thanks !! I was quite frustrated with issue, and it resolved it quite nicely...

@TheAzne
Copy link

TheAzne commented Feb 5, 2022

Where could I put this : roots: ["/src/", "/tests/"],

@ThisIsMissEm
Copy link

ThisIsMissEm commented Oct 28, 2022

I suspect this might be a bug in how jest resolves the config file location, as least in mono repos where you have:

├── packages
│   ├── foo
│   │   ├── ...
│   │   └── package.json
│   ├── bar
│   │   ├── ...
│   │   └── package.json
│  └── baz
│       ├── ...
│       └── package.json
├── jest.config.js
└── package.json

Where the root jest.config.js contains projects configured using an object, and the individual packages use just jest as their test command (or jest --selectProjects foo for package foo). Running npm exec -- jest --showConfig from the packages/foo directory, the config ends up being the default configuration for jest, instead of what is in ../../jest.config.js

I added some tracing into https://github.com/facebook/jest/blob/main/packages/jest-config/src/resolveConfigPath.ts and found that it is trying to recurse up to the parent directories, but it fails to actually recurse upwards due to https://github.com/facebook/jest/blob/main/packages/jest-config/src/resolveConfigPath.ts#L60 not being the parent directory.

Before and after changing that line to path().dirname(absolutePath), with logs on the arguments of resolveConfigPath and resolveConfigPathByTraversing, I get the following:

Before:

 $ npm exec -- jest --showConfig

Trying: /<project>/packages/foo
cwd: /<project>/packages/foo

Trying: /<project>/packages/foo
cwd: /<project>/packages/foo
initial: /<project>/packages/foo, parent: /<project>/packages

Trying: /<project>/packages/foo
cwd: /<project>/packages/foo

Trying: /<project>/packages/foo
cwd: /<project>/packages/foo
initial: /<project>/packages/foo, parent: /<project>/packages

Found: /<project>/packages/foo/package.json

After:

$ npm exec -- jest --showConfig

Trying: /<project>/packages/foo
cwd: /<project>/packages/foo

Trying: /<project>/packages
cwd: /<project>/packages/foo
initial: /<project>/packages/foo, parent: /<project>

Trying: /<project>
cwd: /<project>/packages/foo
initial: /<project>/packages/foo, parent: /

Found: /<project>/jest.config.js

As you can see, in the before the directory it tries to load configuration from doesn't change, despite attempting to recurse upwards. After the change, it correctly tries to load configuration from each of the parent directories.

Note: the package.json in /<project>/packages/foo/ does not include jest configuration (no jest key)

I'm going to try to write tests and a PR to fix the behaviour, though I'm not sure if this'll break things at all.

Edit: clarified the found jest.config.js filename, in my project I'm using typescript, so had accidentally left the .ts extension, but it's the same thing either way.

@ThisIsMissEm
Copy link

Just an update here, maybe I'm misunderstanding how Jest's projects setting is meant to work in mono-repos — i.e., the individual packages aren't meant to have a test script, and instead it's all done from the root package.

@github-actions
Copy link

This issue is stale because it has been open for 1 year with no activity. Remove stale label or comment or this will be closed in 30 days.

@github-actions github-actions bot added the Stale label Oct 28, 2023
Copy link

This issue was closed because it has been stalled for 30 days with no activity. Please open a new issue if the issue is still relevant, linking to this one.

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 27, 2023
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests