๐ Start Service
Run your services in a background process and test them ๐ง๐ปโ๐ฌ
startService(filePath: string, options?: StartServiceOptions);
startService
execute a file directly and keep it running in a background process until you release it.
Poku allows you to use the requester you want, such as Axios, the
native fetch
and everyone โจ
No need to export your app, server or any service, just run it as it is ๐
Stop changing your code due to tester rules, you created your code, not Poku ๐ฉต
See practical examples using fetch, Axios, a persistent session and more.
Good Practices ๐ฎ๐ฝโ
โ Sinalize the "ready" statusโ
When possible, set a console output to indicate that the service is ready.
This will allow you to avoid premature executions and port leaks.
โ Set a timeoutโ
By defining a timeout, you avert indefinite processes that conclude neither in success nor failure.
โ End your service when the job is doneโ
You don't necessarily have to end your service running in the background as the last line of the test, if it's no longer used.
import { startService } from 'poku';
const server = await startService('server.js', {
/**
* Wait for the "ready" console output
*/
startAfter: 'ready',
/**
* By default, the `timeout` is `60000` (1 minute) for neither success nor failure
*/
timeout: 60000,
});
await server.end();
You can pass a port to end
to enforce the background subprocess end for Bun and Deno.
โน๏ธ To use a port in end
, you will need lsof
for Unix or netstat
for Windows.
If you're encountering issues using this feature with Bun, please see oven-sh/bun#11055 when using end
method passing a port or oven-sh/bun#7441 for unfinished subprocesses.
Oh No! I broke it, and now? ๐คก
Releasing a leaking port:
lsof -i :PORT
- Replace
PORT
with the port number you're investigating.
Then, use the PID
returned with the kill
command:
kill -9 PID
- Replace
PID
with the actual process ID you found using thelsof
command.
Available Optionsโ
type StartServiceOptions = {
/**
* - By default, it will resolve in the first console output
* - By setting a string: it will wait for a specifc string on console output to resolve
* - By setting a number: it will wait for time in milliseconds to resolve
*
* ---
*
* โน๏ธ `startAfter` is case sensitive.
*
* ---
*
* @default undefined
*/
startAfter?: string | number;
/**
* Stops the service for neither success nor failure after:
* @default 60000
*/
timeout?: number;
/**
* Shows the output from service
*/
verbose?: boolean;
/**
* Specify a target path to start the process
*
* @default "./"
*/
cwd?: string | undefined;
/**
* By default, Poku will try to identify the actual platform, but you can specify it manually
*/
platform?: 'node' | 'bun' | 'deno';
};
type End = (port?: number | number[]) => Promise<void>;