Skip to content

Instantly share code, notes, and snippets.

@papandreou
Created July 28, 2017 14:49
Show Gist options
  • Save papandreou/cef2d8e051feb7864322d416f39ad6c1 to your computer and use it in GitHub Desktop.
Save papandreou/cef2d8e051feb7864322d416f39ad6c1 to your computer and use it in GitHub Desktop.
Spin up a test server on a random port
describe('my server', function () {
let serverUrl;
beforeEach(function () => {
const server = http.createServer((req, res) => {
// ...
}).listen(0);
const serverAddress = server.address();
const serverHostname = serverAddress.address === '::' ? 'localhost' : serverAddress.address;
serverUrl = 'http://' + serverHostname + ':' + serverAddress.port + '/';
});
it('should foo', function () {
// Make request to `serverUrl` and make assertions about the response
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment