【问题标题】:Typescript + Mocha/Chai - For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolvesTypescript + Mocha/Chai - 对于异步测试和钩子,确保调用“done()”;如果返回一个 Promise,确保它解析
【发布时间】:2020-11-19 19:27:02
【问题描述】:

我知道这种问题很老了。但是我已经尝试了所有方法,我无法解决这个问题。

错误是

Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

我已经尝试过设置较大的超时时间。但是这个解决方案也不起作用。

关注我的代码和访问 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');
        }
    }
}

我也可以分享我的 package.json 和 tsconfig.json。如果有人可以帮助我,我将不胜感激。

【问题讨论】:

    标签: node.js typescript mocha.js chai


    【解决方案1】:

    根据documentation,您需要返回一个promise,或者使用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 syntax:

    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');
      });
    });
    

    【讨论】:

    • 嗨!谢谢回复。这两种方法都试过了,但是错误是一样的。
    猜你喜欢
    • 2019-07-30
    • 2019-10-22
    • 2017-10-24
    • 2020-05-12
    • 1970-01-01
    • 2018-06-07
    • 2021-09-05
    • 1970-01-01
    • 2019-06-14
    相关资源
    最近更新 更多