如何在 beforeAll() 钩子上分配值的测试中使用变量

How to use variables in tests that were assigned with values at beforeAll() hook

我想在 beforeAll() 钩子上给一堆变量赋值,然后在 test.each([]) 中使用它们,但我得到的只是未定义的,而不是实际值。

但是当我尝试在正常情况下访问相同的变量时 test(),事实证明,我的变量具有实际值并且不是未定义的。

有没有办法给变量赋值,然后在 test.each([]) 中使用它们?

let movieId: string;
let serialId: string;

describe('Video page tests', () => {

  beforeAll(async () => {
    movieId = await videos.CreateMovie();
    serialId = await videos.CreateSerial();
  });

test.each([
    ['Deletes movie', movieId],
    ['Deletes serial', serialId],
  ])('%s', async(caseTitle, entityId) => {
    reporter.description(`${caseTitle}`);

    await videos.DeleteContent(entityId); //entityId is undefined
  })

test('Deletes movie', async() => {
    reporter.description(`Deletes movie`);

    await videos.DeleteContent(movieId); //movieId is correct
  })

事实证明,我不能在 test.each 处使用变量,在 beforeAll 挂钩处分配的变量,所以我想出了一个解决问题的方法

let content: string[] = [];
describe('Video page tests', () => {

  beforeAll(async () => {
    content.push(await videos.CreateMovie());
    content.push(await videos.CreateSerial());
  });

test.each([
    ['Deletes movie', 0],
    ['Deletes serial', 1],
  ])('%s', async(caseTitle, entityId) => {
    reporter.description(`${caseTitle}`);

    await videos.DeleteContent(content[entityId]);
  })