{"id":13799860,"url":"https://github.com/lukechilds/create-test-server","last_synced_at":"2025-10-30T10:33:38.768Z","repository":{"id":56824106,"uuid":"93131558","full_name":"lukechilds/create-test-server","owner":"lukechilds","description":"Creates a minimal Express server for testing","archived":false,"fork":false,"pushed_at":"2019-10-10T23:25:54.000Z","size":194,"stargazers_count":119,"open_issues_count":11,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T16:11:14.475Z","etag":null,"topics":["server","test","test-server","testing","tests"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/lukechilds.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-02T05:50:31.000Z","updated_at":"2025-02-28T03:02:48.000Z","dependencies_parsed_at":"2022-09-01T13:02:24.620Z","dependency_job_id":null,"html_url":"https://github.com/lukechilds/create-test-server","commit_stats":null,"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fcreate-test-server","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fcreate-test-server/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fcreate-test-server/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lukechilds%2Fcreate-test-server/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lukechilds","download_url":"https://codeload.github.com/lukechilds/create-test-server/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248036063,"owners_count":21037092,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["server","test","test-server","testing","tests"],"created_at":"2024-08-04T00:01:06.699Z","updated_at":"2025-10-30T10:33:38.690Z","avatar_url":"https://github.com/lukechilds.png","language":"JavaScript","funding_links":[],"categories":["Works with AVA"],"sub_categories":[],"readme":"# create-test-server\n\n\u003e Creates a minimal Express server for testing\n\n[![Build Status](https://travis-ci.org/lukechilds/create-test-server.svg?branch=master)](https://travis-ci.org/lukechilds/create-test-server)\n[![Coverage Status](https://coveralls.io/repos/github/lukechilds/create-test-server/badge.svg?branch=master)](https://coveralls.io/github/lukechilds/create-test-server?branch=master)\n[![npm](https://img.shields.io/npm/dm/create-test-server.svg)](https://www.npmjs.com/package/create-test-server)\n[![npm](https://img.shields.io/npm/v/create-test-server.svg)](https://www.npmjs.com/package/create-test-server)\n\nInspired by the `createServer()` helper function in the [Got tests](https://github.com/sindresorhus/got/blob/1f1b6ffb6da13f483ef7f6bd92dd33f022e7de47/test/helpers/server.js).\n\nA simple interface for creating a preconfigured Express instance listening for both HTTP and HTTPS traffic.\n\nPorts are chosen at random for HTTP/HTTPS. A self signed certificate is automatically generated, along with an associated CA certificate for you to validate against.\n\nCreated because mocking is dirty and can [break between Node.js releases](https://github.com/node-nock/nock/issues/922). Why mock HTTP requests when you can test locally against a real server in a few lines code?\n\n## Install\n\n```shell\nnpm install --save-dev create-test-server\n```\n\n## Usage\n\n```js\nconst createTestServer = require('create-test-server');\n\nconst server = await createTestServer();\nconsole.log(server.url);\n// http://localhost:5486\nconsole.log(server.sslUrl);\n// https://localhost:5487\n\n// This is just an Express route\n// You could use any Express middleware too\nserver.get('/foo', (req, res) =\u003e {\n  res.send('bar');\n});\n\n// You can return a body directly too\nserver.get('/foo', () =\u003e 'bar');\nserver.get('/foo', 'bar');\n\n// server.url + '/foo' and server.sslUrl + '/foo' will respond with 'bar'\n```\n\nThe following `Content-Type` headers will be parsed and exposed via `req.body`:\n\n- JSON (`application/json`)\n- Text (`text/plain`)\n- URL-encoded form (`application/x-www-form-urlencoded`)\n- Buffer (`application/octet-stream`)\n\nYou can change body parsing behaviour with the [`bodyParser`](#optionsbodyparser) option.\n\n`createTestServer()` has a Promise based API that pairs well with a modern asynchronous test runner such as [AVA](https://github.com/avajs/ava).\n\nYou can create a separate server per test:\n\n```js\nimport test from 'ava';\nimport got from 'got';\nimport createTestServer from 'create-test-server';\n\ntest(async t =\u003e {\n  const server = await createTestServer();\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.url}/foo`);\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nOr share a server across multiple tests:\n\n```js\nlet server;\n\ntest.before(async () =\u003e {\n  server = await createTestServer();\n  server.get('/foo', 'bar');\n});\n\ntest(async t =\u003e {\n  const response = await got(`${server.url}/foo`);\n  t.is(response.body, 'bar');\n});\n\ntest(async t =\u003e {\n  const response = await got(`${server.url}/foo`);\n  t.is(response.statusCode, 200);\n});\n\ntest.after(async () =\u003e {\n\tawait server.close();\n});\n```\n\nYou can also make properly authenticated SSL requests by setting a common name for the server certificate and validating against the provided CA certificate:\n\n```js\ntest(async t =\u003e {\n  const server = await createTestServer({ certificate: 'foobar.com' });\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.sslUrl}/foo`, {\n    ca: server.caCert,\n    headers: { host: 'foobar.com' }\n  });\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nYou can still make an SSL connection without messing about with certificates if your client supports unauthorised SSL requests:\n\n```js\ntest(async t =\u003e {\n  const server = await createTestServer();\n  server.get('/foo', 'bar');\n\n  const response = await got(`${server.sslUrl}/foo`, {\n    rejectUnauthorized: false\n  });\n  t.is(response.body, 'bar');\n\n  await server.close();\n});\n```\n\nYou can also easily stop/restart the server. Notice how a new port is used when we listen again:\n\n```js\nconst server = await createTestServer();\nconsole.log(server.port);\n// 56711\n\nawait server.close();\nconsole.log(server.port);\n// undefined\n\nawait server.listen();\nconsole.log(server.port);\n// 56804\n```\n\n## API\n\n### createTestServer([options])\n\nReturns a Promise which resolves to an (already listening) server.\n\n#### options\n\nType: `object`\n\n##### options.certificate\n\nType: `string`, `object`\u003cbr\u003e\nDefault: `undefined`\n\nSSL certificate options to be passed to [`createCert()`](https://github.com/lukechilds/create-cert).\n\n##### options.bodyParser\n\nType: `object | boolean`\u003cbr\u003e\nDefault: `undefined`\n\nBody parser options object to be passed to [`body-parser`](https://github.com/expressjs/body-parser) methods.\n\nIf set to `false` then all body parsing middleware will be disabled.\n\n### server\n\nExpress instance resolved from `createTestServer()`\n\nThis is just a normal Express instance with a few extra properties.\n\n#### server.url\n\nType: `string`, `undefined`\n\nThe url you can reach the HTTP server on.\n\ne.g: `'http://localhost:5486'`\n\n`undefined` while the server is not listening.\n\n#### server.port\n\nType: `number`, `undefined`\n\nThe port number you can reach the HTTP server on.\n\ne.g: `5486`\n\n`undefined` while the server is not listening.\n\n#### server.sslUrl\n\nType: `string`, `undefined`\n\nThe url you can reach the HTTPS server on.\n\ne.g: `'https://localhost:5487'`\n\n`undefined` while the server is not listening.\n\n#### server.sslPort\n\nType: `number`, `undefined`\n\nThe port number you can reach the HTTPS server on.\n\ne.g: `5487`\n\n`undefined` while the server is not listening.\n\n#### server.caCert\n\nType: `string`\n\nThe CA certificate to validate the server certificate against.\n\n#### server.http\n\nType: [`http.server`](https://nodejs.org/api/http.html#http_class_http_server)\n\nThe underlying HTTP server instance.\n\n#### server.https\n\nType: [`https.server`](https://nodejs.org/api/https.html#https_class_https_server)\n\nThe underlying HTTPS server instance.\n\n#### server.listen()\n\nType: `function`\n\nReturns a Promise that resolves when both the HTTP and HTTPS servers are listening.\n\nOnce the servers are listening, `server.url` and `server.sslUrl` will be updated.\n\nPlease note, this function doesn't take a port argument, it uses a new randomised port each time. Also, you don't need to manually call this after creating a server, it will start listening automatically.\n\n#### server.close()\n\nType: `function`\n\nReturns a Promise that resolves when both the HTTP and HTTPS servers have stopped listening.\n\nOnce the servers have stopped listening, `server.url` and `server.sslUrl` will be set to `undefined`.\n\n## Related\n\n- [`create-cert`](https://github.com/lukechilds/create-cert) - Super simple self signed certificates\n\n## License\n\nMIT © Luke Childs\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukechilds%2Fcreate-test-server","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flukechilds%2Fcreate-test-server","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flukechilds%2Fcreate-test-server/lists"}