Get the current test/spec name in Jest

前端 未结 3 615
耶瑟儿~
耶瑟儿~ 2021-01-18 08:19

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 nam

3条回答
  •  耶瑟儿~
    2021-01-18 08:56

    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

提交回复
热议问题