在Jest中获取当前的测试/规格名称 [英] Get the current test/spec name in Jest

查看:79
本文介绍了在Jest中获取当前的测试/规格名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从正在运行的测试中获取当前的规范名称?

基本上我想保存一个文件,例如.使用功能saveFile(),文件名称为规范名称.无需手动重新输入测试名称.

我发现唯一可行的方法是使用expect(),它在this中包含规范名称.做类似的事情

expect.extend({
  async toSaveFile(data) {
    fs.writeFileSync(`${this.currentTestName}.txt`, data)
    return { pass: true };
  },
});

允许然后做

expect().toSaveFile('contents of the file');

这绝对是一个hack,但这是我找到规范名称参考的唯一途径.还有this.testPath指示测试文件

Is there a way to get the current spec name from within the running test?

Basically I want to save a file, eg. using a function saveFile(), with the name of the file being the spec name. Without having to manually retype the name of the test.

解决方案

I found that the only possible way was through the use of expect(), which contains the spec name in its this. doing something like

expect.extend({
  async toSaveFile(data) {
    fs.writeFileSync(`${this.currentTestName}.txt`, data)
    return { pass: true };
  },
});

allows to then do

expect().toSaveFile('contents of the file');

it's definitely a hack, but it's the only way I could find to get a reference to the spec name. there is also this.testPath that indicates the test file

这篇关于在Jest中获取当前的测试/规格名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