Jest + Typescript minus TS-Jest

ts-jest is a popular package when you try to use typescript with jest.

But one issue that is bugging me recently is, the uncovered lines stat is not correct, my test coverage dropped by 10+%.

After googling, nothing useful come up, and after some tro…


This content originally appeared on DEV Community and was authored by Acid Coder

ts-jest is a popular package when you try to use typescript with jest.

But one issue that is bugging me recently is, the uncovered lines stat is not correct, my test coverage dropped by 10+%.

After googling, nothing useful come up, and after some troubleshooting, I found out that it was ts-jest that causing the issue

so I decided to use babel for compilation instead

install dependencies

npm i -D @babel/preset-env @babel/preset-typescript typescript jest

setup npm script

"scripts": {
  "test": "jest",
}

create a jest.config.js

/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */

module.exports = {
    testEnvironment: 'node',
    roots: ['<rootDir>/src'],
    testMatch: ['**/__tests__/**/*.+(ts|js)', '**/?(*.)+(spec|test).+(ts|js)'],
    transform: {
        '^.+\\.(js|ts)$': 'babel-jest',
    },
    moduleDirectories: ['node_modules', 'src'],
    collectCoverage: true,
    collectCoverageFrom: ['**/*.{js,ts}', '!**/*.d.ts'],
}

create a babel.config.js

module.exports = {
    presets: [
        '@babel/preset-typescript',
        [
            '@babel/preset-env',
            {
                targets: {
                    node: 'current',
                },
            },
        ],
    ],
}

you don't need to install babel-jest, it comes with jest

finally npm test and boom, your 100% test coverage is back.


This content originally appeared on DEV Community and was authored by Acid Coder


Print Share Comment Cite Upload Translate Updates
APA

Acid Coder | Sciencx (2022-05-12T01:02:14+00:00) Jest + Typescript minus TS-Jest. Retrieved from https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/

MLA
" » Jest + Typescript minus TS-Jest." Acid Coder | Sciencx - Thursday May 12, 2022, https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/
HARVARD
Acid Coder | Sciencx Thursday May 12, 2022 » Jest + Typescript minus TS-Jest., viewed ,<https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/>
VANCOUVER
Acid Coder | Sciencx - » Jest + Typescript minus TS-Jest. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/
CHICAGO
" » Jest + Typescript minus TS-Jest." Acid Coder | Sciencx - Accessed . https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/
IEEE
" » Jest + Typescript minus TS-Jest." Acid Coder | Sciencx [Online]. Available: https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/. [Accessed: ]
rf:citation
» Jest + Typescript minus TS-Jest | Acid Coder | Sciencx | https://www.scien.cx/2022/05/12/jest-typescript-minus-ts-jest/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.