Skip to content

feat: support onTestFinished hook#493

Merged
9aoy merged 1 commit intomainfrom
onTestFinished
Aug 21, 2025
Merged

feat: support onTestFinished hook#493
9aoy merged 1 commit intomainfrom
onTestFinished

Conversation

@9aoy
Copy link
Copy Markdown
Collaborator

@9aoy 9aoy commented Aug 21, 2025

Summary

onTestFinished hook will called after the test has finished running whatever the test result is. This can be used to perform cleanup actions. This hook will be called after afterEach.

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

test('test server', () => {
  const server = startServer();
  // Register a cleanup function to close the server after the test
  onTestFinished(() => server.close());

  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 21, 2025 07:28
@netlify
Copy link
Copy Markdown

netlify bot commented Aug 21, 2025

Deploy Preview for rstest-dev ready!

Name Link
🔨 Latest commit c0182cd
🔍 Latest deploy log https://app.netlify.com/projects/rstest-dev/deploys/68a6cab476e9bd0007cd0e8d
😎 Deploy Preview https://deploy-preview-493--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 introduces a new onTestFinished hook that executes after test completion regardless of test result, providing a mechanism for cleanup actions. The hook runs after afterEach hooks and supports both synchronous and asynchronous operations with timeout configuration.

Key changes:

  • Added onTestFinished API to the test framework with timeout support
  • Implemented proper execution order (after afterEach hooks)
  • Added comprehensive test coverage for success, failure, and error scenarios

Reviewed Changes

Copilot reviewed 11 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 the OnTestFinishedHandler type and adds onTestFinished to the RunnerAPI interface
packages/core/src/runtime/runner/runner.ts Core implementation of the onTestFinished functionality with timeout wrapping and execution logic
packages/core/src/runtime/runner/runtime.ts Updates the runtime API type to exclude onTestFinished from the base implementation
packages/core/src/runtime/runner/index.ts Integrates onTestFinished into the final runner API by forwarding calls to the TestRunner instance
packages/core/src/runtime/api/public.ts Exports the onTestFinished function for public API consumption
website/docs/en/api/test-api/hooks.mdx English documentation for the new hook with usage examples
website/docs/zh/api/test-api/hooks.mdx Chinese documentation for the new hook with usage examples
e2e/lifecycle/onTestFinished.test.ts Comprehensive end-to-end tests covering execution order, error handling, and validation
e2e/lifecycle/fixtures/*.test.ts Test fixtures demonstrating various scenarios including success, failure, and misuse cases

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


test('test server', () => {
const server = startServer();
// Register a cleanup function to close the server after the test
Copy link

Copilot AI Aug 21, 2025

Choose a reason for hiding this comment

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

The comment is in English while the documentation is in Chinese. Consider translating the comment to Chinese for consistency: // 注册清理函数在测试后关闭服务器

Suggested change
// Register a cleanup function to close the server after the test
// 注册清理函数在测试后关闭服务器

Copilot uses AI. Check for mistakes.
@9aoy 9aoy merged commit f21ce81 into main Aug 21, 2025
17 checks passed
@9aoy 9aoy deleted the onTestFinished branch August 21, 2025 07:41
@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