Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
76 views
in Technique[技术] by (71.8m points)

javascript - 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.

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. 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.

question from:https://stackoverflow.com/questions/65644367/trying-to-generate-dynamique-test-each-with-jest

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...