node.js - Typescript + Mocha/Chai - For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves
问题描述
I know that kind of problem is old. But I already tried everything, and I can't solve this problem.
The error is
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.
I already tried set a large timeout. But this solution does not work too.
Follow my code and the class who access mongo.
import { expect } from 'chai';
import { UserService } from "./../services/userService";
describe('Testar Usuario Service', () => {
describe('Método GET Teste', () => {
const userService = new UserService();
const users = userService.getTeste();
it('Deve retornar uma lista de nomes', () => {
expect(users).to.be.an('array');
});
});
describe('Method GET All Users', () => { //the error happen here
it('Deve retornar uma lista com todos os Usuários', (done) => {
const userService = new UserService();
return userService.getAllUsers().then(result => {
expect(result).to.be.an('array');
done();
}).catch((error) => {
done(error);
})
});
});
});
import { UserSchema } from '../models/userModel';
import * as mongoose from 'mongoose';
import {Request, Response} from "express";
const User = mongoose.model('User', UserSchema);
export class UserService {
public getTeste() {
const text = [{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }];
return text;
}
public async getAllUsers() {
try {
const users = await User.find({});
return users;
} catch (err) {
throw new Error('Erro de conexão');
}
}
public async insertUser(req: Request) {
const newUser = new User(req.body);
try {
await newUser.save();
} catch (err) {
throw new Error('Erro de conexão');
}
}
public async updateUser(req: Request) {
try {
const user = await User.findOneAndUpdate(
{cpf: req.body.cpf},
req.body,
{ new: true }
);
return user;
} catch (err) {
throw new Error('Erro de conexão');
}
}
public async getUser(req: Request) {
try {
const user = await User.findOne({cpf: req.params.cpf});
return user;
} catch (err) {
throw new Error('Erro de conexão');
}
}
public async deleteUser(req: Request) {
try {
const user = await User.findOneAndDelete({cpf: req.params.cpf});
return user;
} catch (err) {
throw new Error('Erro de conexão');
}
}
}
I can share my package.json and tsconfig.json too. If someone could help me, I'll appreciate.
解决方案
根据文档,您需要返回一个承诺或使用done()
回调。
所以要么删除返回:
describe('Method GET All Users', () => { //the error happen here
it('Deve retornar uma lista com todos os Usuários', (done) => {
const userService = new UserService();
userService.getAllUsers().then(result => {
expect(result).to.be.an('array');
done();
}).catch((error) => {
done(error);
})
});
});
或使用async/await 语法:
describe('Method GET All Users', () => { //the error happen here
it('Deve retornar uma lista com todos os Usuários', async () => {
const userService = new UserService();
const result = await userService.getAllUsers();
expect(result).to.be.an('array');
});
});
推荐阅读
- eclipse - Eclipse 格式化程序只是缩进
- java - 使用 appium 运行我的测试脚本时出现 SecurtyException
- python - 如何从字典中的值中获取 key_name?
- r - 重新发布图书时关闭更新提示
- android - 如何在laravel 5.7中使用retrofit send to api从android创建护照令牌,还有比护照更好的选择来进行身份验证
- tensorflow - TensorBoard 上(正确的)ANN 偏差和权重是什么样的?
- javascript - React Native Firebase:从firebase读取数据时出错
- javascript - How can I set value to componentDidMount from another function in React?
- c - 从 C 中获取 *ABS* 符号的值
- codenameone - 代号一透支