Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: 백엔드 E2E 테스트 시간 단축 #324

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions backend/package-lock.json

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

3 changes: 2 additions & 1 deletion backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json --runInBand"
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down Expand Up @@ -55,6 +55,7 @@
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-prettier": "^5.0.0",
"jest": "^29.5.0",
"portfinder": "^1.0.32",
"prettier": "^3.0.0",
"socket.io-client": "^4.7.5",
"source-map-support": "^0.5.21",
Expand Down
3 changes: 2 additions & 1 deletion backend/test/project/join-project.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createMember,
createProject,
getProjectLinkId,
listenAppAndSetPortEnv,
memberFixture,
memberFixture2,
projectPayload,
Expand All @@ -14,7 +15,7 @@ describe('Join Project', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

it('should return 201', async () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/test/project/ws-backlog-page/ws-epic.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LexoRank } from 'lexorank';
import { resolve } from 'path';
import { Socket } from 'socket.io-client';
import { app, appInit } from 'test/setup';
import { app, appInit, listenAppAndSetPortEnv } from 'test/setup';
import {
getMemberJoinedLandingPage,
getTwoMemberJoinedLandingPage,
Expand All @@ -11,7 +11,7 @@ describe('WS epic', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

describe('epic create', () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { LexoRank } from 'lexorank';
import { Socket } from 'socket.io-client';
import { app, appInit } from 'test/setup';
import { app, appInit, listenAppAndSetPortEnv } from 'test/setup';
import { getTwoMemberJoinedLandingPage } from '../ws-common';

describe('WS epic', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

describe('backlog init', () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/test/project/ws-backlog-page/ws-story.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LexoRank } from 'lexorank';
import { Socket } from 'socket.io-client';
import { app, appInit } from 'test/setup';
import { app, appInit, listenAppAndSetPortEnv } from 'test/setup';
import {
getMemberJoinedLandingPage,
getTwoMemberJoinedLandingPage,
Expand All @@ -10,7 +10,7 @@ describe('WS story', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

describe('story create', () => {
Expand Down
4 changes: 2 additions & 2 deletions backend/test/project/ws-backlog-page/ws-task.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { LexoRank } from 'lexorank';
import { Socket } from 'socket.io-client';
import { app, appInit } from 'test/setup';
import { app, appInit, listenAppAndSetPortEnv } from 'test/setup';
import {
getMemberJoinedLandingPage,
getTwoMemberJoinedLandingPage,
Expand All @@ -10,7 +10,7 @@ describe('WS task', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

describe('task create', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import {
createMember,
createProject,
githubApiService,
listenAppAndSetPortEnv,
memberFixture,
memberFixture2,
projectPayload,
} from 'test/setup';
import { io } from 'socket.io-client';

describe('WS landing', () => {
const serverUrl = 'http://localhost:3000/project';
let serverUrl;
let socket;

beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
serverUrl = `http://localhost:${process.env.PORT}/project`;
});

afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getProjectLinkId,
memberFixture2,
joinProject,
listenAppAndSetPortEnv,
} from 'test/setup';
import {
emitJoinLanding,
Expand All @@ -23,7 +24,7 @@ describe('WS join project', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

it('should return joined member', async () => {
Expand Down
3 changes: 2 additions & 1 deletion backend/test/project/ws-landing-page/ws-link.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
createProject,
getProjectLinkId,
joinProject,
listenAppAndSetPortEnv,
memberFixture,
memberFixture2,
projectPayload,
Expand All @@ -23,7 +24,7 @@ describe('WS link', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});
describe('link create', () => {
it('should return created link data when received create link request', async () => {
Expand Down
3 changes: 2 additions & 1 deletion backend/test/project/ws-landing-page/ws-memo.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createProject,
getProjectLinkId,
joinProject,
listenAppAndSetPortEnv,
memberFixture,
memberFixture2,
projectPayload,
Expand All @@ -23,7 +24,7 @@ describe('WS memo', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});
describe('memo create', () => {
it('should return created memo data when received create memo request', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getMemberByAccessToken,
getProjectLinkId,
joinProject,
listenAppAndSetPortEnv,
memberFixture,
memberFixture2,
projectPayload,
Expand All @@ -22,7 +23,7 @@ describe('WS landing', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

it('should return project landing page data', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
getProjectLinkId,
memberFixture2,
joinProject,
listenAppAndSetPortEnv,
} from 'test/setup';
import {
emitJoinLanding,
Expand All @@ -24,7 +25,7 @@ describe('WS update member status', () => {
beforeEach(async () => {
await app.close();
await appInit();
await app.listen(3000);
await listenAppAndSetPortEnv(app);
});

it('should return updated member status', async () => {
Expand Down
57 changes: 52 additions & 5 deletions backend/test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { CreateProjectRequestDto } from 'src/project/dto/CreateProjectRequest.dt
import { io } from 'socket.io-client';
import { Member } from 'src/member/entity/member.entity';
import { MemberService } from 'src/member/service/member.service';
import * as portfinder from 'portfinder';
import { v4 as uuidv4 } from 'uuid';

export let app: INestApplication;
export let githubApiService: GithubApiService;
Expand Down Expand Up @@ -126,12 +128,15 @@ export const joinProject = async (
};

export const connectServer = (projectId, accessToken) => {
const socket = io(`http://localhost:3000/project-${projectId}`, {
path: '/api/socket.io',
auth: {
accessToken,
const socket = io(
`http://localhost:${process.env.PORT}/project-${projectId}`,
{
path: '/api/socket.io',
auth: {
accessToken,
},
},
});
);
return socket;
};

Expand Down Expand Up @@ -159,7 +164,48 @@ export const appInit = async () => {
await app.init();
};

export const listenAppAndSetPortEnv = async (app) => {
let port = await portfinder.getPortPromise();
while (1) {
try {
await app.listen(port);
break;
} catch (error) {
if (error.code === 'EADDRINUSE') {
port = await portfinder.getPortPromise();
} else throw error;
}
}
process.env.PORT = port.toString();
};

const createDatabase = async (dbName: string) => {
const normalDataSource = getDataSource();
await normalDataSource.initialize();
await normalDataSource.query(`CREATE DATABASE \`${dbName}\`;`);
await normalDataSource.destroy();
};

const dropDatabase = async (dbName: string) => {
const normalDataSource = getDataSource();
await normalDataSource.initialize();
await normalDataSource.query(`DROP DATABASE IF EXISTS \`${dbName}\`;`);
await normalDataSource.destroy();
};

const getDataSource = () => {
return new DataSource({
type: 'mysql',
host: process.env.DATABASE_HOST,
port: +process.env.DATABASE_PORT,
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
});
};

beforeAll(async () => {
process.env.DATABASE_NAME = uuidv4();
await createDatabase(process.env.DATABASE_NAME);
await appInit();
});

Expand All @@ -176,4 +222,5 @@ beforeEach(async () => {

afterAll(async () => {
await app.close();
await dropDatabase(process.env.DATABASE_NAME);
});
Loading