Skip to content

Commit

Permalink
feat: Improve e2e tests running
Browse files Browse the repository at this point in the history
  • Loading branch information
letehaha committed Aug 2, 2023
1 parent e3f0dc0 commit ef8c9cd
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 177 deletions.
2 changes: 2 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

export default {
preset: 'ts-jest',
verbose: true,
testEnvironment: 'node',
testMatch: ['**/?(*.)+(unit|spec|e2e).[jt]s?(x)'],
transform: {
'^.+\\.ts?$': ['ts-jest', {
tsconfig: 'tsconfig.json',
}],
},
setupFilesAfterEnv: ["<rootDir>/src/tests/setupTests.ts"],
moduleNameMapper: {
'shared-types': '<rootDir>/shared-types',
'@routes/(.*)': '<rootDir>/src/routes/$1',
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"seed-undo": "npx sequelize-cli db:seed:undo:all",
"db:reset": "cross-env NODE_ENV=test npx sequelize-cli db:drop && npx sequelize-cli db:create && npx sequelize-cli db:migrate",
"pretest": "cross-env NODE_ENV=test npm run db:reset",
"test": "cross-env NODE_ENV=test jest --passWithNoTests --forceExit",
"test": "cross-env NODE_ENV=test jest --runInBand --passWithNoTests --forceExit --detectOpenHandles",
"lint": "eslint .",
"docker-build": "docker build . -t letehaha/budget-tracker-be",
"docker-push": "docker push letehaha/budget-tracker-be",
Expand Down Expand Up @@ -101,7 +101,7 @@
"jest": "^29.6.1",
"module-alias": "^2.2.2",
"nodemon": "^2.0.20",
"supertest": "^6.3.0",
"supertest": "^6.3.3",
"ts-jest": "^29.1.1",
"tslint": "^6.1.3"
}
Expand Down
72 changes: 1 addition & 71 deletions src/controllers/banks/monobank.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,9 @@ import {
ExternalMonobankClientInfoResponse,
API_ERROR_CODES,
} from 'shared-types';
import path from 'path';
import request from 'supertest';
import Umzug from 'umzug';
import { app, serverInstance, redisClient } from '@root/app';
import { connection } from '@models/index';
import { ERROR_CODES } from '@js/errors';
import { makeRequest, extractResponse } from '@tests/helpers';

jest.mock('axios');

const umzug = new Umzug({
migrations: {
// The params that get passed to the migrations
params: [
connection.sequelize.getQueryInterface(),
connection.sequelize.constructor,
],
// The path to the migrations directory
path: path.join(__dirname, '../../migrations'),
// The pattern that determines whether files are migrations
pattern: /\.js$/,
},
storage: 'sequelize',
storageOptions: {
sequelize: connection.sequelize,
},
});

const getMockedClientData = (): { data: ExternalMonobankClientInfoResponse } => ({
data: {
clientId: 'sdfsdfsdf',
Expand Down Expand Up @@ -68,54 +43,13 @@ const getMockedClientData = (): { data: ExternalMonobankClientInfoResponse } =>
});

const DUMB_MONOBANK_API_TOKEN = '234234234234';
const BASE_CURRENCY_ID = 2;

describe('Balances model', () => {
afterAll(() => {
redisClient.quit();
serverInstance.close();
});

let token

beforeEach(async () => {
try {
await connection.sequelize.sync({ force: true });
await connection.sequelize.drop({ cascade: true });
redisClient.FLUSHALL('SYNC');
await umzug.up();

await request(app)
.post('/api/v1/auth/register')
.send({
username: 'test1',
password: 'test1',
});

const res = await request(app)
.post('/api/v1/auth/login')
.send({
username: 'test1',
password: 'test1',
});

token = extractResponse(res).token;

await request(app)
.post('/api/v1/user/currencies/base')
.set('Authorization', token)
.send({ currencyId: BASE_CURRENCY_ID });
} catch (err) {
console.log(err)
}
})

describe('Pair Monobank account', () => {
it('throws validation error if no "token" passed', async () => {
const result = await makeRequest({
method: 'post',
url: '/banks/monobank/pair-user',
token,
});

expect(result.status).toEqual(ERROR_CODES.ValidationError);
Expand All @@ -124,7 +58,6 @@ describe('Balances model', () => {
const result = await makeRequest({
method: 'post',
url: '/banks/monobank/pair-user',
token,
payload: {
token: DUMB_MONOBANK_API_TOKEN,
},
Expand All @@ -139,7 +72,6 @@ describe('Balances model', () => {
const createdMonoUserRestult = await makeRequest({
method: 'post',
url: '/banks/monobank/pair-user',
token,
payload: {
token: DUMB_MONOBANK_API_TOKEN,
},
Expand All @@ -148,7 +80,7 @@ describe('Balances model', () => {
expect(extractResponse(createdMonoUserRestult).apiToken).toBe(DUMB_MONOBANK_API_TOKEN);
expect(extractResponse(createdMonoUserRestult).accounts.length).toBe(mockedClientData.data.accounts.length);

const accountResult = extractResponse(await makeRequest({ method: 'get', url: '/accounts', token }));
const accountResult = extractResponse(await makeRequest({ method: 'get', url: '/accounts' }));

mockedClientData.data.accounts.forEach((item, index) => {
const mockedAccount = mockedClientData.data.accounts[index];
Expand All @@ -169,7 +101,6 @@ describe('Balances model', () => {
const result = await makeRequest({
method: 'post',
url: '/banks/monobank/pair-user',
token,
payload: {
token: DUMB_MONOBANK_API_TOKEN,
},
Expand All @@ -180,7 +111,6 @@ describe('Balances model', () => {
const oneMoreResult = await makeRequest({
method: 'post',
url: '/banks/monobank/pair-user',
token,
payload: {
token: DUMB_MONOBANK_API_TOKEN,
},
Expand Down
Loading

0 comments on commit ef8c9cd

Please sign in to comment.