我在我的电子商务软件上使用了笑话,因此我也可以在端到端测试时进行渲染测试(支持主题,这就是为什么)。
但是我目前正在测试一些猫鼬功能,当我尝试保存文档时,它给了我这个错误:
Error: Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.
我注意到用await doc.save();
删除行时并没有出现该错误。
因此,我认为它与async / await有关,但我找不到它。
package.json
"jest": {
"automock": false,
"moduleDirectories": [
"node_modules"
],
"testPathIgnorePatterns": [
"node_modules",
".idea",
"public"
],
"collectCoverage": true,
"coverageThreshold": {
"global": {
"branches": 5,
"lines": 40,
"functions": 25,
"statements": -1000
}
},
"setupTestFrameworkScriptFile": "./setupJest.js",
"coverageDirectory": "coverage",
"globalSetup": "./globalSetupJest.js"
}
globalSetupJest.js
const Mongoose = require('mongoose').Mongoose;
const mongoose = new Mongoose();
const Mockgoose = require('mockgoose').Mockgoose;
const mockgoose = new Mockgoose(mongoose);
const {promisify} = require('util');
module.exports = async () => {
try {
await mockgoose.prepareStorage();
promisify(mongoose.connect);
await mongoose.connect('mongodb://localhost/test')
mongoose.connection.on('connected', () => {
console.log('db connection is now open');
});
} catch (e) {
console.log('error in setting up mockgoose', e);
}
};
Product.test.js
describe('Product', () => {
const index = require('./index');
const productSchema = require('./model');
const Product = mongoose.model(index.modelName, productSchema);
describe('Instance Functionality', () => {
let baseProductObj;
let localProduct;
beforeEach(async () => {
baseProductObj = {
ean: 123456789,
title: 'Test product',
description: 'Test description',
stock: {
amount: 5,
track: true
},
pricing: {
cost: 5,
price: 10
},
url: 'test-product'
};
localProduct = new Product(baseProductObj);
try {
return await localProduct.save();
} catch (e) {
console.log('error in beforeEach', e);
}
});
describe('.reduceStock', () => {
test('Should decrease the stock by a default of 1', async () => {
try {
localProduct.reduceStock();
await localProduct.save();
} catch (e) {
console.log('error in saving localProduct', e);
}
expect(localProduct.stock.amount).toEqual(4);
});
});
});
});
答案 0 :(得分:0)
您可以通过添加以下内容来指定测试中更大的超时时间:
{'base_score': 0.5, 'colsample_bylevel': 0.5, 'colsample_bytree': 0.7000000000000001, 'learning_rate': 0.2, 'max_depth': 10.0, 'min_child': 80.0, 'min_split_loss': 0.8, 'n_estimators': 500.0, 'norm': 1, 'norm_norm': 2, 'quant_distr': None, 'scale': 3, 'scale_pos_w': 3.1, 'subsample': 0.8}
答案 1 :(得分:0)
我已经有一段时间没有使用JEST了,但我认为您需要异步代码的“完成”回调。
您的代码应为:
test("Should decrease the stock by a default of 1", async (done) => {
try {
localProduct.reduceStock();
await localProduct.save();
done();
} catch (e) {
console.log("error in saving localProduct", e);
}
expect(localProduct.stock.amount).toEqual(4);
});
如果这样不起作用,请尝试退回文档。
await localProduct.save();
应该是
return await localProduct.save();