1

我有 koaapp和 jest 测试框架,app由 mongo 包装http.createServer并连接到

mongooseConnect();

const app = new Koa();

initHandlers(app);

app.use(routes);

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

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

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

2 回答 2

1

您必须手动关闭服务器:

      },
    }, () => {
    app.close()
    done()
  });
});
于 2018-03-13T23:00:54.217 回答
-1

在所有测试关闭猫鼬连接之后

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

关闭MongoConnection:

import mongoose from 'mongoose';

export default () => {
  return new Promise((res) => {
    res(mongoose.connection.close());
  });
};
于 2017-09-27T10:31:44.737 回答