nodejs:测试单独通过,当 运行 在一起时失败(mocha、sinon、aws-sdk)

nodejs: Tests pass individually, fail when run together (mocha, sinon, aws-sdk)

我有以下测试:

  1. 当 运行 在 VS Code 中一起时通过
  2. 在 CLI 上 运行 时失败
  3. PASS 当 运行 单独(例如 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

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

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

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

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

解决方法:清除beforeEach() hook

./bogus的模块缓存

例如

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)