admin管理员组文章数量:1302318
I have a very large array of JSON objects. I need to run Jest tests on each individual element. I tried iterating through the array first and then write the tests in the loop as such:
describe("Tests", (f) => {
it("has all fields and they are valid", () => {
expect(f.portions! >= 0).toBeTruthy();
expect(f.name.length > 0 && typeof f.name === "string").toBeTruthy();
});
it("has an image", () => {
expect(f.image).toBeTruthy();
});
});
However with this code Jest plains that " Your test suite must contain at least one test."
Do I have to loop over this array for every single test I have?
I have a very large array of JSON objects. I need to run Jest tests on each individual element. I tried iterating through the array first and then write the tests in the loop as such:
describe("Tests", (f) => {
it("has all fields and they are valid", () => {
expect(f.portions! >= 0).toBeTruthy();
expect(f.name.length > 0 && typeof f.name === "string").toBeTruthy();
});
it("has an image", () => {
expect(f.image).toBeTruthy();
});
});
However with this code Jest plains that " Your test suite must contain at least one test."
Do I have to loop over this array for every single test I have?
Share edited Feb 13, 2023 at 8:21 Penny Liu 17.5k5 gold badges86 silver badges108 bronze badges asked Sep 16, 2021 at 19:32 SimoneMetaSimoneMeta 1001 silver badge8 bronze badges 1- Does this answer your question? Jest looping through dynamic test cases – Michael Freidgeim Commented May 11, 2023 at 1:23
1 Answer
Reset to default 8Jest does have describe.each
, test.each
and it.each
methods for your needs. It allows you to make same tests with different input/output.
https://jestjs.io/docs/api#describeeachtablename-fn-timeout
Examples :
With global describe.each :
const params = [
[true, false, false],
[true, true, true],
[false, true, false],
[false, false, true],
];
describe.each(params)('With params %s, %s, %s', (a, b, c) => {
it(`${a} === ${b} should be ${c}`, () => {
expect(a === b).toBe(c);
});
});
Output :
PASS test/integration-tests/test.spec.ts (5.938s)
With params true, false, false
√ true === false should be false (2ms)
With params true, true, true
√ true === true should be true
With params false, true, false
√ false === true should be false (1ms)
With params false, false, true
√ false === false should be true
Or with simple it.each :
const params = [
[true, false, false],
[true, true, true],
[false, true, false],
[false, false, true],
];
describe('Dumb test', () => {
it.each(params)('%s === %s should be %s', (a, b, c) => {
expect(a === b).toBe(c);
});
});
Output :
PASS test/integration-tests/test.spec.ts
Dumb test
√ true === false should be false (2ms)
√ true === true should be true
√ false === true should be false
√ false === false should be true
本文标签: javascriptLooping through an array and run a Jest test for each element does not workStack Overflow
版权声明:本文标题:javascript - Looping through an array and run a Jest test for each element does not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741687900a2392553.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论