我在我的电子商务软件上使用 jest,所以我也可以在端到端测试时进行渲染测试(支持主题,这就是原因)。
但我目前只是在测试一些猫鼬功能,当我尝试保存文档时,它给了我这个错误:
Error: Timeout - Async callback was not invoked within the 10000ms timeout specified by jest.setTimeout.
我注意到当我删除带有await doc.save();
. 所以我认为它与async/await有关,但我找不到它。
包.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);
});
});
});
});