鍍金池/ 問答/網(wǎng)絡(luò)安全  HTML/ 如何用jest測試ts文件

如何用jest測試ts文件

看到j(luò)est官網(wǎng)上的指示下載了ts-jest,可是不知道該怎么用,也沒有搜到清晰的教程,懇請(qǐng)大神幫忙

回答
編輯回答
司令

第一步安裝包:

yarn add -D typescript jest ts-jest @types/jest

第二步初始化項(xiàng)目相關(guān)配置文件:
生成tsconfig.json:

tsc --init

創(chuàng)建jest.config.js:

module.exports = {
  transform: {
    '^.+\\.tsx?$': 'ts-jest',
  },
  testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$',
  moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
}

目錄結(jié)構(gòu):

clipboard.png

base.spec.ts:

import { sampleFunction } from "../src";

test('adds 1 + 2 to equal 3', () => {
    expect(sampleFunction("hello")).toBe("hellohello");
});

結(jié)果:

clipboard.png

2017年10月22日 04:26