代码之家  ›  专栏  ›  技术社区  ›  iJade

Mocha单元测试Promise throws错误

  •  1
  • iJade  · 技术社区  · 6 年前

    我在试着对一个承诺进行单元测试。代码如下:

    it('it should return some 10 user data with ok status code when called with url ', (done) => {
        return user.getUsers('https://jsonplaceholder.typicode.com/users')
        .then((response) => {
            console.log('me here')
            assert.equal(JSON.parse(response).length, 10)
        })
        .catch((err)=>{
            console.log('me in error')
            assert.fail('err')
        })
    })
    

    运行时上述代码引发以下错误: Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves. (C:\Users\ajay\jay-workspace\UniTestModule\test\user.test.js)

    3 回复  |  直到 6 年前
        1
  •  3
  •   Estus Flask    6 年前

    done

    it('it should return some 10 user data with ok status code when called with url ', () => {
        return user.getUsers('https://jsonplaceholder.typicode.com/users')
        .then((response) => {
            console.log('me here')
            assert.equal(JSON.parse(response).length, 10)
        });
    })
    

    assert.fail

        2
  •  1
  •   Ilia    6 年前

    https://mochajs.org/#asynchronous-code

    it('it should return some 10 user data with ok status code when called with url ', (done) => {
    user.getUsers('https://jsonplaceholder.typicode.com/users')
    .then((response) => {
        console.log('me here')
        assert.equal(JSON.parse(response).length, 10);
        done();
    })
    .catch((err)=>{
        console.log('me in error')
        assert.fail('err');
        done(err);
    })
    
        3
  •  0
  •   Ahmed Alawady    6 年前

    --timeout ./node_module/.bin/mocha test/ --timeout=5000 this.timeout(5000)

    it('it should return some 10 user data with ok status code when called with url ', (done) => {
    user.getUsers('https://jsonplaceholder.typicode.com/users')
    .then((response) => {
        console.log('me here')
        assert.equal(JSON.parse(response).length, 10);
        this.timeout(5000);
        setTimeout(done, 3000);
    })
    

    https://mochajs.org/#test-level