摩卡没有在承诺列表中注册'it'块

时间:2018-01-04 19:01:06

标签: promise async-await mocha

我正在尝试编写一个将对所有项目运行GET的测试。为此,我在before块中获取该列表,然后我希望每个项目都有一个it块。我试图通过将it块放在itemList.forEach中来实现此目的。但是,我怀疑这里的问题是块永远不会被注册为测试。如何根据需要运行此测试?

let token;
let itemList;

describe('GET items/:itemId with Admin', async () => {
  before(async () => {
    // NOTE: item.find({}) returns a promise of a list of all items
    itemList = await item.find({});
    console.log(item[0]._id) // this logs correctly!
    const res = await userLogin(admin);
    token = res.body.accessToken.toString();   
  });

  it('registers initial it test', () => {
    // This test passes and logs the statement
    console.log('first test registered')
    console.log(itemList.length) // successfully logs non-zero value
  })

  await itemList.forEach(async (item) => {
    it('respond with json with a item', () => {
    const itemId = item._id;
    return getItem(itemId, token)
      .then((response) => {
        assert.property(response.body, '_id');
      });
    });
  });
});

2 个答案:

答案 0 :(得分:0)

在每次before测试之前运行it设置。它不会立即运行,并且在您尝试迭代itemList之前绝对不会等待任何事情。我想你需要做任何一件事

describe('GET items/:itemId with Admin', async () => {
  let token;
  before(async() => {
    const res = await userLogin(admin);
    token = res.body.accessToken.toString();   
  });

  // a list of all items for which tests should be created
  const itemList = await item.find({});
  console.log(itemList.length) // successfully logs non-zero value

  for (const item of itemList) {
    it('responds with json for item '+item, () => {
    const itemId = item._id;
    return getItem(itemId, token).then((response) => {
      assert.property(response.body, '_id');
    });
  }
});

describe('GET items/:itemId with Admin', () => {
  let itemList;
  let token;
  before(async() => {
    [itemList, token] = await Promise.all([
      item.find({}),
      userLogin(admin).then(res => res.body.accessToken.toString())
    ]);
  });

  it('responds with json for every item', () => {
    return Promise.all(itemList.map(item => {
      const itemId = item._id;
      return getItem(itemId, token)
        .then((response) => {
          assert.property(response.body, '_id');
        });
      });
    }));
  });
});

答案 1 :(得分:-1)

这是我最终解决的问题。我最终在describe块中添加了一个新的before块。 before块会产生提供项目列表的承诺。顶层有一个it块,以便mocha首先注册测试。

describe('GET items/:itemId with Admin', async () => {
  before((done) => {
    Item.find({}).then(async (itemList) => {
      // create the admin user to get the items with
      await createUsers([admin]);
      const res = await userLogin(admin);
      const token = res.body.accessToken.toString();

      itemList.forEach((item, index) => {
        const itemId = item._id;
        describe(`get item number ${index}: _id: ${itemId}`, () => {
          it('responds with item id', () =>
            getItem(item, token)
              .expect(200)
              .then((response) => {
                assert.notProperty(response.body, 'error');
                assert.property(response.body, '_id');
                assert.equal(response.body._id, itemId);
              }));
        });
      });
      done();
    });
  });
  // If there is no it block here, it will not run the before block!
  it(`register the initial it`, () => {
    assert.equal('regression test!', 'regression test!');
  });
});