Skip to content

[jest-transform]: add a new option to transformOptions to detect when jest runs in watch mode #11118

@ahnpnl

Description

@ahnpnl

🚀 Feature Proposal

A new option for transformOptions to tell transformers if Jest runs in watch mode.

Motivation

I have a scenario in ts-jest related to kulshekhar/ts-jest#943

  • First I have 2 files
// foo.ts
export function getWelcomeMessage(username: string): string {
  return `yolo ${username}`;
}
// foo.spec.ts
it(`should return a message for this username`, (): void => {
    const result = getWelcomeMessage(username);

    expect(result).toStrictEqual(`yolo C0ZEN`);
});
  • Run test in watch mode, the test passes successfully
  • Modify foo.ts to
// foo.ts
export function getWelcomeMessage(username: number): string {
  return `yolo ${username}`;
}
  • Now test is rerun, however test still passes because ts-jest doesn't know if Jest runs in watch mode to properly redo type check

Example

A new option for transformOptions can be named something like watchMode and ts-jest can use that.

Currently I'm using a workaround that I have a check process.argv.includes('--watch'), it works but not optimal.

Pitch

Allow transformers to perform certain logic depending on watch mode

cc @SimenB @thymikee

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions