From 069ac9e6900b83b90840a8c0894192ead2b802c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pir=C3=B3g?= <69601940+aetn23@users.noreply.github.com> Date: Thu, 23 Mar 2023 15:21:07 +0100 Subject: [PATCH] Supertest setup (#24) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Setup supertest to test http endpoints. Split `index.ts` to `server.ts` with `listen` and `app.ts` with endpoints. This is done, so testing of endpoints will be possible without calling `listen` (that way supertest can assign its own port, also no need to worry about closing down the server). It turn out that imports of `ts` files need to end with `.js`, otherwise they will not work after `tsc`. See `server.ts` --------- Co-authored-by: Karol WÄ…sowski --- .github/workflows/test.yml | 2 -- package.json | 6 ++++-- src/app.ts | 8 ++++++++ src/server.ts | 5 +++++ src/tests/app.test.ts | 12 ++++++++++++ src/tests/index.test.ts | 6 ------ 6 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 src/app.ts create mode 100644 src/server.ts create mode 100644 src/tests/app.test.ts delete mode 100644 src/tests/index.test.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c8b8855..c5f99ac 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -36,8 +36,6 @@ jobs: - name: Start server run: | yarn install - yarn start & - sleep 10 yarn test - name: Upload coverage reports to Codecov env: diff --git a/package.json b/package.json index 9540dc4..7545f45 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "type": "module", "license": "MIT", "scripts": { - "start": "tsc && node dist/index.js", + "start": "tsc && node dist/server.js", "build": "tsc", "test": "jest src/tests --coverage --config package.json", "lint": "eslint --ext '.js,.ts,.tsx' src/", @@ -26,6 +26,7 @@ "eslint-plugin-promise": "^6.0.0", "husky": "^8.0.0", "prettier": "2.8.4", + "supertest": "^6.3.3", "ts-jest": "^29.0.5", "typescript": "4.9.5" }, @@ -39,6 +40,7 @@ "testEnvironment": "node", "collectCoverageFrom": [ "src/*.ts" - ] + ], + "preset": "ts-jest" } } diff --git a/src/app.ts b/src/app.ts new file mode 100644 index 0000000..6a2a82e --- /dev/null +++ b/src/app.ts @@ -0,0 +1,8 @@ +import express from 'express' + +export const app = express() +export const port = 42069 + +app.get('/test', (req, res) => { + res.send('Hello from typescript express!') +}) diff --git a/src/server.ts b/src/server.ts new file mode 100644 index 0000000..9aa44a4 --- /dev/null +++ b/src/server.ts @@ -0,0 +1,5 @@ +import { app, port } from './app.js' + +app.listen(port, () => { + console.log(`[server]: Server is running at localhost:${port}`) +}) diff --git a/src/tests/app.test.ts b/src/tests/app.test.ts new file mode 100644 index 0000000..441c86e --- /dev/null +++ b/src/tests/app.test.ts @@ -0,0 +1,12 @@ +import { app } from '../app' + +import request from 'supertest' + +test('Simple test', (done) => { + request(app) + .get('/test') + .expect(200) + .expect('Content-Type', 'text/html; charset=utf-8') + .expect('Hello from typescript express!') + .end(done) +}) diff --git a/src/tests/index.test.ts b/src/tests/index.test.ts deleted file mode 100644 index 2e75baf..0000000 --- a/src/tests/index.test.ts +++ /dev/null @@ -1,6 +0,0 @@ -test('Simple test', async () => { - // for some reason using port from imported varaible gives weird errors - const response = await fetch('http://localhost:42069/test') - const text = await response.text() - expect(text).toEqual('Hello from typescript express!') -})