gpt4 book ai didi

jestjs - 数据驱动的 Jestjs 测试

转载 作者:行者123 更新时间:2023-12-02 11:47:05 28 4
gpt4 key购买 nike

有没有办法用 Jestjs 编写更多数据驱动的测试?

我想出了这样的东西:

    it('assignments and declarations', () => {

testCases.forEach(function (testCase) {
const slx = fs.readFileSync(testDirectory + 'slx/' + testCase.slx, 'utf-8');

const document = compiler.parse(slx);

const lmsGenerator = new LMSGenerator(document);
const lms = lmsGenerator.generate();

const expected = fs.readFileSync(testDirectory + 'lms/' + testCase.lms, 'utf-8');

expect(expected).toBe(lms);
});
});

我从文件中读取输入和预期输出。文件(以及输入和输出之间的链接)保存在带有对象的数组中。问题是我丢失了多个 it() 函数的特定错误消息。

有没有办法做得更好?或者在预期调用失败的情况下使用单独的消息?

最佳答案

您为每个测试用例创建一个 it block ,并将它们捆绑在 describe block 中。现在,对于每个损坏的案例,您都会收到一条错误消息,并且测试在第一次失败后不会停止。

describe('assignments and declarations for', () => {
testCases.forEach(function (testCase) {
it(`case ${testCase}`, () => {
const slx = fs.readFileSync(testDirectory + 'slx/' + testCase.slx, 'utf-8');
const document = compiler.parse(slx);
const lmsGenerator = new LMSGenerator(document);
const lms = lmsGenerator.generate();
const expected = fs.readFileSync(testDirectory + 'lms/' + testCase.lms, 'utf-8');
expect(expected).toBe(lms);
});
});
});

关于jestjs - 数据驱动的 Jestjs 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40598985/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com