diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 283a2d7..ae28707 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -4,8 +4,21 @@ on: branches: - 5 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: name: build & publish Docker image + needs: [lint-test] runs-on: ubuntu-latest steps: - name: check out the repo diff --git a/api.js b/api.js index cb0c5c9..7118766 100644 --- a/api.js +++ b/api.js @@ -71,6 +71,7 @@ const api = createApi(hafas, config, (api) => { }) module.exports = { + hafas, config, api, } diff --git a/package.json b/package.json index e4b838e..cf9e277 100644 --- a/package.json +++ b/package.json @@ -36,11 +36,16 @@ }, "devDependencies": { "@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": { "docs": "node api-docs.js >docs/api.md && build-technical-doc --syntax-stylesheet-url /syntax.css docs/index.html && build-technical-doc --syntax-stylesheet-url /syntax.css docs/getting-started.html && build-technical-doc --syntax-stylesheet-url /syntax.css docs/api.html && build-technical-doc --syntax-stylesheet github >docs/syntax.css", "build": "npm run docs", - "start": "node index.js" + "start": "node index.js", + "test": "node test.js | tap-min" } } diff --git a/test/index.js b/test/index.js new file mode 100644 index 0000000..c309e07 --- /dev/null +++ b/test/index.js @@ -0,0 +1,6 @@ +'use strict' + +const tape = require('tape') +const {fetchWithTestApi} = require('./util') + +// todo diff --git a/test/util.js b/test/util.js new file mode 100644 index 0000000..3498ee2 --- /dev/null +++ b/test/util.js @@ -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, +}