Data driven Jestjs tests

FDe*_*off 2 jestjs

Is there a way to write more data driven tests with Jestjs?

I came up with something like this:

    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);
    });
});
Run Code Online (Sandbox Code Playgroud)

I read the input and the expected output out of files. The files (and the link between input and output) are saved in an array with objects. The problem is that I lost the specific errors messages I had with multiple it() functions.

Is there a way to do this better? Or to use an individual message for the case the expected-call will fail?

And*_*rle 5

it为每个测试用例创建一个块,并将它们捆绑在一个describe块中。现在,您将收到每个破损情况的错误消息,并且在第一次失败后测试不会停止。

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);
    });
  });
});
Run Code Online (Sandbox Code Playgroud)