Skip to content

Commit

Permalink
run tests in ci again
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Oct 28, 2024
1 parent d2a978a commit 9f6a114
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 20 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/node-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,6 @@ jobs:
- name: Build Python API
working-directory: ./api-node
run: yarn python-api:build
- name: Start Python API
- name: Run E2E tests
working-directory: ./api-node
run: yarn python-api:start
- name: Run e2e tests
working-directory: ./api-node
run: yarn test:e2e
- name: Stop Python API
working-directory: ./api-node
run: yarn python-api:stop
run: yarn test:e2e:ci
5 changes: 4 additions & 1 deletion api-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
"scripts": {
"build": "nest build && yarn sentry:sourcemaps",
"build:ci": "nest build",
"clean": "rm -rf dist",
"clean:all": "yarn clean && rm -rf dist node_modules && yarn install",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
Expand All @@ -19,10 +21,11 @@
"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 --testPathIgnorePatterns=everything.e2e-spec.ts",
"test:e2e:ci": "NODE_ENV=test ts-node ./scripts/run-e2e-tests.ts",
"test:e2e:urls": "NODE_ENV=test jest --config ./test/jest-e2e.json everything.e2e-spec.ts",
"test:e2e:urls:generate": "ts-node ./test/utils/getUrls.ts",
"python-api:build": "docker build -t registry-api-server ../",
"python-api:start": "docker run -d -p 5031:5030 registry-api-server",
"python-api:start": "docker start $(docker ps -aq --filter ancestor=registry-api-server) && echo 'Flask API started successfully' || (docker run -d -p 5031:5030 registry-api-server && echo 'Flask API started successfully')",
"python-api:stop": "docker stop $(docker ps -q --filter ancestor=registry-api-server)",
"start:all": "yarn build && yarn start:dev & yarn python-api:start",
"sentry:sourcemaps": "sentry-cli sourcemaps inject --org sentry --project release-registry-nestjs ./dist && sentry-cli sourcemaps upload --org sentry --project release-registry-nestjs ./dist"
Expand Down
68 changes: 68 additions & 0 deletions api-node/scripts/run-e2e-tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// A script that runs the e2e tests
// 1. Starts the NestJS and Flask APIs in Docker
// 2. Runs the tests
// 3. Stops the NestJS and Flask APIs

import * as childProcess from 'child_process';

const nodeApi = childProcess.spawn('yarn', ['start:prod']);

let nodeApiStarted = false;
let pythonApiStarted = false;

nodeApi.stdout.on('data', (data) => {
console.log('[NestJS]', data.toString());
if (data.toString().includes('application successfully started')) {
nodeApiStarted = true;
checkAndRunTests();
}
});

nodeApi.stderr.on('data', (data) => {
console.error('[NestJS]', data.toString());
});

const pythonApi = childProcess.spawn('yarn', ['python-api:start']);

pythonApi.stdout.on('data', (data) => {
console.log('[Flask]', data.toString());
if (data.toString().includes('Flask API started successfully')) {
pythonApiStarted = true;
checkAndRunTests();
}
});

pythonApi.stderr.on('data', (data) => {
console.error('[Flask]', data.toString());
});

function checkAndRunTests(): void {
console.log('checkAndRunTests', { nodeApiStarted, pythonApiStarted });
if (nodeApiStarted && pythonApiStarted) {
console.log('Both APIs are running. Starting e2e tests...');
const tests = childProcess.spawn('yarn', ['test:e2e']);

tests.stdout.on('data', (data) => {
console.log('[Tests]', data.toString());
});

tests.stderr.on('data', (data) => {
console.error('[Tests]', data.toString());
});

tests.on('close', (code) => {
console.log(`Tests finished with code ${code}`);
cleanup(code);
});
}
}

function cleanup(code: number): void {
console.log('Cleaning up...');
nodeApi.kill();
pythonApi.kill();
childProcess.execSync('yarn python-api:stop');
process.exit(code);
}

process.on('SIGINT', () => cleanup(0));
2 changes: 1 addition & 1 deletion api-node/test/utils/getUrls.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PYTHON_API_URL } from '../utils';
import { PYTHON_API_URL } from './makeRequest';
import * as fs from 'fs';
import * as path from 'path';

Expand Down
14 changes: 6 additions & 8 deletions api-node/test/utils/makeRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@ export async function makeDuplexRequest(
path: string,
options: RequestInit = { redirect: 'manual' },
): Promise<{ python: ResponseParts; node: ResponseParts }> {
const [pythonResponse, nodeResponse] = await Promise.all([
fetch(`${PYTHON_API_URL}${path}`, options),
fetch(`${API_NODE_URL}${path}`, options),
]);
const pythonResponse = await fetch(`${PYTHON_API_URL}${path}`, options);
const pythonBody = await pythonResponse.text();

const nodeResponse = await fetch(`${API_NODE_URL}${path}`, options);
const nodeBody = await nodeResponse.text();

const pythonStatus = pythonResponse.status;
const nodeStatus = nodeResponse.status;
Expand All @@ -45,9 +46,6 @@ export async function makeDuplexRequest(
nodeHeaders: normalizedNodeHeaders,
} = normalizeHeaders(originalPythonHeaders, originalNodeHeaders);

const pythonBody = await pythonResponse.text();
const nodeBody = await nodeResponse.text();

return {
python: {
status: pythonStatus,
Expand Down Expand Up @@ -86,7 +84,7 @@ function normalizeHeaders(
const pythonContentLength = parseInt(pythonHeaders['content-length']);
const nodeContentLength = parseInt(nodeHeaders['content-length']);
if (pythonContentLength - nodeContentLength === 1) {
pythonHeaders['content-length'] = nodeHeaders['content-length'];
nodeHeaders['content-length'] = pythonHeaders['content-length'];
}
}

Expand Down
2 changes: 1 addition & 1 deletion api-node/tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
"exclude": ["node_modules", "test", "dist", "**/*spec.ts", "scripts"]
}
2 changes: 1 addition & 1 deletion api-node/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
"inlineSources": true,
"sourceRoot": "/"
"sourceRoot": "/src"
}
}

0 comments on commit 9f6a114

Please sign in to comment.