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

[Bug]: Failed to collect coverage from. | 'rootDir' is expected to contain all source files. #4219

Open
nader-zouaoui opened this issue Oct 18, 2023 · 2 comments

Comments

@nader-zouaoui
Copy link

nader-zouaoui commented Oct 18, 2023

Version

29.1.0

Steps to reproduce

Have a monorepo with multiple typescript packages and nodejs apps
run testing with coverage set to true
jest config:

module.exports = {
  resetMocks: true,
  rootDir: "src",
  coverageDirectory: "../coverage",
  setupFilesAfterEnv: [
    ...defaultConfig.setupFilesAfterEnv,
    "<rootDir>/__test__/setupFile.ts",
  ],
  transformIgnorePatterns: ["<rootDir>/node_modules/(?!mongodb-memory-server)"],
  globalSetup: "<rootDir>/__test__/globalSetup.ts",
  globalTeardown: "<rootDir>/__test__/globalTeardown.ts",
  preset: "ts-jest",
  testEnvironment: "node",
  collectCoverage: true,
  coveragePathIgnorePatterns: ["__fixtures__", "__mocks__", "templates"],
  setupFilesAfterEnv: ["@testing-library/jest-dom"],
  collectCoverageFrom: ["<rootDir>/**/*.{js,ts}"],
  moduleFileExtensions: ["js", "json", "ts"],
  transform: {
    "^.+\\.ts$": "ts-jest",
  },
  coverageThreshold: null,
  moduleDirectories: ["node_modules", "<rootDir>"],
};

Expected behavior

Expected to return the complete coverage like so:

categories:test: STACK:
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------
categories:test: File                                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------
categories:test: All files                            |   81.31 |     60.4 |    77.5 |   84.33 |
categories:test:  src                                 |   59.18 |    54.71 |   44.44 |   65.85 |
categories:test:   init.testSetup.ts                  |   45.71 |    47.61 |    37.5 |   51.85 | 3-11,14-16,20-23
categories:test:   init.ts                            |   92.85 |    81.81 |     100 |   92.85 | 16
categories:test:  src/__test__/utils/memoryServer     |    87.8 |    77.14 |   83.33 |   90.32 |
categories:test:   utils.ts                           |    87.8 |    77.14 |   83.33 |   90.32 | 10-11,16
categories:test:  src/controllers/category            |   75.32 |    46.15 |   70.58 |   81.42 |
categories:test:   index.ts                           |   75.32 |    46.15 |   70.58 |   81.42 | 3-11,14-16,20-23
categories:test:  src/events                          |   85.71 |    85.71 |     100 |   85.71 |
categories:test:   listeners.ts                       |       0 |      100 |     100 |       0 | 2
categories:test:   producer.ts                        |     100 |    85.71 |     100 |     100 | 3
categories:test:  src/helpers                         |    92.5 |       68 |     100 |   91.89 |
categories:test:   getLayeredCategoriesAggregation.ts |     100 |      100 |     100 |     100 |
categories:test:   getPaginationAggregation.ts        |     100 |       80 |     100 |     100 | 3,17-36
categories:test:   populateChildren.ts                |   86.95 |       50 |     100 |   85.71 | 9,14,16
categories:test:  src/models                          |     100 |      100 |     100 |     100 |
categories:test:   Category.ts                        |     100 |      100 |     100 |     100 |
categories:test:  src/routes                          |     100 |    85.71 |     100 |     100 |
categories:test:   index.ts                           |     100 |    85.71 |     100 |     100 | 3
categories:test:  src/routes/category                 |   55.81 |       40 |   16.66 |   63.88 |
categories:test:   index.ts                           |   55.81 |       40 |   16.66 |   63.88 | 3-11,14-16,20-23
categories:test:  src/services/category               |    93.2 |     64.1 |     100 |   93.06 |
categories:test:   index.ts                           |    93.2 |     64.1 |     100 |   93.06 | 34,39,61,110,155,174,191
categories:test:  src/validators/sync/category        |   84.78 |    88.88 |    90.9 |   84.78 |
categories:test:   index.ts                           |   84.78 |    88.88 |    90.9 |   84.78 | 96-113
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------

Actual behavior

receive the following error:

