Using Jest in bitbucket pipelines, it doesn't find the tests and hence failed with the following error :
npx jest
No tests found, exiting with code 1
Run with `--passWithNoTests` to exit with code 0
In /opt/atlassian/pipelines/agent/build
12 files checked.
testMatch: **/__tests__/**/*.[jt]s?(x), **/?(*.) (spec|test).[tj]s?(x) - 2 matches
testPathIgnorePatterns: /node_modules/, /build/ - 0 matches
testRegex: - 0 matches
Pattern: - 0 matches
Locally my tests run fine.
Project structure :
.
├── build
│ ├── coverage
│ └── js
└── src
├── account
│ ├── account.ts
│ └── account.test.ts
├── index.ts
└── index.test.ts
jest.config.js
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
"^. \\.(t|j)sx?$": "ts-jest",
},
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
coverageDirectory: "build/coverage",
testPathIgnorePatterns: ["/node_modules/", "/build/"],
};
Tests are launched using following npm script
"test": "jest --coverage",
Is it normal for Jest not to run the test it finds with testMatch
?
- Tried to switch from
testMatch
totestRegex
without success - Tried to add the path to src in the launch script (
npx jest ./src
)
CodePudding user response:
Well, the problem was that bitbucket runs the pipelines in a folder called
/opt/atlassian/pipelines/agent/build
My complete folder structure is then :
/opt/atlassian/pipelines/agent/build
├── build
│ ├── coverage
│ └── js
└── src
├── account
│ ├── account.ts
│ └── account.test.ts
├── index.ts
└── index.test.ts
Because of the build
in the testPathIgnorePatterns
, all the folder was ignored.
The solution was to remove the build
from testPathIgnorePatterns
and change a litle bit the testMatch (in my case a testRegex) :
testRegex: ".*/src/.*\\.test\\.(t|j)sx?$"
CodePudding user response:
It looks like Jest is not able to find your tests because of the testPathIgnorePatterns
configuration in your jest.config.js
file. The testPathIgnorePatterns
option specifies patterns to exclude paths from the search for test files. In your case, this means that any files in the src
directory will not be considered as tests, since that is the directory where your tests are located.
To fix this issue, you can either remove the testPathIgnorePatterns
option from your jest.config.js file, or you can update the testPathIgnorePatterns
option to exclude paths other than the src
directory. For example:
module.exports = {
// ...
testPathIgnorePatterns: ["/node_modules/", "/build/", "/lib/"],
};
This will prevent Jest from excluding the src
directory from the search for test files, and your tests should be discovered and run properly. You may also need to update the testMatch
option to specify the pattern that Jest should use to find your test files. For example:
module.exports = {
// ...
testMatch: ["**/__tests__/**/*.[jt]s?(x)", "**/?(*.) (spec|test).[tj]s?(x)"],
};
This will tell Jest to look for test files that match the specified pattern, which should include your test files in the src
directory.
I hope this helps!