ts-jest icon indicating copy to clipboard operation
ts-jest copied to clipboard

[Bug]: ts-jest does not enforce typing when input is undefined or null

Open SReject opened this issue 2 years ago • 1 comments

Version

29.0.2

Steps to reproduce

Create a new node project,

Install related dependencies:

npm install -D typescript @types/node jest @types/jest ts-jest

Create the following files in the project root directory

tsconfig.json

{
    "compilerOptions": {
        "target": "ES2020",
        "module": "commonjs",
        "esModuleInterop": true,
        "lib": ["ES2020"]
    },
    "files":[
        "./example.ts"
    ],
    "exclude": [
        "node_modules/**/*"
    ],
}

jest.config.js

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    transform: {
        '\\.ts$': [
            'ts-jest',
            {
                tsconfig: "./tsconfig.json"
            }
        ]
    }
};

example.ts

interface IToken {
    value: string; // this value is required
}

export default class Token {
    public input: IToken;
    constructor(input: IToken) {
        if (input == null) {
            throw new Error();
        }
        this.input = input;
    }
}

example.spec.ts

import Token from './example';

test('Example', () => {
    expect(() => {

        // here is the issue:
        //   TSC (and thus IDEs like VSCode) reports the input as erroneous so `@ts-expect-error` is used
        //   But ts-jest doesn't see the input as erroneous so complains about the @ts-expect-error comment
        // @ts-expect-error: checking input as undefined
        new Token(undefined);
    }).toThrow();
});

Run jest and note the error

Expected behavior

ts-jest should enforce type checking for inputs that are explicitly undefined or null so that error suppression mechanism do not error as needless.

Actual behavior

ts-jest does not enforce type checking when inputs are explicitly undefined or null, thus error suppression mechanisms throw an error for being needlessly used.

Debug log

ts-jest.log

Additional context

This applies to nested values explicity set to undefined or null.

// This results in a type error from TSC but not from ts-jest
example({value: undefined});

Environment

System:
  OS: Windows 10 10.0.19044
  CPU: (12) x64 AMD Ryzen 5 1600 Six-Core Processor
Binaries:
  Node: 14.18.0 - D:\Programs\nodejs\node.EXE
  npm: 8.16.0 - D:\Programs\nodejs\npm.CMD
npmPackages:
  jest: ^29.0.3 => 29.0.3

SReject avatar Sep 25 '22 00:09 SReject

As a workaround to make sure both tooling and jest do not output errors for tests specific to passing in undefined and/or null, I've replaced the @ts-expect-error comments with:

/*eslint-disable-next-line @typescript-eslint/ban-ts-comment*/
// @ts-ignore: reason_message_for_ignoring_errors

This is not ideal as @ts-expect-error was specifically implemented for bypassing typing errors for test-suits. where as @ts-ignore ignores all compiler errors and is not recommended for use.

SReject avatar Sep 25 '22 01:09 SReject