【问题标题】:In Jest, is there any way to get a beforeAll/beforeEach block to run prior to assertions when using a function to generate describe/it blocks?在 Jest 中,当使用函数生成 describe/it 块时,有没有办法让 beforeAll/beforeEach 块在断言之前运行?
【发布时间】:2019-03-28 07:31:36
【问题描述】:

当使用函数生成测试文件中经常使用的describe/it 块时,父级describe 中存在的beforeAllbeforeEach 块将被忽略。

例如:

const repeatedTests = (num) => {
  // DOES NOT WORK - num is undefined
  describe(`Testing number ${num}`, () => {
    it('should exist', () => {
      expect(num).toBeDefined();
    });
    it('should be a number', () => {
      expect(num).not.toBeNaN();
    });
    it('should be less than 5', () => {
      expect(num).toBeLessThan(5);
    });
  });
};

describe.each([[1],[2],[3]])('Describe for %i', (num) => {
  let numForTesting;
  beforeAll(() => {
    numForTesting = num;
  });
  repeatedTests(numForTesting);
});

我理解为什么会出现这种情况 - repeatedTests 将立即运行,因为测试运行者注意的不是 describe/it

为了让它工作,我需要做这样的事情:

const repeatedTests = (num) => {
  describe(`Testing number ${num}`, () => {
    let numForTesting;
    beforeAll(() => {
      numForTesting = num;
    });
    it('should exist', () => {
      expect(numForTesting).toBeDefined();
    });
    it('should be a number', () => {
      expect(numForTesting).not.toBeNaN();
    });
    it('should be less than 5', () => {
      expect(numForTesting).toBeLessThan(5);
    });
  });
};

describe.each([[1],[2],[3]])('Describe for %i', (num) => {
  repeatedTests(num);
});

在我正在处理的特定测试套件中(比这复杂得多,以防不明显) - 这样做会使事情变得非常棘手,并且使重用生成器函数非常具有挑战性。

有没有办法让beforeAllbeforeEach 在函数内部生成的测试块之前运行,就像我原来的例子一样?

对于它的价值,在我上面的简单示例中设置 num 的等价物是 mount 使用 enzyme 的反应节点。

【问题讨论】:

  • 我的感觉是,让它正常工作的唯一方法是在调用生成器之前让 beforeAll 等效运行。但接下来的挑战是,如果有其他beforeEach 以更高的描述运行,在我们的情况下 - 这似乎会导致问题......也许那里有一个不同的错误,我应该调查......

标签: unit-testing jasmine tdd jestjs enzyme


【解决方案1】:

有没有办法让 beforeAll 或 beforeEach 在函数内部生成的测试块之前运行,就像我原来的例子一样?

由于你提到的原因,我的感觉是答案是否定的。

这是一种不同的方法,可以满足您的需要。它使用generateState 函数而不是开玩笑的钩子。

const repeatedTests = (generateState) => {

    const num = generateState();

    describe(`Testing number ${num}`, () => {
        it('should exist', () => {
            expect(num).toBeDefined();
        });
        it('should be a number', () => {
            expect(num).not.toBeNaN();
        });
        it('should be less than 5', () => {
            expect(num).toBeLessThan(5);
        });
    });
};

describe.each([[1], [2], [3]])('Describe for %i', (num) => {

    const generateState = () => {
        return num;
    }

    repeatedTests(generateState);
});

这是测试输出:

 PASS  ./index.test.js
  Describe for 1
    Testing number 1
      √ should exist (4ms)
      √ should be a number (1ms)
      √ should be less than 5
  Describe for 2
    Testing number 2
      √ should exist
      √ should be a number
      √ should be less than 5 (1ms)
  Describe for 3
    Testing number 3
      √ should exist
      √ should be a number (1ms)
      √ should be less than 5

【讨论】:

  • 不知道为什么这被否决了......似乎准确!我认为最终归结为“需要按顺序运行 - 不能有 beforeAll/beforeEach/etc。”
猜你喜欢
  • 2018-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
相关资源
最近更新 更多