【发布时间】:2021-03-04 09:45:56
【问题描述】:
我有一个带有 jest e2e 测试套件的 nestjs 应用程序。在本地运行大约需要 13 秒~。但是,当尝试在 circleci 上运行它们时,测试挂起并且似乎没有成功退出。他们在 10 分钟超时并失败。我不确定是什么导致了这种行为。
/.circleci/config.yml
version: 2.1
jobs:
run_tests:
docker:
- image: circleci/node:10
steps:
- checkout #cloning git repo
- run:
name: Install npm dependencies
command: |
npm install --save
- run:
name: Run unit tests
command: |
NODE_ENV=test && node node_modules/.bin/jest
environment:
DATABASE_URL: postgres://_:_:@localhost:5432/testdb
- run:
name: Run e2e tests
command: |
NODE_ENV=test && node --inspect-brk node_modules/.bin/jest --runInBand --config ./test/jest-e2e.json --watchAll=false
environment:
DATABASE_URL: postgres://_:_:@localhost:5432/testdb
- store_test_results:
path: test-results
workflows:
build_test:
jobs:
- run_tests
test/jest-e2e.json
{
"moduleFileExtensions": ["js", "json", "ts"],
"rootDir": ".",
"testEnvironment": "node",
"testRegex": ".e2e-spec.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"verbose": true
}
测试设置
beforeAll(async () => {
const rootModule: TestingModule = await Test.createTestingModule({
imports: [RootModule]
}).compile();
app = rootModule.createNestApplication();
await app.init();
shopboxService = app.select(ShopboxModule).get(ShopboxService);
clothesService = app.select(ClothesModule).get(ClothesService);
await shopboxService.deleteTags();
});
it('authenticates a user and includes a jwt token in the response', async () => {
const response = await request(app.getHttpServer())
.post('/auth/login')
.send({ email: 'myemail@example.com', password: '123456789' })
.expect(201);
adminAccessToken = response.body.token;
expect(adminAccessToken).toMatch(
/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/
);
});
测试拆解
afterAll(async () => {
try {
await shopboxService.deleteTags();
} catch (e) {
console.warn('error', e);
} finally {
await app.close();
}
});
删除 --inspect-brk circleci 后开始提供测试反馈。
FAIL test/user-store-reservation-flow.e2e-spec.ts (37.493 s)
User create store reservation flow
✕ authenticates a user and includes a jwt token in the response (2 ms)
✕ authenticates a user and includes a jwt token in the response
✕ creates a user
✕ creates a reservation, check that it exists in shopbox (4 ms)
✕ create clothing on storeReservation, check that it has the right properties
✕ check that clothing exists in shopbox, and has correct price, barcode, user, tag and reservation (1 ms)
✕ fails when persisting another one, should not persist to shopbox
✕ updating shopbox stock should retrieve stock 1
✕ get current reservations should return 1 (1 ms)
✕ should not be able to create a second reservation
✕ delete clothing should fail on user, but be accepted as employee
✕ delete clothing should fail on user, but be accepted as employee
✕ create a new piece of clothing and assert that it exists in the db and shopbox (1 ms)
✕ delete reservation and check that the clothing and reservation is terminated in postgres and does not exist in shopbox
● User create store reservation flow › authenticates a user and includes a jwt token in the response
Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.Error: Timeout - Async callback was not invoked within the 30000 ms timeout specified by jest.setTimeout.
at mapper (../node_modules/jest-jasmine2/build/queueRunner.js:27:45)
● User create store reservation flow › authenticates a user and includes a jwt token in the response
TypeError: Cannot read property 'getHttpServer' of undefined
38 |
39 | it('authenticates a user and includes a jwt token in the response', async () => {
> 40 | const response = await request(app.getHttpServer())
| ^
41 | .post('/auth/login')
42 | .send({ email: '_', password: '123456789' })
43 | .expect(201);
at Object.it (user-store-reservation-flow.e2e-spec.ts:40:40)
我不确定为什么会失败,因为它在本地按预期工作。
另外,在运行测试后,似乎数据库连接没有正确关闭
Time: 37.537 s
Ran all test suites.
[Nest] 624 - 11/21/2020, 12:37:07 AM [ExceptionHandler] Unable to connect to the database. Retrying (1)...
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.
[Nest] 624 - 11/21/2020, 12:37:40 AM [ExceptionHandler] Unable to connect to the database. Retrying (2)... +33005ms
[Nest] 624 - 11/21/2020, 12:38:13 AM [ExceptionHandler] Unable to connect to the database. Retrying (3)... +33007ms
[Nest] 624 - 11/21/2020, 12:38:46 AM [ExceptionHandler] Unable to connect to the database. Retrying (4)... +33004ms
[Nest] 624 - 11/21/2020, 12:39:19 AM [ExceptionHandler] Unable to connect to the database. Retrying (5)... +33005ms
[Nest] 624 - 11/21/2020, 12:39:52 AM [ExceptionHandler] Unable to connect to the database. Retrying (6)... +33006ms
【问题讨论】:
-
嗨 Jonas,你的 CI 命令中需要这个
--inspect-brk吗?此外,超时似乎是 CircleCI 的默认设置,而不是开玩笑。 -
@IvanSantos 我不认为我这样做,我想我前段时间添加了它以进行调试。我已将其删除并再次运行管道
-
@IvanSantos 实际上产生了很大的不同,现在 circleci 提供测试信息!
-
看起来您的测试无法连接到数据库。检查配置。
-
@IvanSantos 是的,我怀疑它是 mongo,因为应用程序正在使用它来记录日志。我必须摆弄我假设的
config.yml
标签: testing jestjs continuous-integration nestjs circleci