
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
fetch-test-server
Advanced tools
This package allows you to easily run your Node.js server for integration testing, and interact with it using the Fetch API. It is similar to SuperTest, but using the Fetch API means that you can take advantage of promises, and newer ES2017 features like async/await.
npm install --save-dev fetch-test-server
Create a new instance of TestServer, passing in your HTTP server. You can then call fetch() to make requests against it. This example uses Mocha (which natively supports promises), but you can use any test framework you like.
import { assert } from 'chai';
import app from './myapp';
const server = new TestServer(app);
describe('API Integration Test', () => {
it('responds to /user', () => {
return server.fetch('/user').then((res) => {
assert.strictEqual(res.status, 200);
return res.json();
}).then((body) => {
assert.strictEqual(body.name, 'Adrian');
});
});
});
Using async/await (currently requires Babel or another transpiler):
import { assert } from 'chai';
import app from './myapp';
const server = new TestServer(app);
describe('API Integration Test', () => {
it('responds to /user', async () => {
const res = await server.fetch('/user');
const body = await res.json();
assert.strictEqual(res.status, 200);
assert.strictEqual(body.name, 'Adrian');
});
});
Behind the scenes, it uses node-fetch to implement the Fetch API. The server listens on a random port, and does not start listening until you first call fetch(). Your requests will be automatically held until the server is available.
You can also use helper methods to call common HTTP verbs:
server.head('/path');
server.get('/path');
server.post('/path');
server.put('/path');
server.patch('/path');
server.delete('/path');
server.options('/path');
Per the Fetch API, you can customize the request with an optional second parameter:
server.post('/users', {
headers: { authorization: 'supersecret' },
body: 'name=adrian',
});
Finally, if you pass an object as the body parameter, it will automatically be encoded as JSON and sent with a Content-Type: application/json header:
server.post('/users', {
headers: { authorization: 'supersecret' },
body: { name: 'Adrian' },
});
This is equivalent to body: JSON.stringify({ name: 'adrian' })
If you need the URL of your test server, use server.address:
server.listen().then(() => {
// server is listening
console.log(server.address);
});
If you want to stop the HTTP server, simply call server.close():
server.listen().then(() => {
// server is listening
return server.close();
}).then(() => {
// server is now stopped
});
Fetch Test Server works with any Node.js HTTP framework.
Express
import app from './expressapp';
const server = new TestServer(app);
Koa
import app from './koaapp';
const server = new TestServer(app.callback());
FAQs
Test node.js HTTP servers using the fetch API
The npm package fetch-test-server receives a total of 896 weekly downloads. As such, fetch-test-server popularity was classified as not popular.
We found that fetch-test-server demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.