承诺链并不会一直执行

时间:2018-11-28 05:07:53

标签: node.js jestjs

我正在尝试对管理用户的功能进行集成测试。它发出的调用是异步的,但在添加测试用户并检查详细信息之后,Jest似乎无法使用。

  it("runs lifecycle", async done => {
    // expect.assertions(1);
    function check(a, b, c) {
      return new Promise((resolve, reject) => {
        console.log(a, b, c);
        if (a != b) reject(c);
        console.error(a, b, c);
        resolve();
      });
    }
    await new Promise((resolve, reject) => {
      user.add(testUser).then(() => {
        user.get(testUser.id).then(out => {
          check(out, testUser, "adding").then(() => {
            user.update(testUser1).then(() => {
              user
                .get(testUser1.id)
                .then(out => check(out, testUser1, "update"))
                .then(() => {
                  user.delete(testUser.id).then(() => {
                    user
                      .get(testUser1.id)
                      .then(out => check(out, null, "delete"))
                      .then(() => {
                        resolve();
                        done();
                      });
                  });
                });
            });
          });
        });
      });
    }).then(() => {
      expect(true).toBeTruthy;
      done();
    });
    done();

testUsertestUser1是对象,其某些属性已更改。我已经在Jest外部进行了测试,并且代码运行正常。

console.log user.js:68
    Successfully created new user: test

  console.log user.test.js:26
    { id: 'test',
      email: 'you@me.com',
      first: 'Wilhelm',
      last: 'Erasmus',
      phone: '+271234567' } { id: 'test',
      email: 'yo@me.com',
      first: 'Wilhelm',
      last: 'Erasmus',
      phone: '+277654321' } 'adding'

  console.error user.test.js:28
    { id: 'test',
      email: 'you@me.com',
      first: 'Wilhelm',
      last: 'Erasmus',
      phone: '+271234567' } { id: 'test',
      email: 'yo@me.com',
      first: 'Wilhelm',
      last: 'Erasmus',
      phone: '+277654321' } 'adding'

 FAIL  ./user.test.js (7.269s)
  User APIs
    ✕ runs lifecycle (2779ms)

  ● User APIs › runs lifecycle

    Error

      adding

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |  Unknown |  Unknown |  Unknown |  Unknown |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        9.916s
Ran all test suites.
  console.log user.js:114
    Successfully deleted test

Jest did not exit one second after the test run has completed.

This usually means that there are asynchronous operations that weren't stopped in your tests. Consider running Jest with `--detectOpenHandles` to troubleshoot this issue.

1 个答案:

答案 0 :(得分:0)

我解决了这个问题。我不确定该解决方案为何有效,我很乐意接受能更好地解释它的答案。

    var addUser = user.add(testUser);
    var checkAdd = addUser
      .then(() => user.get(testUser.id))
      .then(check1 => check(check1, testUser, "Add"));

    var updateUser = checkAdd.then(() => user.update(testUser1));
    var checkUpdate = updateUser
      .then(() => user.get(testUser1.id))
      .then(check2 => check(check2, testUser1, "Add"));

    var deleteUser = checkUpdate.then(() => user.delete(testUser1.id));
    var checkDelete = deleteUser.then(() => {
      expect(user.exists).resolves.toBeFalsy;
    });
    return Promise.all([
      addUser,
      checkAdd,
      updateUser,
      checkUpdate,
      deleteUser,
      checkDelete
    ]);