node.js - nodejs:测试单独通过,一起运行时失败(mocha,sinon,aws-sdk)
问题描述
我有测试:
- 在 VS Code 中一起运行时通过
- 在 CLI 上一起运行时失败
- 单独运行时通过(例如
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.cache
在第二次期间从对象中获取require
。该const SQS = new AWS.SQS();
语句将只执行一次。
你通过·sinon.restore()·清除了stub的调用信息,所以第二个测试用例的·stub.callCount·是0
。
解决方法:清除./bogus
within 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)
推荐阅读
- python - 将 Python 字典列表传递给 Rust 函数 PyO3
- php - 模块视图文件在 Codeigniter 4 中显示无效
- ubuntu - 甚至可以确认Nginx的运行,但是浏览器中没有显示登陆页面
- c# - 捕获异步方法的异常
- winforms - iGrid 不应该一键跳转到 col
- java - 我想从 .dat 文件中创建一个数组
- javascript - 承诺 {
} - 异步函数返回等待 - angular - 如何在没有 oauth 但使用 angular 和 dotnet 自定义实现的情况下安全地发送 clientid 和 clientsecret
- sockets - 从字节数组中解码分隔帧
- css - 对 fontawesome i 标签不显示任何内容不起作用,但 .fas 类可以