VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 初识 TypeScript

  • TypeScript 常用语法

  • ts-axios 项目初始化

  • ts-axios 基础功能实现

  • ts-axios 异常情况处理

  • ts-axios 接口扩展

  • ts-axios 拦截器实现

  • ts-axios 配置化实现

  • ts-axios 取消功能实现

  • ts-axios 更多功能实现

  • ts-axios 单元测试

    • Foreword
    • Jest Installation and Configuration
    • Helper Module Unit Tests
    • Request Module Unit Tests
    • Headers Module Unit Tests
    • Axios Instance Module Unit Tests
    • Interceptor Module Unit Tests
    • mergeConfig Module Unit Tests
    • Request Cancellation Module Unit Tests
    • Remaining Module Unit Tests
  • ts-axios 部署与发布

  • 《TypeScript 从零实现 axios》
  • ts-axios 单元测试
HuangYi
2020-01-05

Foreword

# 前言

单元测试是前端一个很重要的方向,鉴别一个开源库是否靠谱的一个标准是它的单元测试是否完善。有了完整的单元测试,未来你去重构现有代码或者是增加新的需求都会有十足的把握不出现 regression bug。

在前面的章节,我们已经编写完成 ts-axios 库的代码,并通过 demo 的形式简单地对一些功能做了验证,但是 demo 可以走到的代码分支,覆盖的场景都是极其有限的。为了用更科学的手段保证我们代码的可靠性,我们需要去编写单元测试,并尽可能达到 99% 以上的测试覆盖率。

这门课我们会使用开源测试框架 Jest (opens new window),它是 Facebook 出品的一个测试框架,相对其他测试框架,它的一大特点就是内置了常用的测试工具,比如自带断言、测试覆盖率工具,实现了开箱即用。

由于时间有限,我不会带大家一行行手敲测试代码,但我会把所有的知识点和测试代码都带大家过一遍,确保大家都能够学会。但是我希望你们在学习的过程中,能自己手敲这些测试代码,这样有助于你们学习和巩固。

通过这一章节的学习,我希望你们能够学会使用 Jest 去对 JS 库或者是 TS 库编写单元测试,并能把所学应用到你们的实际项目中。给自己的代码添加完整的测试代码也是一个非常好的开发习惯,虽然枯燥但十分实用,如果养成这些好习惯会有助于提升你的行业竞争力,所以希望大家虽然把代码实现了,也不要太骄傲,耐心把单元测试写好。

那么接下来就让我们开启单元测试之旅。

Edit (opens new window)
#TypeScript
Last Updated: 2026/03/21, 12:14:36
Static Method Extensions
Jest Installation and Configuration

← Static Method Extensions Jest Installation and Configuration→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode