prepare for tests 💚

This commit is contained in:
Jannis R 2022-04-03 13:44:22 +02:00
parent 97b1c10d7c
commit 622fcfbf70
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
5 changed files with 83 additions and 2 deletions

View file

@ -4,8 +4,21 @@ on:
branches: branches:
- 5 - 5
jobs: jobs:
lint-test:
name: lint & test
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: setup Node
uses: actions/setup-node@v1
with:
node-version: 16.x
- run: npm install
- run: npm test
build-and-publish: build-and-publish:
name: build & publish Docker image name: build & publish Docker image
needs: [lint-test]
runs-on: ubuntu-latest runs-on: ubuntu-latest
steps: steps:
- name: check out the repo - name: check out the repo

1
api.js
View file

@ -71,6 +71,7 @@ const api = createApi(hafas, config, (api) => {
}) })
module.exports = { module.exports = {
hafas,
config, config,
api, api,
} }

View file

@ -36,11 +36,16 @@
}, },
"devDependencies": { "devDependencies": {
"@derhuerst/technical-docs-cli": "^1.1.0", "@derhuerst/technical-docs-cli": "^1.1.0",
"pino-pretty": "^4.0.0" "axios": "^0.26.1",
"get-port": "^5.1.1",
"pino-pretty": "^4.0.0",
"tap-min": "^2.0.0",
"tape": "^5.5.2"
}, },
"scripts": { "scripts": {
"docs": "node api-docs.js >docs/api.md && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/readme.md >docs/index.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/getting-started.md >docs/getting-started.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/api.md >docs/api.html && build-technical-doc --syntax-stylesheet github >docs/syntax.css", "docs": "node api-docs.js >docs/api.md && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/readme.md >docs/index.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/getting-started.md >docs/getting-started.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/api.md >docs/api.html && build-technical-doc --syntax-stylesheet github >docs/syntax.css",
"build": "npm run docs", "build": "npm run docs",
"start": "node index.js" "start": "node index.js",
"test": "node test.js | tap-min"
} }
} }

6
test/index.js Normal file
View file

@ -0,0 +1,6 @@
'use strict'
const tape = require('tape')
const {fetchWithTestApi} = require('./util')
// todo

56
test/util.js Normal file
View file

@ -0,0 +1,56 @@
'use strict'
const createApi = require('hafas-rest-api')
const getPort = require('get-port')
const {createServer} = require('http')
const {promisify} = require('util')
const axios = require('axios')
const pkg = require('../package.json')
const {config, hafas: unmockedHafas} = require('../api')
// adapted from https://github.com/public-transport/hafas-rest-api/blob/60335eacd8332d7f448da875a7498dd97934e360/test/util.js#L40-L77
const createTestApi = async (mocks, cfg) => {
const mockedHafas = Object.assign(Object.create(unmockedHafas), mocks)
cfg = {
...config,
hostname: 'localhost',
name: 'test',
version: '1.2.3a',
homepage: 'http://example.org',
description: 'test API',
docsLink: 'https://example.org',
logging: false,
...cfg,
}
const api = createApi(mockedHafas, cfg, () => {})
const server = createServer(api)
const port = await getPort()
await promisify(server.listen.bind(server))(port)
const stop = () => promisify(server.close.bind(server))()
const fetch = (path, opt = {}) => {
opt = Object.assign({
method: 'get',
baseURL: `http://localhost:${port}/`,
url: path,
timeout: 5000
}, opt)
return axios(opt)
}
return {stop, fetch}
}
const fetchWithTestApi = async (mocks, cfg, path, opt = {}) => {
const {fetch, stop} = await createTestApi(mocks, cfg)
const res = await fetch(path, opt)
await stop()
return res
}
module.exports = {
createTestApi,
fetchWithTestApi,
}