如何在node-tap中使用beforeEach?

How to use the beforeEach in node-tap?

有人可以提供有关如何使用 beforeEach 的示例吗? http://www.node-tap.org/api/ 理想情况下,promise 版本的示例,但回调版本的示例也很好。

这是我创建的测试,效果很好:

'use strict';

const t = require('tap');
const tp = require('tapromise');
const app = require('../../../server/server');
const Team = app.models.Team;

t.test('crupdate', t => {
  t = tp(t);

  const existingId = '123';
  const existingData = {externalId: existingId, botId: 'b123'};
  const existingTeam = Team.create(existingData);

  return existingTeam.then(() => {
    stubCreate();

    const newId = 'not 123'
    const newData = {externalId: newId, whatever: 'value'};
    const newResult = Team.crupdate({externalId: newId}, newData);

    const existingResult = Team.crupdate({externalId: existingId}, existingData);

    return Promise.all([
      t.equal(newResult, newData, 'Creates new Team when the external ID is different'),
      t.match(existingResult, existingTeam, 'Finds existing Team when the external ID exists')
    ]);
  });
})
.then(() => {
  process.exit();
})
.catch(t.threw);


function stubCreate() {
  Team.create = data => Promise.resolve(data);
}

做任何事情之前,我都想坚持existingTeam。保存后,我想存根Team.create。这两件事之后,我想开始实际测试。我认为如果不使用 Promise.all 或复制测试代码,我可以使用 beforeEach.

会更干净

我如何将其转换为使用 beforeEach?或者它的用法示例是什么?

简单,只需return回调函数的承诺

const t = require('tap');
const tp = require('tapromise');
const app = require('../../../server/server');
const Team = app.models.Team;

const existingId = '123';
const existingData = {
  externalId: existingId,
  botId: 'b123'
};

t.beforeEach(() => {      
  return Team.create(existingData).then(() => stubCreate());
});

t.test('crupdate', t => {
  t = tp(t);

  const newId = 'not 123'
  const newData = {
    externalId: newId,
    whatever: 'value'
  };
  const newResult = Team.crupdate({
    externalId: newId
  }, newData);

  const existingResult = Team.crupdate({
    externalId: existingId
  }, existingData);

  return Promise.all([
    t.equal(newResult, newData, 'Creates new Team when the external ID is different'),
    t.match(existingResult, existingTeam, 'Finds existing Team when the external ID exists')
  ]);
}).then(() => {
  process.exit();
}).catch(t.threw);


function stubCreate() {
  Team.create = data => Promise.resolve(data);
}