I want to reset the history (number of invocations) after each test. Works fine in Webstorm IDE, fails in NPM test telling me
State Machine Trigger "before each" hook for "invokes the step function handler if input is non empty": TypeError: stepFunctionHandler.startStepFunction.resetHistory is not a function
From what I can tell, I'm following the sinon documentation.
Here's the code under test:
const config = require('config');
const { attempt } = require('@mystuff/utils');
const stepFunctionHandler = require('../../serviceHandlers/stepFunctionHandler');
const logger = require('../../../components/logger.js');
async function trigger(event) {
if (!event || !Object.keys(event).length) {
throw new Error('Invalid input. Expecting a list of normalized objects but input was empty.');
}
logger.info(`Input event: ${event}`);
const stateMachineARN = config.get('stateMachine.arn');
const [error, result] = await attempt(() =>
stepFunctionHandler.startStepFunction(stateMachineARN, event),
);
if (error) {
logger.error(`Invoking state machine failed ${error}`);
throw new Error(`Invoking state machine failed ${error}`)
}
return { result, statusCode: 200 };
}
module.exports = { trigger };
Here's the test
const assert = require('assert');
const sinon = require('sinon');
const stateMachineTriggerHandler = require('../../handler');
const stepFunctionHandler = require('../../../../serviceHandlers/stepFunctionHandler');
const event = { foo: 'bar' };
const startStepFunctionMock = sinon.stub(stepFunctionHandler, 'startStepFunction');
describe('State Machine Trigger', () => {
beforeEach(() => {
stepFunctionHandler.startStepFunction.resetHistory()
})
it('invokes the step function handler if input is non empty', async () => {
startStepFunctionMock.resolves({ state: 'success' });
await assert.doesNotReject(async () => {
await stateMachineTriggerHandler.trigger(event);
});
sinon.assert.calledOnce(stepFunctionHandler.startStepFunction);
});
it('throws an error if the step function invocation failed', async () => {
startStepFunctionMock.rejects(Error(`failed to start`));
await assert.rejects(async () => {
await stateMachineTriggerHandler.trigger(event);
}, (err) => {
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Invoking state machine failed Error: failed to start');
return true;
});
sinon.assert.calledOnce(stepFunctionHandler.startStepFunction);
});
it('throws an error if input is undefined', async () => {
await assert.rejects(async () => {
await stateMachineTriggerHandler.trigger();
}, (err) => {
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Invalid input. Expecting a list of normalized restrictions but input was empty.');
return true;
});
sinon.assert.notCalled(stepFunctionHandler.startStepFunction);
});
it('throws an error if input is empty', async () => {
await assert.rejects(async () => {
await stateMachineTriggerHandler.trigger({});
}, (err) => {
assert.strictEqual(err.name, 'Error');
assert.strictEqual(err.message, 'Invalid input. Expecting a list of normalized restrictions but input was empty.');
return true;
});
sinon.assert.notCalled(stepFunctionHandler.startStepFunction);
});
});
Here's the stack trace coming from npm test
State Machine Trigger "before each" hook for "invokes the step function handler if input is non empty":
TypeError: stepFunctionHandler.startStepFunction.resetHistory is not a function
at Context.<anonymous> (functions/qeTools/stateMachineTrigger/test/unit/test.js:17:43)
at processImmediate (internal/timers.js:456:21)
at process.topLevelDomainCallback (domain.js:137:15)
You need to use your stub variable in order to able to call resetHistory(), which you have already defined: startStepFunctionMock
.
So change your before each to:
beforeEach(() => {
startStepFunctionMock.resetHistory();
});