Servidor Local
Vamos criar um servidor simples:
- 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'),
});
E agora, teste-o:
- test/server.test.js
import { assert, startService } from 'poku';
const server = await startService('server.js', {
// Aguarde pelo "ready" na saída do console
startAfter: 'ready',
});
// Use a biblioteca de requisições que preferir
const res = await fetch('http://localhost:4000');
const data = await res.json();
assert.strictEqual(res.status, 200, 'O servidor está ativo');
assert.deepStrictEqual(data, { name: 'Poku' }, 'O Poku está aqui');
server.end();
Precisa testar usando uma sessão consistente? 🍪
Apenas faça isso 🚀
Aqui está um exemplo usando 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' }, 'O Poku está aqui');
dica
Você também pode iniciar seu servidor usando o 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', {
// Aguarde pelo "ready" na saída do console
startAfter: 'ready',
});
// Use a biblioteca de requisições que você preferir.
const res = await fetch('http://localhost:4000');
const data = await res.json();
assert.strictEqual(res.status, 200, 'O servidor está ativo');
assert.deepStrictEqual(data, { name: 'Poku' }, 'O Poku está aqui');
server.end();
{
"script": {
"start": "node server.js"
}
}
{
"script": {
"start": "bun server.js"
}
}
{
"tasks": {
"start": "deno run --allow-net server.js"
}
}