gpt4 book ai didi

jestjs - 开 Jest 异步测试 - expect.assertion 似乎是全局性的

转载 作者:行者123 更新时间:2023-12-04 15:58:18 25 4
gpt4 key购买 nike

我有以下单元测试:

var { Mockgoose } = require("mockgoose");
var mongoose = require("mongoose");
var Transaction = require("./transaction");

var mockgoose = new Mockgoose(mongoose);

describe("transaction", function() {
afterEach(function() {
return mockgoose.helper.reset();
});

afterAll(function() {
const { connections } = mongoose;
const { childProcess } = mockgoose.mongodHelper.mongoBin;
// kill mongod
childProcess.kill();
// close all connections
for (const con of connections) {
return con.close();
}
return mongoose.disconnect();
});

test("category is required", function() {
expect.assertions(1);
return mockgoose.prepareStorage().then(function() {
mongoose.connect("mongodb://foobar/baz");
return mongoose.connection.on("connected", function() {
var mockTransaction = new Transaction({
amount: 25,
comment: "Gas money, Petrol.",
tags: ["Gas", "Car", "Transport"],
currency: "EUR"
});
return mockTransaction.save(function(err, savedTransaction) {
expect(err.errors.category.properties.message).toBe(
"Category is required."
);
});
});
});
});

test("category should match one of the predefined categories", function() {
expect.assertions(1);
return mockgoose.prepareStorage().then(function() {
mongoose.connect("mongodb://foobar/baz");
return mongoose.connection.on("connected", function() {
var mockTransaction = new Transaction({
category: "dsawdsfawfsaf",
amount: 25,
comment: "Gas money, Petrol.",
tags: ["Gas", "Car", "Transport"],
currency: "EUR"
});
return mockTransaction.save(function(err, savedTransaction) {
expect(err.errors.category.properties.message).toBe(
"{VALUE} is not a valid category."
);
});
});
});
});
});

现在,如果我只运行其中一个测试,一切都会通过,但是当我运行这两个测试时,我会收到以下错误:

● transaction › category should match one of the predefined categories

expect.assertions(1)

Expected one assertion to be called but received two assertion calls.

43 |
44 | test("category should match one of the predefined categories", function() {
> 45 | expect.assertions(1);
| ^
46 | return mockgoose.prepareStorage().then(function() {
47 | mongoose.connect("mongodb://foobar/baz");
48 | return mongoose.connection.on("connected", function() {


每个测试不应该有自己的断言和自己的期望吗?

最佳答案

我在我的代码中发现,当我混合使用“异步”和“同步”测试来测试异步功能时,就会出现问题。

我使用导致问题的 jest expect().resolves/rejects API 以“同步方式”测试了异步函数。不得不以 async-await 风格重写测试。那解决了这个问题。

describe('asyncSum()', () => {
test('"broken" sync test', () => {
expect(asyncSum(2, 2)).resolves.toEqual(4); // This line causes the issue incorrect assertion count
})

test('async test resolves example', async () => {
expect.assertions(1);

const sum = await asyncSum(2, 2);
expect(sum).toEqual(4);
})

test('async test reject example', async () => {
expect.assertions(1);

let error;
try {
await asyncSum(2);
} catch (err) {
error = err;
}

expect(error.message).toEqual('Missing 2nd parameter')
}
})

关于jestjs - 开 Jest 异步测试 - expect.assertion 似乎是全局性的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51105594/

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