Skip to main content
Version: v2.x.x

Local Server

Let's create a simple server:

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'));

And now, test it:

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.

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();
tip

Using startScript, you can execute NextJS, SvelteJS, nodemon and all the scripts you've always used 🐷