- 前言
- 什么是 Jest
- 测试意味着什么
- 我怎么知道要测试什么
- 测试块,断言和匹配器
- CLI 和配置
- 模拟
- 执行环境
- 生成报告
- jest-cli
- jest-config
- jest-haste-map
- jest-runner
- jest-environment-node
- jest-circus
- jest-runtime
- 总结

当上面复写全局方法和保存好 state 之后,会进入到真正执行 describe 的回调函数的逻辑里面,在 packages/jest-circus/src/run.ts 的 run 方法里面,这里使用 getState 方法把 describe 代码块取出来,然后使用 _runTestsForDescribeBlock 执行这个函数,然后进入到 _runTest 方法,然后使用 _callCircusHook 执行前后的钩子函数,使用 _callCircusTest 执行。
const run = async (): Promise<Circus.RunResult> => {
const { rootDescribeBlock } = getState();
await dispatch({ name: "run_start" });
await _runTestsForDescribeBlock(rootDescribeBlock);
await dispatch({ name: "run_finish" });
return makeRunResult(getState().rootDescribeBlock, getState().unhandledErrors);
};
const _runTest = async (test, parentSkipped) => {
// beforeEach
// test 函数块,testContext 作用域
await _callCircusTest(test, testContext);
// afterEach
};
这是钩子函数实现的核心位置,也是 Jest 功能的核心要素。
总结
最后附上 Jest 核心引擎的代码实现给有需要的同学,欢迎关注和交流:jest-tutorial
原文链接:点击这里