test: configured jest + cucumber, added tests for 'create user', + some refactoring

This commit is contained in:
user
2021-09-19 13:41:37 +02:00
parent ecffa9bcf4
commit 6646099455
29 changed files with 424 additions and 60 deletions

8
tests/jestGlobalSetup.ts Normal file
View File

@@ -0,0 +1,8 @@
import * as dotenv from 'dotenv';
import * as path from 'path';
// This will force dotenv to use environmental variables from ".env.test" instead of ".env"
const envPath: string = path.resolve(__dirname, '../.env.test');
module.exports = async (): Promise<void> => {
dotenv.config({ path: envPath });
};

View File

@@ -0,0 +1,48 @@
import { Test, TestingModuleBuilder, TestingModule } from '@nestjs/testing';
import { AppModule } from '@src/app.module';
import { NestExpressApplication } from '@nestjs/platform-express';
export class TestServer {
constructor(
public readonly serverApplication: NestExpressApplication,
public readonly testingModule: TestingModule,
) {}
public static async new(
testingModuleBuilder: TestingModuleBuilder,
): Promise<TestServer> {
const testingModule: TestingModule = await testingModuleBuilder.compile();
const serverApplication: NestExpressApplication = testingModule.createNestApplication();
await serverApplication.init();
return new TestServer(serverApplication, testingModule);
}
}
export async function generateTestingApplication(): Promise<{
testServer: TestServer;
// api: ApiClient;
}> {
const testServer = await TestServer.new(
Test.createTestingModule({
imports: [AppModule],
}),
);
return {
testServer,
};
}
let testServer: TestServer;
export function getTestServer(): TestServer {
return testServer;
}
beforeAll(
async (): Promise<void> => {
({ testServer } = await generateTestingApplication());
},
);

View File

@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Create a user Creating a user happy path 1`] = `
Object {
"id": Any<String>,
}
`;
exports[`Create a user Creating a user happy path 2`] = `
Array [
Object {
"country": "England",
"createdAt": Any<String>,
"email": "john.doe@gmail.com",
"id": Any<String>,
"postalCode": "29145",
"street": "Road Avenue",
"updatedAt": Any<String>,
},
]
`;

View File

@@ -0,0 +1,62 @@
import { defineFeature, loadFeature } from 'jest-cucumber';
import * as request from 'supertest';
import { CreateUser } from '@src/interface-adapters/interfaces/user/create.user.interface';
import { Id } from '@src/libs/ddd/interface-adapters/interfaces/id.interface';
import { UserResponse } from '@src/modules/user/dtos/user.response.dto';
import { snapshotBaseProps } from '@src/libs/test-utils/snapshot-base-props';
import { getTestServer, TestServer } from '../../jestSetupAfterEnv';
const feature = loadFeature('tests/user/create-user/create-user.feature');
defineFeature(feature, test => {
let testServer: TestServer;
let httpServer: request.SuperTest<request.Test>;
beforeAll(() => {
testServer = getTestServer();
httpServer = request(testServer.serverApplication.getHttpServer());
});
afterAll(() => {
// TODO: clean db after tests are finished
});
test('Creating a user happy path', ({ given, when, then, and }) => {
const userDto: Partial<CreateUser> = {};
let userId: Id;
given(/^that my email is "(.*)"$/, (email: string) => {
userDto.email = email;
});
and(
/^my country is "(.*)", my postal code is "(.*)" and my street is "(.*)"$/,
(country: string, postalCode: string, street: string) => {
userDto.country = country;
userDto.postalCode = postalCode;
userDto.street = street;
},
);
when('I send a request to create a user', async () => {
const res = await httpServer
.post('/users')
.send(userDto)
.expect(201);
userId = res.body;
});
then('I receive my user ID', () => {
expect(userId).toMatchSnapshot({ id: expect.any(String) });
});
and('I can see my user in a list of all users', async () => {
const res = await httpServer.get('/users').expect(200);
expect(res.body).toMatchSnapshot([snapshotBaseProps]);
expect(res.body.some((item: UserResponse) => item.id === userId.id)).toBe(
true,
);
});
});
});

View File

@@ -0,0 +1,8 @@
Feature: Create a user
Scenario: Creating a user happy path
Given that my email is "john.doe@gmail.com"
And my country is "England", my postal code is "29145" and my street is "Road Avenue"
When I send a request to create a user
Then I receive my user ID
And I can see my user in a list of all users