integrationtest typescript code example

Example 1: integrationtest typescript

describe('Contract test for DamageService: getDamageState', () => {
    const provider: Pact = new Pact({
        consumer: 'vehicle-service',
        provider: 'damage-service',
        port: 8080,
        host: 'localhost',
        dir: resolve(__dirname, '../contract/pacts'),
        log: resolve(__dirname, '../contract/logs/pact.log'),
        spec: 2,
    });
    const BASIC_AUTH_MATCHER: string = '^Basic ';
 
    const damagedVehicleId = '1';
    const notDamagedVehicleId = '2';
    const nonExistingDamagedVehicleId = '17';
 
    const validInteractionForDamagedVehicle = new Interaction()
        .given(`Valid vehicle ID = ${damagedVehicleId}`)
        .uponReceiving('a valid vehicle ID from Vehicle Service')
        .withRequest({
            method: 'GET',
            path: `/damage/vehicle/${damagedVehicleId}`,
            headers: {
                [AUTHORIZATION_HEADER]: Matchers.term({
                    generate: `Basic ${BASIC_AUTH}`,
                    matcher: BASIC_AUTH_MATCHER,
                }),
            },
        })
        .willRespondWith({
            status: OK,
            body: Matchers.somethingLike({ damaged: true }),
        });
  
    before(async () => {
        await provider.setup();
    });
 
    after(async () => {
        await provider.finalize();
    });
 
    afterEach(async () => {
        await provider.removeInteractions();
    });
 
    it(`GIVEN vehicle id="${damagedVehicleId}", WHEN getDamageState() is executed, THEN the Promise will be resolved with true.`, async () => {
        await provider.addInteraction(validInteractionForDamagedVehicle);
 
        const result = DamageService.getDamageState(damagedVehicleId);
 
        await expect(result).to.be.eventually.true;
        await expect(provider.verify()).to.not.be.rejected;
    });

Example 2: integrationtest typescript

it('GIVEN a valid vehicle, WHEN posting to vehicles endpoint, THEN a Response with HTTP STATUS OK and the created vehicle will be sent.', async () => {
        const vehicle = {
            id: '1',
            color: 1,
        };
 
        const result = await chai.request(App)
            .post(VEHICLE_ENDPOINT)
            .send(vehicle);
 
        expect(result).to.have.status(OK);
        expect(result.body).to.be.deep.equal(vehicle);
    });

Example 3: integrationtest typescript

it(`GIVEN vehicle id="${healthyVehicle.id}", WHEN getDamageState() is executed THEN the Promise will be resolved with false.`, async () => {
 
            const result = DamageService.getDamageState(healthyVehicle.id);
 
            await expect(result).to.be.eventually.false;
});

Example 4: integrationtest typescript

it(`GIVEN an id = "${id}", WHEN getDetailedVehicle() is executed, THEN the Promise will be resolved with ${expectedVehicleDto}.`, async () => {
            (VehicleRepository.findOne as SinonStub).withArgs(id).resolves(vehicleEntity);
            (ClassTransformer.plainToClass as SinonStub).withArgs(VehicleDto, vehicleEntity).returns(vehicleDto);
            (DamageService.getDamageState as SinonStub).withArgs(vehicleDto.id).resolves(true);
 
            const result = VehicleService.getDetailedVehicle(id);
 
            await expect(result).to.be.eventually.deep.equal(expectedVehicleDto);
});

Tags:

Misc Example