jest-runner-tsc 项目教程
1. 项目的目录结构及介绍
jest-runner-tsc/
├── .github/
│ └── workflows/
├── src/
│ ├── index.ts
│ └── ...
├── tests/
│ └── ...
├── .gitignore
├── jest.config.js
├── package.json
├── README.md
└── tsconfig.json
- .github/workflows/: 包含 GitHub Actions 的工作流配置文件。
- src/: 项目的源代码目录,包含 TypeScript 文件。
- tests/: 测试文件目录,包含项目的测试代码。
- .gitignore: 指定 Git 忽略的文件和目录。
- jest.config.js: Jest 测试框架的配置文件。
- package.json: 项目的依赖和脚本配置文件。
- README.md: 项目的说明文档。
- tsconfig.json: TypeScript 的配置文件。
2. 项目的启动文件介绍
项目的启动文件位于 src/index.ts
。该文件是项目的入口点,负责初始化项目并启动主要功能。
// src/index.ts
import { run } from './runner';
run();
- run(): 这是一个函数,负责执行项目的主要逻辑。
3. 项目的配置文件介绍
3.1 jest.config.js
Jest 的配置文件,用于配置测试框架的行为。
module.exports = {
runner: 'jest-runner-tsc',
displayName: 'tsc',
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testMatch: ['**/*.test.ts'],
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
};
- runner: 指定使用的 Jest 运行器。
- displayName: 测试套件的显示名称。
- moduleFileExtensions: 指定支持的文件扩展名。
- testMatch: 指定测试文件的匹配模式。
- transform: 指定如何转换 TypeScript 文件。
3.2 tsconfig.json
TypeScript 的配置文件,用于配置 TypeScript 编译器的行为。
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "**/*.spec.ts"]
}
- compilerOptions: 编译器选项,包括目标 ECMAScript 版本、模块系统等。
- include: 指定包含的文件或目录。
- exclude: 指定排除的文件或目录。
3.3 package.json
项目的依赖和脚本配置文件。
{
"name": "jest-runner-tsc",
"version": "1.0.0",
"scripts": {
"test": "jest",
"build": "tsc"
},
"dependencies": {
"typescript": "^4.0.0"
},
"devDependencies": {
"jest": "^26.0.0",
"ts-jest": "^26.0.0"
}
}
- scripts: 定义了项目的脚本命令,如
test
和build
。 - dependencies: 项目的生产依赖。
- devDependencies: 项目的开发依赖。