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]: preset doesn't work (when collecting coverage) on Jest configs with multiple projects #3691

Closed
nevir opened this issue Jul 15, 2022 · 1 comment
Labels
Not An Issue Not ts-jest issue

Comments

@nevir
Copy link

nevir commented Jul 15, 2022

Version

28.0.6

Steps to reproduce

When using the ts-jest preset with a Jest configuration containing multiple projects, it doesn't behave quite as expected during coverage collection of untested files.

import { Config } from 'jest';

const config: Config = {
  preset: 'ts-jest',
  testEnvironment: 'node',

  collectCoverage: true,
  collectCoverageFrom: ['<rootDir>/{bin,src}/**'],

  projects: [
    {
      displayName: 'unit',
      testMatch: ['<rootDir>/src/**/__tests__/*'],
    },
    {
      displayName: 'integration',
      testMatch: ['<rootDir>/test/integration/**'],
    },
    {
      displayName: 'functional',
      testMatch: ['<rootDir>/test/functional/**'],
    },
  ],

  passWithNoTests: true,
};

export default config;

Expected behavior

Tests should run, and coverage should be generated.

Actual behavior

Files that are not tested instead run into syntax errors on TypeScript constructs (presumably because babel is processing them rather than ts-jest), and coverage fails to be collected.

Debug log

Running coverage on untested files...Failed to collect coverage from <project>/src/cli.ts
ERROR: <project>/src/cli.ts: Unexpected token, expected "," (1:24)

> 1 | export function run(argv: string[]) {
    |                         ^
  2 |   return argv;
  3 | }
  4 |
STACK: SyntaxError: <project>/src/cli.ts: Unexpected token, expected "," (1:24)

> 1 | export function run(argv: string[]) {
    |                         ^
  2 |   return argv;
  3 | }

Additional context

Note that I can work around this by explicitly including the ts-jest transform within each project:

import { Config } from 'jest';
import { defaults as tsJest } from 'ts-jest/presets';

const config: Config = {
  testEnvironment: 'node',

  collectCoverage: true,
  collectCoverageFrom: ['<rootDir>/{bin,src}/**'],

  projects: [
    {
      displayName: 'unit',
      testMatch: ['<rootDir>/src/**/__tests__/*'],
      transform: tsJest.transform,
    },
    {
      displayName: 'integration',
      testMatch: ['<rootDir>/test/integration/**'],
      transform: tsJest.transform,
    },
    {
      displayName: 'functional',
      testMatch: ['<rootDir>/test/functional/**'],
      transform: tsJest.transform,
    },
  ],

  passWithNoTests: true,
};

export default config;

Environment

System:
    OS: macOS 12.4
    CPU: (8) arm64 Apple M1 Pro
  Binaries:
    Node: 16.16.0 - ~/.volta/tools/image/node/16.16.0/bin/node
    Yarn: 3.2.1 - ~/.volta/tools/image/yarn/1.22.19/bin/yarn
    npm: 8.11.0 - ~/.volta/tools/image/node/16.16.0/bin/npm
@ahnpnl
Copy link
Collaborator

ahnpnl commented Jul 15, 2022

This is an issue for Jest. When you specify projects, Jest will overwrite the other config options and only a few options are not overwritten (I don't remember which one).

This is not something ts-jest can do. You need to ask Jest to support it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Not An Issue Not ts-jest issue
Projects
None yet
Development

No branches or pull requests

2 participants