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

Integration test #173

Merged
merged 11 commits into from
Oct 19, 2021
6 changes: 5 additions & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ jobs:
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ matrix.node-version }}-${{ hashFiles('**/yarn.lock') }}
- name: Install dependencies
run: yarn install
- name: Test and build
- name: Test and build
run: |
yarn lint
yarn test
yarn build
- name: Integration test
run: |
yarn build:intTest
yarn test:int
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ dist
yarn-error.log
.npmrc
coverage
tests/integration/node_modules
tests/integration/yarn-error.log
tests/integration/yarn.lock
tests/integration/quick-start.ts
tests/integration/package.json
tests/integration/tsconfig.json
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"build:compile": "tsc --build tsconfig.build.json",
"build:swagger": "ts-node example/generate-open-api-schema.ts > example/example.swagger.yaml",
"build:license": "ts-node tools/generate-license.ts > ./LICENSE",
"build:intTest": "ts-node tools/generate-integration-test.ts && yarn install --cwd ./tests/integration",
"test": "yarn test:jest && yarn test:badge",
"test:int": "yarn jest ./tests/integration",
"test:u": "yarn test:jest -u",
"test:jest": "yarn jest --detectOpenHandles ./tests/unit ./tests/system",
"test:badge": "yarn make-coverage-badge --output-path ./coverage.svg",
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/quick-start.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {ChildProcessWithoutNullStreams, spawn} from 'child_process';
import fetch from 'node-fetch';
import {waitFor} from '../helpers';

describe('Integration Test', () => {
let quickStart: ChildProcessWithoutNullStreams;
let out = '';
const listener = (chunk: Buffer) => {
out += chunk.toString();
};

beforeAll(() => {
quickStart = spawn(
'yarn',
['start'],
{cwd: './tests/integration'}
);
quickStart.stdout.on('data', listener);
quickStart.stdout.on('data', listener);
});

afterAll(async () => {
quickStart.stdout.removeListener('data', listener);
quickStart.kill();
await waitFor(() => quickStart.killed);
});

afterEach(() => {
out = '';
});

describe('Quick Start from Readme', () => {
test('Should listen', async () => {
await waitFor(() => /Listening 8090/.test(out));
expect(true).toBeTruthy();
});

test('Should handle valid GET request', async () => {
const response = await fetch('http://localhost:8090/v1/hello?name=Rick');
expect(response.status).toBe(200);
const json = await response.json();
expect(json).toEqual({
status: 'success',
data: {
greetings: 'Hello, Rick. Happy coding!'
}
});
});
});
});
54 changes: 54 additions & 0 deletions tools/generate-integration-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs';

const nodeVersion = process.versions.node.split('.').shift();
const tsconfigBase = nodeVersion === '15' ? '14' : nodeVersion;

const packageJson = `
{
"name": "express-zod-api-integration-test",
"version": "1.0.0",
"scripts": {
"start": "ts-node quick-start.ts"
},
"author": {
"name": "Anna Bocharova",
"url": "https://robintail.cz",
"email": "me@robintail.cz"
},
"license": "MIT",
"dependencies": {
"@tsconfig/node${tsconfigBase}": "latest",
"express-zod-api": "../../dist",
"ts-node": "9.1.1",
"typescript": "4.4.4"
}
}
`;

const tsConfigJson = `
{
"extends": "@tsconfig/node${tsconfigBase}/tsconfig.json",
}
`;

const readme = fs.readFileSync('README.md', 'utf-8');
const quickStartSection = readme.match(/# Quick start(.+?)\n#\s[A-Z]+/s);

if (!quickStartSection) {
throw new Error('Can not find Quick Start section');
}

const tsParts = quickStartSection[1].match(/```typescript(.+?)```/gis);

if (!tsParts) {
throw new Error('Can not find typescript code samples');
}

const quickStart = tsParts.map(
(part) => part.split('\n').slice(1,-1).join('\n')
).join('\n\n');

const dir = './tests/integration';
fs.writeFileSync(`${dir}/package.json`, packageJson.trim());
fs.writeFileSync(`${dir}/tsconfig.json`, tsConfigJson.trim());
fs.writeFileSync(`${dir}/quick-start.ts`, quickStart.trim());