categories:test: Failed to collect coverage from C:\Users\nader\Documents\devProjects\microservice-hub\apps\APIs\categories\src\index.ts
categories:test: ERROR: src/index.ts:1:20 - error TS6059: File 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/logger/index.ts' is not under 'rootDir' 'C:/Users/nader/Documents/devProjects/microservice-hub/apps/APIs/categories/src'. 'rootDir' is expected to contain all source files.
categories:test:   The file is in the program because:
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/apps/APIs/categories/src/index.ts' with packageId 'logger/[email protected]'
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/events/producer/index.ts' with packageId 'logger/[email protected]'
categories:test:     Root file specified for compilation
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/events/producer/index.ts' with packageId 'logger/[email protected]'
categories:test:
categories:test: 1 import logger from "logger";
categories:test:                      ~~~~~~~~
categories:test:
categories:test:   ../../../packages/node/events/producer/index.ts:9:20
categories:test:     9 import logger from "logger";
categories:test:                          ~~~~~~~~
categories:test:     File is included via import here.
categories:test:   ../../../packages/node/events/producer/index.ts:9:20
categories:test:     9 import logger from "logger";
categories:test:                          ~~~~~~~~
categories:test:     File is included via import here.

Debug log

$ dotenv -- turbo run test --concurrency=1 --filter=categories --force
╭────────────────────────────────────────────────────────────────────────╮
│                                                                        │
│                   Update available v1.9.3 ≫ v1.10.15                   │
│    Changelog: https://github.com/vercel/turbo/releases/tag/v1.10.15    │
│               Run "npx @turbo/codemod update" to update                │
│                                                                        │
│      Follow @turborepo for updates: https://twitter.com/turborepo      │
╰────────────────────────────────────────────────────────────────────────╯
• Packages in scope: categories
• Running test in 1 packages
• Remote caching enabled
categories:test: cache bypass, force executing 9e46dfab069524bf
categories:test: $ SET TS_JEST_LOG=ts-jest.log && jest --coverage --runInBand --detectOpenHandles --forceExit --passWithNoTests
categories:test: (node:14608) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
categories:test: (Use `node --trace-deprecation ...` to show where the warning was created)
categories:test: PASS src/routes/category/__test__/index.test.ts (13.231 s)
categories:test:   POST /category/
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (221 ms)
categories:test:       √ should throw a validation error (2) (36 ms)
categories:test:   POST /category/subCategory
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (85 ms)
categories:test:       √ should throw a validation error (2) (60 ms)
categories:test:   PUT /category/:id
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (61 ms)
categories:test:       √ should throw a validation error (2) (59 ms)
categories:test:   DELETE /category/:id
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (58 ms)
categories:test:   GET /category/bulk
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (22 ms)
categories:test:       √ should throw a validation error (2) (27 ms)
categories:test:   GET /unpaginated
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (20 ms)
categories:test:   GET /category/static-paths
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (46 ms)
categories:test:   GET /category/category-tree
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (87 ms)
categories:test:       √ should respond successfully (2) (86 ms)
categories:test:   GET /category/:id
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (51 ms)
categories:test:       √ should throw a not found error (2) (30 ms)
categories:test:   GET /category/
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (21 ms)
categories:test:       √ should respond successfully (2) (21 ms)
categories:test:       √ should throw a validation error (3) (28 ms)
categories:test:   GET /category/id-by-slug/:slug
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (49 ms)
categories:test:   GET /category/by-slug/:slug
categories:test:     validation tests
categories:test:       √ should respond successfully (1) (49 ms)
categories:test:     not found tests
categories:test:       √ should throw a Not Found Error (1) (58 ms)
categories:test:
categories:test: Failed to collect coverage from C:\Users\nader\Documents\devProjects\microservice-hub\apps\APIs\categories\src\index.ts
categories:test: ERROR: src/index.ts:1:20 - error TS6059: File 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/logger/index.ts' is not under 'rootDir' 'C:/Users/nader/Documents/devProjects/microservice-hub/apps/APIs/categories/src'. 'rootDir' is expected to contain all source files.
categories:test:   The file is in the program because:
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/apps/APIs/categories/src/index.ts' with packageId 'logger/[email protected]'
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/events/producer/index.ts' with packageId 'logger/[email protected]'
categories:test:     Root file specified for compilation
categories:test:     Imported via "logger" from file 'C:/Users/nader/Documents/devProjects/microservice-hub/packages/node/events/producer/index.ts' with packageId 'logger/[email protected]'
categories:test:
categories:test: 1 import logger from "logger";
categories:test:                      ~~~~~~~~
categories:test:
categories:test:   ../../../packages/node/events/producer/index.ts:9:20
categories:test:     9 import logger from "logger";
categories:test:                          ~~~~~~~~
categories:test:     File is included via import here.
categories:test:   ../../../packages/node/events/producer/index.ts:9:20
categories:test:     9 import logger from "logger";
categories:test:                          ~~~~~~~~
categories:test:     File is included via import here.
categories:test: STACK:
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------
categories:test: File                                 | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------
categories:test: All files                            |   81.31 |     60.4 |    77.5 |   84.33 |
categories:test:  src                                 |   59.18 |    54.71 |   44.44 |   65.85 |
categories:test:   init.testSetup.ts                  |   45.71 |    47.61 |    37.5 |   51.85 | 3-11,14-16,20-23
categories:test:   init.ts                            |   92.85 |    81.81 |     100 |   92.85 | 16
categories:test:  src/__test__/utils/memoryServer     |    87.8 |    77.14 |   83.33 |   90.32 |
categories:test:   utils.ts                           |    87.8 |    77.14 |   83.33 |   90.32 | 10-11,16
categories:test:  src/controllers/category            |   75.32 |    46.15 |   70.58 |   81.42 |
categories:test:   index.ts                           |   75.32 |    46.15 |   70.58 |   81.42 | 3-11,14-16,20-23
categories:test:  src/events                          |   85.71 |    85.71 |     100 |   85.71 |
categories:test:   listeners.ts                       |       0 |      100 |     100 |       0 | 2
categories:test:   producer.ts                        |     100 |    85.71 |     100 |     100 | 3
categories:test:  src/helpers                         |    92.5 |       68 |     100 |   91.89 |
categories:test:   getLayeredCategoriesAggregation.ts |     100 |      100 |     100 |     100 |
categories:test:   getPaginationAggregation.ts        |     100 |       80 |     100 |     100 | 3,17-36
categories:test:   populateChildren.ts                |   86.95 |       50 |     100 |   85.71 | 9,14,16
categories:test:  src/models                          |     100 |      100 |     100 |     100 |
categories:test:   Category.ts                        |     100 |      100 |     100 |     100 |
categories:test:  src/routes                          |     100 |    85.71 |     100 |     100 |
categories:test:   index.ts                           |     100 |    85.71 |     100 |     100 | 3
categories:test:  src/routes/category                 |   55.81 |       40 |   16.66 |   63.88 |
categories:test:   index.ts                           |   55.81 |       40 |   16.66 |   63.88 | 3-11,14-16,20-23
categories:test:  src/services/category               |    93.2 |     64.1 |     100 |   93.06 |
categories:test:   index.ts                           |    93.2 |     64.1 |     100 |   93.06 | 34,39,61,110,155,174,191
categories:test:  src/validators/sync/category        |   84.78 |    88.88 |    90.9 |   84.78 |
categories:test:   index.ts                           |   84.78 |    88.88 |    90.9 |   84.78 | 96-113
categories:test: -------------------------------------|---------|----------|---------|---------|--------------------------
categories:test: Test Suites: 1 passed, 1 total
categories:test: Tests:       21 passed, 21 total
categories:test: Snapshots:   0 total
categories:test: Time:        13.47 s, estimated 14 s
categories:test: Ran all test suites.

 Tasks:    1 successful, 1 total
Cached:    0 cached, 1 total
  Time:    21.573s

ts-jest.log

Additional context

No response

Environment

System:
    OS: Windows 10 10.0.22000
    CPU: (16) x64 Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
  Binaries:
    Node: 20.5.1 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.17 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.8.0 - C:\Program Files\nodejs\npm.CMD
  npmPackages:
    jest: 29.5.0 => 29.5.0
@dschmitt2
Copy link

I'm running into something similar, also with a Typescript monorepo:

3 import * as T from "./SchemaTypes";
                     ~~~~~~~~~~~~~~~
STACK: 
Failed to collect coverage from /var/home/me/application/packages/types/src/SchemaTypes.ts
ERROR: src/SchemaTypes.ts:1:29 - error TS6059: File '/var/home/me/application/packages/types/src/AccessLevel.ts' is not under 'rootDir' '/var/home/me/application/src'. 'rootDir' is expected to contain all source files.

jest.config.ts:

export default {
  preset: 'ts-jest',
  testEnvironment: 'node',
  collectCoverageFrom: [
    '<rootDir>/src/**/**.ts'
  ]
};

Strangely, the actual coverage report seems to be generated.

@dschmitt2
Copy link

I was able to make the errors go away by importing the index.ts file in my tests rather than importing directly via relative paths.

In other words changing from this form in my Types.test.ts file:

import * as T from "./SchemaTypes";

to this form (since my index.ts file exports everything from SchemaTypes):

import * as T from ".";

made the errors go away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants