Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(processes): waitForExpectedResult helper #461

Merged
merged 3 commits into from
Jun 25, 2024
Merged

feat(processes): waitForExpectedResult helper #461

merged 3 commits into from
Jun 25, 2024

Conversation

wellwelwel
Copy link
Owner

Waiting For Expected Results

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

Wait for connections, 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,
});

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 to run the entire test suite and stops it on finishing:

Poku API (in-code) usage.

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

// Load the docker-compose.yml
const compose = docker.compose();

// Starts the container
await compose.up();

// Waits for the database
await waitForExpectedResult(async () => {
  try {
    await db.connect();
    return true;
  } catch {}
}, true);

// Starts the test suite
const result = await poku('./test/integration', {
  noExit: true,
});

// Stops the container
await compose.down();

// Shows the test results and ends the process with the test exit code.
exit(result);
node run.test.mjs

@wellwelwel wellwelwel marked this pull request as ready for review June 25, 2024 06:08
Copy link

codecov bot commented Jun 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.36%. Comparing base (720f61a) to head (1de7f8e).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #461      +/-   ##
==========================================
+ Coverage   99.35%   99.36%   +0.01%     
==========================================
  Files          33       33              
  Lines        3243     3294      +51     
  Branches      552      519      -33     
==========================================
+ Hits         3222     3273      +51     
  Misses         20       20              
  Partials        1        1              
Flag Coverage Δ
linux-parallel 99.18% <100.00%> (+0.01%) ⬆️
linux-parallel-options 99.30% <100.00%> (+0.01%) ⬆️
linux-sequential ?
linux-sequential-options ?
osx-parallel 94.93% <100.00%> (+0.07%) ⬆️
osx-parallel-options 95.05% <100.00%> (+0.07%) ⬆️
osx-sequential 93.83% <100.00%> (+0.09%) ⬆️
osx-sequential-options 93.92% <100.00%> (+0.09%) ⬆️
windows-parallel 94.38% <100.00%> (+0.08%) ⬆️
windows-parallel-options ?
windows-sequential ?
windows-sequential-options 93.38% <100.00%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@wellwelwel wellwelwel merged commit 7e08d59 into main Jun 25, 2024
39 checks passed
@wellwelwel wellwelwel deleted the wait branch June 25, 2024 06:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant