A Roadmap
  • Introduction
  • Roadmap
  • Basics
    • Basic Terminal Usage
      • Shell
        • 基本
        • 变量
        • 传递参数
        • 运算符
        • 命令
        • 流程控制
        • 函数
        • 输入输出重定向
    • Character Encoding
      • 字符编码
    • Data Structures & algorithms
    • Network
      • TCP/IP
      • Http/Https
        • 浏览器缓存
        • Status Code
        • GET & POST
        • HTTPS 握手过程
        • HostOnly Cookie
      • CDN
        • CDN 工作原理
    • Version Control
      • git message format
      • git commands
    • Principles
    • Design Patterns
    • Others
      • JSON
      • 正则表达式
  • Front End
    • Web Standard
      • Html
      • CSS
        • font-face 小记
        • Grid 布局简易笔记
      • Javascript
        • Ajax
          • Fetch
          • XMLHttpRequest
        • DOM
          • Selection
          • 常用 DOM 操作
          • script 标签的几个属性
        • Ecmascript
          • this 关键字
          • Event Loop
          • 隐式转换
          • Date
        • Event
          • 模拟事件
          • Others
    • Development
      • Package Management
        • npm
      • Modulization
        • 模块化机制
        • webpack 打包解析
      • Architecture
      • Build Tools
        • 如何编写一个自定义的 eslint 规则?
      • Debug
        • 移动端调试 web
      • Pre/Post Processors
      • Test
        • Jest Snapshot 使用指南
      • Type Checkers
        • TS 中 enum 的编译结果
    • Libs & frameworks
    • Platforms
      • Browser
        • Basics
          • 从输入 URL 到页面加载完成都发生了什么事情?
          • HTML 加载的过程示意图
          • 为什么读取某些属性,也会导致回流?
          • Chrome 进程模型
        • PWA
        • Compatible
        • Cross Origin
        • Performance
          • 性能检测:performance 对象
          • 性能指标
        • Others
          • 移动端 web 开发笔记
      • Server
      • Desktop Applications
      • Mobile Applications
        • Flutter 在移动端和 Web 端的技术实现
  • Back End
    • outline
    • Languages
      • Node
        • Event Loop
        • NodeJS 中的进程与线程
        • NodeJS 中的 esModule 与 commonJS
  • Clients
    • outline
    • 安装 IPA 包
  • DevOps
    • Languages
    • OS Concepts
    • Servers & terminal
  • UI & UX
    • outline
  • Others
    • Posts
      • 使用 node 爬取数据并导出到 excel
      • antd 1.x datepicker 时区问题
      • babel-transform-runtime 踩坑记录
      • lodash 按需加载注意事项
      • 记一次项目迁移的踩坑记录
      • 时区与JS中的Date对象
      • 记一次 vue + ts 开发踩坑
    • Tools
      • 个人常用的工具分享
      • tmux 简要笔记
Powered by GitBook
On this page
  • Intro
  • Usage
  • Tips
  • Common Issues
  • 如何让 jest 识别未编译的code?

Was this helpful?

  1. Front End
  2. Development
  3. Test

Jest Snapshot 使用指南

PreviousTestNextType Checkers

Last updated 5 years ago

Was this helpful?

Intro

Usage

  • 如何生成快照?

  • 如何测试?

    • toMatchSnapshot()

  • 怎么更新快照?

Tips

  1. snapshot 可以快速在已有项目中使用,方便后期的迭代

    (It) works well for us since our code base currently contains a large amount of code that is difficult to test for one reason or another.

    ​ 记住两点:

    • 初始化时,在测试用例中调用 toMatchSnapshot

    • 修改视图后,使用 jest -u 更新snapshot

  2. snapshot只是一个简化测试的辅助手段,而不能作为测试的替代品。 react/vue 组件单测,还需要配合 enzyme,编写简易的交互逻辑,不能全指望snapshot

  3. 缺点

    Like any other test, if the initial snapshot passes with bugs, or doesn’t capture the full range of cases, the test won’t adequately cover those behaviors.

    snapshot test和普通的测试没有区别,也存在 case 覆盖不全,导致快照对比不完整的情况。

Common Issues

如何让 jest 识别未编译的code?

我们编写的测试用例,大多数场景下都是直接引用的开发代码,即基于 esNext 编写的代码。如果不做额外的处理,测试引入的 jsx、ts、tsx文件,以及未经转义的 node_modules 包,都有可能让单测跑不起来。

{
    transform: {
        '^.+\\.(js|jsx|ts|tsx)$': 'babel-jest'
    },
}

但是,对于 node_modules 中某些包引用依赖,babel-jest默认会从相对目录读取配置,如果对应的包没有设置.babelrc,就会导致转义不成功。

// jest.transformer.js
const babelJest = require('babel-jest');
const options = require('./.babelrc.js');

// test 过程中,有可能引入 es 版本的 node_modules 代码,
// 而 babel-jest 默认会根据相对路径读取 babelrc。
// 所以使用自定义的 transformer, 手动注入 babel options, 避免上述问题
module.exports = babelJest.createTransformer(options);


// jest.config.js
{
    transform: {
        '^.+\\.[t|j]sx?$': './jest.transformer.js',
    },
}

所以需要用到 jest 提供的 ,同步编译测试过程中使用到的资源。官方已经为我们实现了一个插件,,可以直接读取本地的 .babelrc,可以做到和 webpack 同样的编译效果,使用方式如下:

因此可以使用 的方式来规避上述问题:

Testing with Jest Snapshots: First Impressions
官方文档
第一次测试时,会自动生成
仅在你修改了视图后才需要更新 jest -u
transform
babel-jest-plugin
自定义 transformer