gpt4 book ai didi

node.js - 一起运行时 nodejs : Tests pass individually, 失败(mocha、sinon、aws-sdk)

转载 作者:行者123 更新时间:2023-12-05 03:43:39 24 4
gpt4 key购买 nike

我有以下测试:

  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

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

最佳答案

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)

关于node.js - 一起运行时 nodejs : Tests pass individually, 失败(mocha、sinon、aws-sdk),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66715598/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com