初始化项目 npm init -y
创建ts编译文件 tsc --init
{
"compilerOptions": {
"target": "ES2015",
"module": "ES2015",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"declaration":true,
"outDir": "./dist",
"lib":[
"ES5",
"DOM",
"ES2015.Promise"
]
},
"exclude": [
"./dist",
"./src/test",
"./node_modules",
"./jest.config.js"
]
}
编写打包命令
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc",
"jest": "jest",
"jest:coverage": "jest --coverage"
},
添加 单元测试
yarn add jest ts-jest @types/jest
初始化jest配置
yarn ts-jest config:init
测试
export const a=(a:number):number=>a
import { a } from '../index';
test('a函数', () => {
expect(a(1)).toBe(1);
});
运行 yarn jest && npm run jest
打包 yarn build && npm run build