Local Server
Let's create a simple server:
- server.js (Node.js)
- server.js (Bun)
- server.js (Deno)
import { createServer } from 'node:http';
createServer((_, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ name: 'Poku' }));
}).listen(4000, () => console.log('ready'));
Bun.serve({
port: 4000,
fetch: () =>
new Response(JSON.stringify({ name: 'Poku' }), {
headers: { 'Content-Type': 'application/json' },
}),
});
console.log('ready');
Deno.serve({
port: 4000,
handler: () =>
new Response(JSON.stringify({ name: 'Poku' }), {
headers: { 'Content-Type': 'application/json' },
}),
onListen: () => console.log('ready'),
});
And now, test it:
- test/server.test.js
import { assert, startService } from 'poku';
const server = await startService('server.js', {
// Wait for the "ready" console output
startAfter: 'ready',
});
// Use the requester you want
const res = await fetch('http://localhost:4000');
const data = await res.json();
assert.strictEqual(res.status, 200, 'Server is on');
assert.deepStrictEqual(data, { name: 'Poku' }, 'Poku is here');
server.end();
Need to test using a consistent session? πͺ
Just do it π
Here's an example using Axios:
import { assert, startService } from 'poku';
import axios from 'axios';
import axiosCookieJarSupport from 'axios-cookiejar-support';
import { CookieJar } from 'tough-cookie';
const server = await startService('server.js');
axiosCookieJarSupport(axios);
const cookieJar = new CookieJar();
export const api = axios.create({
withCredentials: true,
jar: cookieJar,
});
const { data } = await api.get('http://localhost:4000');
assert.deepStrictEqual(data, { name: 'Poku' }, 'Poku is here');
tip
You also can start your server using the startScript
.
- test/server.test.js
- package.json (Node.js)
- package.json (Bun)
- deno.json (Deno)
import { assert, startScript } from 'poku';
const server = await startScript('start', {
// Wait for the "ready" console output
startAfter: 'ready',
});
// Use the requester you want
const res = await fetch('http://localhost:4000');
const data = await res.json();
assert.strictEqual(res.status, 200, 'Server is on');
assert.deepStrictEqual(data, { name: 'Poku' }, 'Poku is here');
server.end();
{
"script": {
"start": "node server.js"
}
}
{
"script": {
"start": "bun server.js"
}
}
{
"tasks": {
"start": "deno run --allow-net server.js"
}
}