I have tests that:
- PASS when run together in VS Code
- FAIL when run together on the CLI
- PASS when run individually (e.g.
mocha bogus.test.js -g "second")
Here's the code as bare bones as I can make it (and it's pretty darned bare bones):
const AWS = require("aws-sdk")
const SQS = new AWS.SQS()
exports.handler = () => {
return SQS.deleteMessage({foo: "bar"}).promise()
}
and the test:
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')
})
})
})
Results:
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
I would be DELIGHTED to find out that I'm doing something stupid...