简体   繁体   English

尝试用玩笑生成动态 test.each

[英]Trying to generate dynamique test.each with jest

I have made a simplified version of my issue to illustrate, here is the code:我已经制作了我的问题的简化版本来说明,这里是代码:

class mock {
  constructor(a, b, c) {
    this.a = a;
    this.b = b;
    this.c = c;
  }
  getFunNames() {
    return ["funNameA", "funNameB", "funNameC"];
  }

  funNameA() {
    return this.a;
  }
  funNameB() {
    return this.b;
  }
  funNameC() {
    return this.c;
  }
}
var value = new mock(1, 2, 3);

var funnames = value.getFunNames();
var succeeded = 0;
var awaited = 0;
for (let i in funnames) {
  awaited += 1;
}

for (let i in funnames) {
  test("Empty test for " + funnames[i], () => {
    expect(() => {
      value[funnames[i]]();
      succeeded++;
    }).not.toThrowError();
  });
}

test("test all tests have passed", () => {
  expect(awaited).toBe(succeeded);
});

This is working fine.这工作正常。 However, in my real code situation, I have a random fatal failure that happen once in a while, non predictably, right where I create an object of the class that "mock" represent.但是,在我的真实代码情况下,我偶尔会发生一次随机致命故障,这是不可预测的,就在我创建“模拟”代表的 class 的 object 的地方。

So I need to enclose it in a test, in order to keep an eye on whatever log jest could get from this fatal failure.所以我需要将它包含在一个测试中,以便密切关注从这个致命失败中得到的任何日志笑话。

So basically, I need to set "value" from inside a test, then, prepare tests from this value outside for generating new tests.所以基本上,我需要从测试内部设置“值”,然后从外部的这个值准备测试以生成新的测试。 I know about this documentation on how to customize test steps jest documentation: Setup and Teardown but, since I have to get some result from value, in order to prepares the tests, I've not been able to use test.each, which seem to never get access to whatever value I want to add in the variable I give it, since I do it after the code Have been launched.我知道这个关于如何自定义测试步骤的文档开玩笑的文档:Setup and Teardown但是,因为我必须从值中得到一些结果,为了准备测试,我无法使用 test.each,这似乎永远不要访问我想在我给它的变量中添加的任何值,因为我是在代码启动后执行的。 Also, starting when I set "value" from a test, I can not access value content outside others test, I can in any test, but not from outside test.另外,从我从测试中设置“值”开始,我无法在其他测试之外访问值内容,我可以在任何测试中,但不能从外部测试。

So, how can I test setting value, then, accede this value outside test to prepare my next tests, and then, do whatever tests I have left?那么,我怎样才能测试设置值,然后,在测试之外接受这个值来准备我的下一个测试,然后,做我剩下的任何测试? I have tried a lot of thing but nothing has worked so far.我已经尝试了很多东西,但到目前为止没有任何效果。 I don't know what I am missing.我不知道我错过了什么。

I finally preferred to generate the data I need for my tests outside them, to not stay locked on this issue for days.我最终更喜欢在它们之外生成我的测试所需的数据,而不是在这个问题上停留几天。 I generated a file that I will update every time I push, in wish I have the full matrix to use test.each, it may be not the best solution, but it will be enough for me for now...我生成了一个文件,每次推送时都会更新,希望我有完整的矩阵来使用 test.each,它可能不是最好的解决方案,但现在对我来说已经足够了......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jest test.each 的类型不正确 - Incorrect types for Jest test.each 开玩笑| test.each 表 | 如何使用 beforeAll function 中定义的变量配置 test.each 表 - Jest | test.each table | How to configure the test.each table with variables defined in beforeAll function 使用Jest的test.each参数化测试变量范围 - Using Jest's test.each parameterized test variable scope 如何在 test.each api of jest 中格式化测试标题 - How to format title of a test in the test.each api of jest 使用 jest 的 test.each 与使用 forEach 循环 - Using jest's test.each vs. looping with forEach 在玩笑中使用 test.each 时跳过一些测试 - Skip some tests when using test.each in jest 使用 async beforeAll 时 test.each 全局内未定义的 var - Jest 24 - undefined var inside test.each global when using async beforeAll - Jest 24 我可以从 test.each 标记的模板文字中为另一个上下文捕获测试吗? - Can I capture tests from test.each tagged template literals for another context? 使用 React 测试库在 `test.each` 中使用 fetchMock 和 `userEvent.click` 进行多次 fetch 调用 - Multiple fetch calls using fetchMock & `userEvent.click` within a `test.each` using React Testing Library 在每个测试文件之前进行Jest异步设置 - Jest async setup before each test file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM