如何在node-tap中使用beforeEach?

有人可以提供一个关于如何使用beforeEach吗? http://www.node-tap.org/api/理想情况下,承诺版本的一个例子,但callback版本的例子也不错。

这是我创build的一个testing工作正常的testing:

 '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 。 这两件事之后,我想开始真正的testing。 我认为如果不是使用Promise.all或者可能复制testing代码,我可以使用beforeEach来更清洁。

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

简单,只需从callback函数返回承诺

 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); }