测试后,Supertest不会关闭Koa服务器

时间:2017-09-27 10:26:14

标签: node.js jestjs koa supertest koa2

我有koa app和jest测试框架,app包裹http.createServer并连接到mongo

mongooseConnect();

const app = new Koa();

initHandlers(app);

app.use(routes);

export default http.createServer(app.callback());

测试后,supertest不关闭服务器,测试示例

test('success create user', (done) => {
      return supertest(app)
        .post(`/api/auth/signup`)
        .send(userData)
        .expect(200)
        .expect({
          data: {
            email: userData.email,
            firstName: userData.firstName.toLowerCase(),
            lastName: userData.lastName.toLowerCase(),
            bio: userData.bio,
          },
        }, done);
    });

2 个答案:

答案 0 :(得分:1)

您必须手动关闭服务器:

      },
    }, () => {
    app.close()
    done()
  });
});

答案 1 :(得分:-1)

在所有测试结束之后关闭mongoose连接

afterAll(async (done) => {
  await closeMongoConnection();
  done();
});

closeMongoConnection:

import mongoose from 'mongoose';

export default () => {
  return new Promise((res) => {
    res(mongoose.connection.close());
  });
};