项目场景:

node环境下编写js库,处于规范性考虑,需要做单元测试,我选择了Jest

问题描述

我的js库需要访问数据库,因此操作都是异步的,而且各个测试单元有严格的先后执行顺序(比如,建表 > 插 > 改 > 删),而Jest的每个单元是独立的,并且默认下并行执行测试。

       为此,我查询了解决方案,官方手册和一些博文帖子都告诉我,在jest配置文件中(jest.config.js),设置testSequencer属性,对应的是一个自定义的js模块,它导出了一个按照路径字母的排序算法,如下:

官方手册给出的custom-sequencer.js

const Sequencer = require('@jest/test-sequencer').default;
class CustomSequencer extends Sequencer {
sort(tests) {
// Test structure information
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests = Array.from(tests);
return copyTests.sort((testA, testB) => (testA.path > testB.path ? 1 : -1));
}
}
module.exports = CustomSequencer;

相应的jest.config.js配置

/** @type {import('jest').Config} */
const config = {
testSequencer: 'path/to/custom-sequencer.js',
}; module. Exports = config;

我照做了,但我的单元依然随机地并行执行,或只是最初加载时在表面上排好了队,即便我设置maxConcurrency为1(A number limiting the number of tests that are allowed to run at the same time when using test.concurrent. Any test above this limit will be queued and executed once a slot is released.)。在有一些帖子的回答中,有人则认为,Jest难以支持指定顺序,或者要额外写不少代码才能实现Jest有序测试。


解决方案:

仅仅为jest指定排序算法是不够的,因为默认运行模式就是并行执行!

同时,应该将jest设置为串行运行,这样测试单元才会按照预期的顺序执行

你可以在运行时加上runInBand参数npm test -- --runInBand



可以看到,对于我的四个测试文件,.1.dbIns.tes.js等等,从1-4执行了

你也可以在package.json中为npm脚本添加这个参数:

"scripts": {
"test": "jest",
"test-inline": "jest \"--runInBand\"", //注意是 "--runInBand" 而不是 -- --runInBand,而且一定要转义双引号
"babel-build": "babel lib/sql.js -d dist"
},

或者在package.json中,设置maxWorkers为1:

( 官方手册:Specifies the maximum number of workers the worker-pool will spawn for running tests. In single run mode, this defaults to the number of the cores available on your machine minus one for the main thread)

简而言之,你可以认为maxWorkers=1时Jest为单线程

const config = {
reporters: [~
"default",
"jest-allure"
],
setupFilesAfterEnv: ["jest-allure/dist/setup"],
testRunner: "jest-jasmine2",
testSequencer: './jest.sequencer.js',
maxWorkers: 1,
}; module.exports = config;

官方给出的排序法,是按文件路径字母排的序,我个人不喜欢用a,b,aa,ab为文件当作前缀,看着很难受,没有数字来的直观,如果你和我一样,可以使用我改写的排序模块:

此外,你的测试文件需要按照上图我那样子命名:.1.xxx.test.js

const Sequencer = require('@jest/test-sequencer').default;

class CustomSequencer extends Sequencer {
/**
* Sort test to determine order of execution
* Sorting is applied after sharding
*/
sort(tests) {
// Test structure information
// https://github.com/facebook/jest/blob/6b8b1404a1d9254e7d5d90a8934087a9c9899dab/packages/jest-runner/src/types.ts#L17-L21
const copyTests = Array.from(tests);
return copyTests.sort((testA, testB) => (parseInt(testA.path.split('.').reverse()[3]) > parseInt(testB.path.split('.').reverse()[3]) ? 1 : -1));
}
} module.exports = CustomSequencer;

题外话

写完这篇文后,我在stackoverflow上看到了相同的诉求,并且有人给出了正确的解答。那叫一个后悔啊,我怎么没早点看到

最新文章

  1. 【Beta】Scrum07
  2. Hello, cnblogs !
  3. Xcode调试技巧(断点和重构)
  4. asp.net 页面如何将Eval中的时间显示为“yyyy-MM-dd ” 格式
  5. kettle(6.0)如何连接远程集群(CDH5.1)?
  6. [Voice communications] 让音乐响起来
  7. 在当前Server上找某某object,注意只需修改"要找的object"就可以使用
  8. 微软开源资源 NET Foundation Projects
  9. JAVA基础知识之网络编程——-基于NIO的非阻塞Socket通信
  10. poj 3635(bfs+优先队列)
  11. 批处理脚本修改hosts文件指定域名解析IP
  12. C#编写记事本(高仿)
  13. pat_1009
  14. WIN7 64位配置Oracle SQL Developer工具
  15. [1] Tornado Todo Day0
  16. (?m) 标记
  17. latex表格线的颜色设置(边框添加颜色)
  18. 【Qt for Android】OpenGL ES 绘制彩色立方体
  19. Kinetis学习笔记(一)——基于KSDK 2.0
  20. qtp中vb脚本,经典收藏

热门文章

  1. CSS布局秘籍(2)-6脉神剑
  2. 基于SqlSugar的开发框架循序渐进介绍(21)-- 在工作流列表页面中增加一些转义信息的输出,在后端进行内容转换
  3. 【网络】内网穿透方案&FRP内网穿透实战(基础版)
  4. mindxdl--common--k8s_utils.go
  5. Reversal
  6. 【Java并发006】使用层面:Lock锁机制全解析
  7. 算法5: LeetCode_单链表_两数相加
  8. HTTP Analyzer 伴侣,解决Probably another instance is already up
  9. 一行shell实现tree
  10. 【每日一题】【list转int数组】【Lambda的简化-方法引用】2022年1月15日-NC45 实现二叉树先序,中序和后序遍历