我的应用程序运行堆栈NESTJS-ARANGODB。我正在使用jest和supertest进行e2e测试。在我的本地环境中,所有测试均通过。我们有一个管道,可以像这样使用arangodb提供的docker镜像
FROM arangodb/arangodb:3.6.1
COPY . .
RUN arangod \
--server.endpoint tcp://127.0.0.1:8529 \
--server.authentication false \
--log.file /tmp/init-log \
--log.foreground-tty false & \
sleep 5 && \
sh arangoconf.sh root password
EXPOSE 8529
CMD ["arangod"]
其中arangoconf.sh是使用aragosh填充数据库的简短脚本。要部署Docker映像,我们运行以下命令
docker build -t dbname .
docker run -e ARANGO_ROOT_PASSWORD=password -p 8529:8529 dbname
然后我们运行e2e测试。我们有11个模块,每个模块都有一个e2e测试文件。在测试的第一轮中,其中一个随机失败。错误如下
登录失败,如图所示,但是所有文件都使用相同的凭据。我试过在Jest中添加-runInBand选项,但是它不起作用。在我当地,一切工作都非常完美。我认为arango配置有问题。还发布了e2e测试文件。
import * as request from 'supertest';
import { Test } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import { EventModule } from '../../../../src/modules/session/event/event.module';
import { TreatmentModule } from '../../../../src/modules/session/treatment/treatment.module';
import { AuthModule } from '../../../../src/modules/auth/auth.module';
import { EventService } from '../../../../src/modules/session/event/services/event.service';
import { TreatmentService } from '../../../../src/modules/session/treatment/services/treatment.service';
import { EventDataMock } from '../../../mocks/session/event/event-data.mock';
import { TreatmentDataMock } from '../../../mocks/session/treatment/treatment-data.mock';
import { AuthDataMock } from '../../../mocks/auth/auth-data.mock';
import { GenericResponses } from '../../../../src/constants/generic-responses';
import { EventTreatmentService } from '../../../../src/modules/session/event/services/event-treatment.service';
describe('Events', () => {
let app: INestApplication;
let service: EventService;
let treatmentService: TreatmentService;
let eventTreatmentService: EventTreatmentService;
let eventDataMock: EventDataMock;
let treatmentDataMock: TreatmentDataMock;
let authDataMock: AuthDataMock;
let eventRequest;
let eventResponse;
let treatmentStartResponse;
const keysList: string[] = new Array();
const treatmentKey: string[] = new Array();
let userToken: string;
beforeAll(async (done) => {
const module = await Test.createTestingModule({
imports: [EventModule, TreatmentModule, AuthModule],
}).compile();
app = module.createNestApplication();
service = module.get<EventService>(EventService);
treatmentService = module.get<TreatmentService>(TreatmentService);
eventTreatmentService = module.get<EventTreatmentService>(EventTreatmentService);
eventDataMock = new EventDataMock();
treatmentDataMock = new TreatmentDataMock();
authDataMock = new AuthDataMock();
eventRequest = JSON.parse(JSON.stringify(eventDataMock.newEvent));
eventResponse = JSON.parse(JSON.stringify(eventDataMock.newEventResponse));
treatmentStartResponse = JSON.parse(JSON.stringify(treatmentDataMock.startResponse));
await app.init();
request(app.getHttpServer()).post('/').end( () => done() );
});
it('Login', () => {
return request(app.getHttpServer())
.post('/auth/login/user')
.send(authDataMock.userLoginData)
.expect( (res) => {
expect(res.body.data).toHaveProperty('accessToken');
userToken = res.body.data.accessToken;
});
});
it('/Start a new treatment return ok response and the treatment with the SUID', () => {
return request(app.getHttpServer())
.post('/treatment/start')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + userToken)
.send(treatmentDataMock.startRequest)
.expect((response) => {
expect(response.body).toHaveProperty('data');
expect(response.body.data).toHaveProperty('treatment');
expect(response.body.data.treatment).toHaveProperty('_key');
expect(response.body.data.treatment).toHaveProperty('SUID');
const key = response.body.data.treatment._key;
const SUID = response.body.data.treatment.SUID;
treatmentKey.push(key);
treatmentStartResponse._key = key;
treatmentStartResponse.SUID = SUID;
eventRequest.SUID = SUID;
eventResponse.SUID = SUID;
});
});
it('/Add event must return ok response and the new event', () => {
return request(app.getHttpServer())
.post('/event')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + userToken)
.send(eventRequest)
.expect((response) => {
expect(response.body).toHaveProperty('data');
expect(response.body.data).toHaveProperty('event');
expect(response.body.data.event).toHaveProperty('_key');
expect(response.body.data.event).toHaveProperty('SUID');
const key = response.body.data.event._key;
keysList.push(key);
eventResponse._key = key;
});
});
it('/Post event without SUID returns bad request', () => {
const requestBody = JSON.parse(JSON.stringify(eventRequest));
delete requestBody.SUID;
return request(app.getHttpServer())
.post('/event')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + userToken)
.send(requestBody)
.expect(GenericResponses.GENERIC_BAD_REQUEST('"SUID" is required'));
});
it('/Post event with wrong SUID returns treatment not found', () => {
const requestBody = JSON.parse(JSON.stringify(eventRequest));
requestBody.SUID = '923174891724897';
return request(app.getHttpServer())
.post('/event')
.set('Accept', 'application/json')
.set('Authorization', 'Bearer ' + userToken)
.send(requestBody)
.expect(GenericResponses.GENERIC_NOT_FOUND('treatment'));
});
afterAll(async () => {
await service.deleteByKeys(keysList);
await treatmentService.deleteByKeys(treatmentKey);
const eventTreatmentList = await eventTreatmentService.getAllEdges(`events/${keysList[0]}`);
await eventTreatmentService.deleteByKeys(eventTreatmentList);
await app.close();
});
});