Node.js 在Sinon中重置存根历史失败
我想在每次测试后重置历史记录(调用次数)。在Webstorm IDE中运行良好,在NPM测试中失败告诉我 如果输入为非空,则每个“hook for”之前的“State Machine Trigger”将调用步骤函数处理程序:TypeError:stepFunctionHandler.startStepFunction.resetHistory不是函数 据我所知,我正在遵循sinon文档 下面是正在测试的代码:Node.js 在Sinon中重置存根历史失败,node.js,sinon,Node.js,Sinon,我想在每次测试后重置历史记录(调用次数)。在Webstorm IDE中运行良好,在NPM测试中失败告诉我 如果输入为非空,则每个“hook for”之前的“State Machine Trigger”将调用步骤函数处理程序:TypeError:stepFunctionHandler.startStepFunction.resetHistory不是函数 据我所知,我正在遵循sinon文档 下面是正在测试的代码: const config = require('config'); const { a
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);
});
});
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)
这是来自npm测试的堆栈跟踪
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);
});
});
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)
如果输入为非空,则每个“hook for”调用步骤函数处理程序之前的“State Machine Trigger”:
TypeError:stepFunctionHandler.startStepFunction.resetHistory不是函数
在上下文中。(functions/qeTools/stateMachineTrigger/test/unit/test.js:17:43)
在processImmediate(internal/timers.js:456:21)
在process.topLevelDomainCallback(domain.js:137:15)
您需要使用存根变量才能调用您已经定义的:
startStepFunctionMock
因此,请将您的前一项更改为:
beforeach(()=>{
startStepFunctionMock.resetHistory();
});