Well I'm writing some NPM module with typescript, but i'm not using Webpack to compile the scripts.
how should I configure jest to run properly with typescript files?
// test.spec.ts
import {calc} from './index'
test('shoud...', () => {
expect(calc()).toBeDefined()
})
// index.ts
import {calc} from './index'
const calc = (a, b) => {
return a + b
}
I got this error:
testMatch: /__tests__//*.js?(x),**/?(*.)+(spec|test).js?(x) - 0 matches testPathIgnorePatterns: /node_modules/ - 9 matches
Step 1: Install
npm i jest @types/jest ts-jest -D
Explanation:
Install jest framework (jest)
Install the types for jest (@types/jest)
Install the TypeScript preprocessor for jest (ts-jest) which allows jest to transpile TypeScript on the fly and have source-map support built in.
Save all of these to your dev dependencies (testing is almost always a npm dev-dependency)
Step 2: Configure Jest
Add the following jest.config.js file to the root of your project:
module.exports = {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
}
Explanation:
We always recommend having all TypeScript files in a src folder in your project.
We assume this is true and specify this using the roots option. The transform config just tells jest to use ts-jest for ts / tsx files. jest docs: https://jestjs.io/docs/en/configuration#transform-object-string-string
refernce: https://basarat.gitbooks.io/typescript/docs/testing/jest.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With