我想在每次测试后重置历史记录(调用次数)。在Webstorm IDE中工作正常,在NPM测试中失败告诉我
状态机触发器“在每个之前”的钩子,用于“如果输入非空则调用步骤函数处理程序”:TypeError:stepFunctionHandler.startStepFunction.resetHistory不是函数
据我所知,我正在关注sinon文档。
这是测试中的代码:
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 };
这是测试
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);
});
});
这是来自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)
您需要使用存根变量才能调用已经定义的resetHistory()startStepFunctionMock
。
因此,将您的每项更改为:
beforeEach(() => {
startStepFunctionMock.resetHistory();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句