3

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 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.