javascript - Jest 测试函数绑定
问题描述
一个人怎么能绑定s.th。到 jest 执行的测试函数?
我想为不同的配置运行同一组测试两次,如下所示:
function wrap(title, fn) {
[1,2].forEach(x => {
this.hello = x
fn.bind(this)
fn() // works
test(title, fn) // does not work
})
}
wrap('test hello world', function() {
console.log('this.hello', this.hello)
});
但是this
是undefined
开玩笑的时候。
解决方案
原来 jest 提供了一个内置的助手来做到这一点:
describe.each([1,2])('test', x => {
test(`test ${x}`, function() {
console.log('x', x)
});
})
推荐阅读
- ios - 如何将 coredata 中的条目导出到 csv 文件中以通过电子邮件发送?
- java - 捕获并增强 LockedException 和 DisabledException
- java - 模块化 Spring Security SecurityWebFilterChain
- rest - 在共享上下文中通过 API 运行 Databricks 笔记本作业
- javascript - 如何将我的在线 pine 脚本转换为图表库的指标?
- r - 查找一个值是否大于向量中所有先前值的函数
- postgresql - Postgresql 等效于 SQL Server CONTEXT_INFO
- laravel-5 - 迁移中的数组到字符串转换错误
- java - 无法使用 Docker 容器名称连接到 MySQL,但可以连接到 localhost
- list - 在 Perl 中,为什么在语句中添加“|| die”会导致它在标量上下文中求值?