Skip to content

feat: support onTestFailed hook#501

Merged
9aoy merged 2 commits intomainfrom
onTestFailed
Aug 22, 2025
Merged

feat: support onTestFailed hook#501
9aoy merged 2 commits intomainfrom
onTestFailed

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Aug 22, 2025

Summary

onTestFailed hook will be called after the test has failed.

import { onTestFailed, test } from '@rstest/core';

test('test server', () => {
  const server = startServer();

  onTestFailed(({ task }) => {
    console.log(task.result.errors);
  });

  server.listen(3000, () => {
    console.log('Server is running on port 3000');
  });
});

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings August 22, 2025 07:09
@netlify
Copy link
Copy Markdown

netlify bot commented Aug 22, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit f896cb9
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/68a817a3b5ed730008d5da25
😎 Deploy Preview https://deploy-preview-501--rstest-dev.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds support for an onTestFailed hook that executes after a test has failed, providing access to test results and errors for cleanup or logging purposes.

Key changes:

  • Introduces onTestFailed hook API with consistent signature matching onTestFinished
  • Updates existing hook signatures to include test result context
  • Adds comprehensive documentation and test coverage

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
packages/core/src/types/api.ts Defines OnTestFailedHandler type and adds hook to RunnerAPI and TestContext
packages/core/src/types/testSuite.ts Updates type signatures for hooks to include test result context
packages/core/src/runtime/runner/runner.ts Implements core logic for onTestFailed hook execution and context binding
packages/core/src/runtime/runner/runtime.ts Initializes onFailed array in test case creation
packages/core/src/runtime/runner/index.ts Exposes onTestFailed in runner API
packages/core/src/runtime/api/public.ts Exports onTestFailed for public API
e2e/lifecycle/onTestFailed.test.ts End-to-end test verifying hook execution on test failure
e2e/lifecycle/fixtures/onTestFailed.test.ts Test fixture demonstrating hook usage
website/docs/en/api/test-api/hooks.mdx English documentation for the new hook
website/docs/zh/api/test-api/hooks.mdx Chinese documentation for the new hook

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

try {
for (const fn of afterEachFns) {
await fn();
await fn({ task: { result } });
Copy link

Copilot AI Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change breaks backward compatibility for existing afterEach hooks. The original signature was () => MaybePromise<void>, but now it requires a parameter. Existing code that uses afterEach(() => {}) will fail at runtime.

Suggested change
await fn({ task: { result } });
if (fn.length === 0) {
await fn();
} else {
await fn({ task: { result } });
}

Copilot uses AI. Check for mistakes.
@9aoy 9aoy merged commit e31695b into main Aug 22, 2025
17 checks passed
@9aoy 9aoy deleted the onTestFailed branch August 22, 2025 07:35
@9aoy 9aoy mentioned this pull request Aug 27, 2025
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.

2 participants