node.js - 当源代码更改时,带有种子随机数的玩笑测试是不可预测的,并且测试失败
问题描述
我在这里创建了一个小的可重现测试用例:https ://github.com/opyate/jest-seedrandom-testcase
当我使用seedrandom时,我得到了可预测的随机性,正如测试所证明的那样(使用 一次又一次地运行它时npx jest
)。
但是,当我在测试中添加无关紧要的代码时,它会失败。
谁能阐明为什么会发生这种情况?更重要的是,如何配置jest
以便测试在更改时不会失败?
解决方案
我已经在https://github.com/opyate/jest-seedrandom-testcase更新了 repo,并给出了解释:
全局修补Math.random
不是正确的方法,因为 Node/Jest 内部可能会调用Math.random
并推进 RNG。
这可以通过取消注释为测试增加另一个期望的行来证明,因为它将 RNG 又推进了 9 次。
遗憾的是,被测代码不适合模拟/注入,但在正常情况下,建议Math.random
像这样模拟:
import seedrandom from "seedrandom"
import { expect, test, jest } from "@jest/globals"
beforeEach(() => {
const rng = seedrandom("hello.")
jest.spyOn(global.Math, 'random').mockImplementation(rng)
})
afterEach(() => {
jest.spyOn(global.Math, 'random').mockRestore()
})
推荐阅读
- php - 编辑配置文件laravel 7未定义变量中的错误密码更改
- go - 如何为多个环境和突变的组合生成配置
- python - Django 在 Django 模板中访问列表元素的属性
- android - 在 Flutter 中使用 android 10 活动缩放过渡
- microsoft-graph-api - 对于我的 Teams 应用在 App Studio 中收到的权限,我应该使用什么 API?
- java - 如何防止自定义视图覆盖自身?
- linux - 从命令行执行 Java Mission Control
- python - 我是 python 新手,我自己尝试将其作为个人项目。AttributeError:“NoneType”对象没有属性“get”
- python - 即使在 `try ... except` 中,asyncio.wait 也会返回异常
- xamarin - 如何在我的 xamarin 表单项目中使用 sqlite-net-pcl 设置外键