Skip to main content
Version: ACE 5

How to unit test step

To make sure that all of the bugs that are reported are gone for good, it is important to create unit tests, that repeat what was received in the bug report.

Unit test must receive as an input step configuration and input doc state and check if output doc state matches expected doc state from the bug report.

For example here is the unit test, to make sure that doc properties in mongo step payload are properly expanded (from {{_id}} to the actual value of _id in the doc).

import sinon from 'sinon';
import { expect } from 'chai';
import * as mongo from 'mongodb';
import { stepContainer } from '../../../services/stepContainer';
import { Steps, StepMongoDbFlowConfig } from '../../../constants/index';
import { StepExecuter } from '../baseStepExecuter';

describe('Mongo step', function () {
it('inserts values of document as part of the payload', async function () {
// Obtain step execution engine
const executer = stepContainer.resolve<StepExecuter>(Steps.MONGODB);

// Input document from a bug report
const doc = {
_id: '100',
};

// Step configuration from a bug report
const config = {
connection: 'mongodb://localhost',
database: 'test',
config: {
actions: [
{
action: 'insert',
payload: [
{
_id: '{{_id}}',
},
],
},
],
},
} as StepMongoDbFlowConfig;

// Expected output from a bug report
const expected = {
_id: '100',
};

// Stubbed operation
const db = {
collection: (collection) => {
return {
insertMany: (payload) => {
expect(payload).to.deep.equal([expected]);
},
} as mongo.Collection;
},
} as mongo.Db;

// Stub connect, so it is not failing
sinon.stub(mongo.MongoClient.prototype, 'connect');

// Stub operation that has to be tested
sinon.stub(mongo.MongoClient.prototype, 'db').returns(db);

// Execute step
const result = await executer.executeStep(doc, config, 'test');

// check output doc
expect(result.doc).to.deep.equal(expected);

// check if there are no errors
expect(result.errors.errors).to.deep.equal([]);
});
});