首页 > 解决方案 > nodejs:测试单独通过,一起运行时失败(mocha,sinon,aws-sdk)

问题描述

我有测试:

  1. 在 VS Code 中一起运行时通过
  2. 在 CLI 上一起运行时失败
  3. 单独运行时通过(例如mocha bogus.test.js -g "second"

这是我能做的最简单的代码(而且它是非常糟糕的骨头):

const AWS = require("aws-sdk")
const SQS = new AWS.SQS()

exports.handler = () => {
  return SQS.deleteMessage({foo: "bar"}).promise()
}

和测试:

const sinon = require("sinon")
const expect = require("chai").expect
const AWS = require("aws-sdk")

describe("Bogus Test", () => {
  let sandbox, deleteMessageStub

  beforeEach(() => {
    sandbox = sinon.createSandbox()

    deleteMessageStub = sandbox.fake.returns({ promise: () => Promise.resolve({}) })
    sandbox.stub(AWS, 'SQS').returns({
      deleteMessage: deleteMessageStub
    })  
  })

  afterEach(() => { sandbox.restore() })

  it("is the first test", () => {
    const bogus = require("../bogus")
    return bogus.handler().then(() => {
      expect(deleteMessageStub.callCount).to.equal(1, 'Should have called deleteMessage once')
    })
  })

  it("is the second test", () => {
    const bogus = require("../bogus")
    return bogus.handler().then(() => {
      expect(deleteMessageStub.callCount).to.equal(1, 'Should have called deleteMessage once')
    })
  })
})

结果:

Bogus Test
✓ is the first test
1) is the second test


1 passing (14ms)
1 failing

1) Bogus Test
is the second test:

Should have called deleteMessage once
+ expected - actual

-0
+1

at ... bogus.test.js:29:46

我会很高兴发现我正在做一些愚蠢的事情......

标签: node.jsmocha.jsaws-sdkchaisinon

解决方案


require一个模块多次,模块作用域的代码只会被执行一次,因为模块缓存在require.cache对象中。

这意味着./bogus模块仅加载一次,并require.cache在第二次期间从对象中获取require。该const SQS = new AWS.SQS();语句将只执行一次。

你通过·sinon.restore()·清除了stub的调用信息,所以第二个测试用例的·stub.callCount·是0

解决方法:清除./boguswithin beforeEach()hook的模块缓存

例如

bogus.js

const AWS = require('aws-sdk');
const SQS = new AWS.SQS();

exports.handler = () => {
  return SQS.deleteMessage({ foo: 'bar' }).promise();
};

bogus.test.js

const sinon = require('sinon');
const expect = require('chai').expect;
const AWS = require('aws-sdk');

describe('Bogus Test', () => {
  let sandbox, deleteMessageStub;

  beforeEach(() => {
    sandbox = sinon.createSandbox();

    deleteMessageStub = sandbox.fake.returns({ promise: () => Promise.resolve({}) });
    sandbox.stub(AWS, 'SQS').returns({
      deleteMessage: deleteMessageStub,
    });
    delete require.cache[require.resolve('./bogus')];
  });

  afterEach(() => {
    sandbox.restore();
  });

  it('is the first test', () => {
    const bogus = require('./bogus');
    return bogus.handler().then(() => {
      expect(deleteMessageStub.callCount).to.equal(1, 'Should have called deleteMessage once');
    });
  });

  it('is the second test', () => {
    const bogus = require('./bogus');
    return bogus.handler().then(() => {
      expect(deleteMessageStub.callCount).to.equal(1, 'Should have called deleteMessage once');
    });
  });
});

单元测试结果:

  Bogus Test
    ✓ is the first test
    ✓ is the second test


  2 passing (9ms)

推荐阅读