Search code examples
typescriptjestjsts-jestbitbucket-pipelines

Jest does not find tests even if testMatch has matches


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 to testRegex without success
  • Tried to add the path to src in the launch script (npx jest ./src)

Solution

  • 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 were ignored.
    See the Jest doc about this : https://jestjs.io/docs/configuration#testpathignorepatterns-arraystring

    These pattern strings match against the full path. Use the string token to include the path to your project's root directory to prevent it from accidentally ignoring all of your files in different environments that may have different root directories. Example: ["/build/", "/node_modules/"].

    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?$"

    OR

    To add <rootDir> in each testPathIgnorePatterns elements

     testPathIgnorePatterns: ["<rootDir>/node_modules/", "<rootDir>/build/"],