3
votes

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

1

1 Answers

4
votes

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:

refernce: https://basarat.gitbooks.io/typescript/docs/testing/jest.html