dimanche 10 juillet 2016

How to use the beforeEach in node-tap?

Can someone provide an example on how to use the beforeEach? http://ift.tt/29O210z Ideally, an example of the promise version, but a callback version example would also be nice.

Here is a test I created which works fine:

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

Before I do anything, I want to persist existingTeam. After it's saved, I want to stub Team.create. After these two things, I want to start actually testing. I think it would be cleaner if instead of using a Promise.all or perhaps duplicating the test code, I could use beforeEach.

How would I convert this to use beforeEach? Or what is an example of its usage?

Aucun commentaire:

Enregistrer un commentaire