在 Sinon 中重置存根历史记录失败

Resetting stub history fails in Sinon

我想在每次测试后重置历史记录(调用次数)。在 Webstorm IDE 中工作正常,在 NPM 测试中失败告诉我

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

据我所知,我正在关注 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();
});