@thanpolas
These are the tools I use and can talk about, tools can change, patterns remain the same...
const postgreService = require('./services/postgre.service');
const webService = require('./services/web.service');
const app = module.exports = {};
app.init = Promise.method(function () {
return Promise.all([
postgreService.init(),
// ... any other low-level / datastore service
])
.then(function () {
return Promise.all([
webService.init(),
// ... other high-level services
]);
});
});
// Determine if module was the execution entry point
const isStandAlone = require.main === module;
if (isStandAlone) {
app.init();
}
index.js
Application Boot
Standalone Test
Ignition
- /test/
|-- asserts/
|-- e2e/
|-- fixtures/
|-- lib/
|-- unit/
Perform standardized tests on your models
Tests Directory Structure
Tests Directory Structure
const chai = require('chai');
const expect = chai.expect;
const itemTests = module.exports = {};
itemTests.runAll = function (item) {
itemTests.testProperties(item);
itemTests.testTypes(item);
itemTests.testValues(item);
};
Part 1/2
Tests Directory Structure
itemTests.testProperties = function (item) {
expect(item).to.be.an('object');
expect(item).to.have.keys([
'id',
'name',
]);
};
itemTests.testTypes = function (item) {
expect(item.id).to.be.a('string');
expect(item.name).to.be.a('string');
};
itemTests.testValues = function (item) {
expect(item.id.length).to.equal(12);
expect(item.name).to.match(/^[\w]{3,8}$/);
};
Part 2/2
Tests Directory Structure
Tests Directory Structure
test/fixtures/account.fix.js
const faker = require('faker');
const phoneFix = require('./phone-numbers.fix');
const accountFix = module.exports = {};
accountFix.minFields = () => ({
name: faker.name.firstName(),
phone_number: phoneFix.getUS(),
});
Tests Directory Structure
Contains master test boot "test.lib.js" (more next).
Tests Directory Structure
Tests Directory Structure
Tests Directory Structure
const axios = require('axios');
const accountFix = require('../fixtures/account.fix');
const accountLib = module.exports = {};
accountLib.setupOne = function() {
beforeEach(function () {
const accountData = accountFix.one();
return accountLib.create(accountData)
.then((accountRecord) => {
this.accountOne = accountRecord;
});
});
};
accountLib.create = function (accountData) {
return axios.post('/account/', accountData)
.then(function (res) {
return res.data;
});
}
Putting it all together
const expect = require('chai').expect;
const testLib = require('../lib/test.lib');
const accountFix = require('../fixtures/account.fix');
const accountLib = require('../lib/account.lib');
const accountAssert = require('../asserts/account.assert');
describe('Account Create', function () {
testLib.init();
describe('Nominal behaviors', function() {
it('Should create an account and get expected outcome', function () {
const accountData = accountFix.one();
return accountLib.create(accountData)
.then((accountRecord) => {
accountAssert.runAll(accountRecord);
});
});
});
});
Putting it all together
Important points:
Putting it all together
const expect = require('chai').expect;
const testLib = require('../lib/test.lib');
const accountLib = require('../lib/account.lib');
const eventsFix = require('../fix/events.fix');
const eventsLib = require('../lib/events.lib');
const eventsAssert = require('../asserts/events.assert');
describe('Create Event', function () {
testLib.init();
describe('Nominal behaviors', function() {
accountLib.setupOne();
it.only('Should create an account and get expected outcome', function () {
const eventData = eventFix.one();
return eventLib.create(this.accountOne.id, eventData)
.then((eventRecord) => {
eventAssert.runAll(eventRecord);
});
});
});
});
Putting it all together
More points:
Thank you
Questions?