Skip to main content
Version: v4.x.x

Waiting For Expected Results

Similar to assert, but instead of returning an error when comparing, it will retry until success or timeout.

Wait for external services to be ready, or a specific result from a method before starting the tests.

waitForExpectedResult​

import { waitForExpectedResult } from 'poku';

await waitForExpectedResult(() => true, true, {
delay: 0,
interval: 100,
timeout: 60000,
strict: false,
});

Options
export type WaitForExpectedResultOptions = {
/**
* Retry interval in milliseconds
*
* ---
*
* @default 100
*/
interval?: number;
/**
* Timeout in milliseconds
*
* ---
*
* @default 60000
*/
timeout?: number;
/**
* Delays both the start and end by the defined milliseconds.
*
* ---
*
* @default 0
*/
delay?: number;
/**
* Ensure strict comparisons.
*
* - For **Bun** users, this option isn't necessary.
*
* ---
*
* @default false
*/
strict?: boolean;
};

Examples​

Waiting for a Database Connection Returning true:

import { waitForExpectedResult } from 'poku';
import { db } from './db.js';

await waitForExpectedResult(() => db.connect(), true);
// await waitForExpectedResult(async () => await db.connect(), true);

Waiting for a Database Connection doesn't Throw:

import { waitForExpectedResult } from 'poku';
import { db } from './db.js';

await waitForExpectedResult(async () => {
try {
await db.connect();
return true;
} catch {}
}, true);

Waiting for a database connection from a container before running the entire test suite and stopping it on finishing, using a poku.config.js with an anonymous plugin:

import { defineConfig, waitForExpectedResult } from 'poku';
import { docker } from '@pokujs/docker';
import { db } from './db.js';

const compose = docker.compose();

export default defineConfig({
include: './test/integration',
plugins: [
{
setup: async () => {
await compose.up();

await waitForExpectedResult(async () => {
try {
await db.connect();
return true;
} catch {}
}, true);
},
teardown: () => compose.down(),
},
],
});
npx poku