Registry / testing / jest-after-this

jest-after-this

JSON →
library1.0.4jsnpmunverified

jest-after-this is a Jest extension providing a new lifecycle hook, `afterThis`, which schedules code to run dynamically after the current test completes. Unlike `afterEach`, `afterThis` can be called multiple times within a single test to manage specific, unique side effects, ensuring cleanup even if the test fails. It is currently at version 1.0.4, with a recent release (v1.0.2) focused on release tooling, suggesting a maintenance or active, but not rapid, release cadence. Its key differentiator is the ability to tie cleanup directly to the test that created the side effect, promoting self-cleaning test helpers and reducing the complexity of shared `afterEach` hooks for diverse test scenarios. Handlers are executed in reverse order of registration and support asynchronous operations.

npm install jest-after-this
INSTALL
IMPORT
SIG · JEST-AFTER-THIS
J
jest-after-this
testingjavascriptv1.0.4
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

afterThis
import { afterThis } from 'jest-after-this';
const { afterThis } = require('jest-after-this');
Primarily designed for ESM usage in modern Jest setups, though CommonJS `require` might work with transpilation, direct ESM import is preferred.

Demonstrates creating a self-cleaning helper function that uses `afterThis` to manage temporary files for specific tests, ensuring proper cleanup after each test, even with multiple file creations.

import { afterThis } from 'jest-after-this'; import fs from 'fs'; import path from 'path'; // A helper function that creates a temporary file and schedules its cleanup function createTempFileForTest(filename: string) { const filePath = path.join(__dirname, 'temp', filename); if (!fs.existsSync(path.dirname(filePath))) { fs.mkdirSync(path.dirname(filePath), { recursive: true }); } fs.writeFileSync(filePath, `Content for ${filename}`); afterThis(() => { try { fs.rmSync(filePath); console.log(`Cleaned up: ${filePath}`); } catch (error) { console.error(`Failed to clean up ${filePath}:`, error.message); } }); return filePath; } describe('File operations', () => { it('should create one temporary file and clean it up', () => { const file1 = createTempFileForTest('test-file-1.txt'); expect(fs.existsSync(file1)).toBe(true); // After this test, file1 will be deleted by the afterThis hook }); it('should create multiple temporary files and clean them up', () => { const file2 = createTempFileForTest('test-file-2.txt'); const file3 = createTempFileForTest('test-file-3.txt'); expect(fs.existsSync(file2)).toBe(true); expect(fs.existsSync(file3)).toBe(true); // After this test, file3 then file2 will be deleted by their respective afterThis hooks }); afterAll(() => { // Ensure the temp directory itself is clean after all tests const tempDir = path.join(__dirname, 'temp'); if (fs.existsSync(tempDir)) { fs.rmSync(tempDir, { recursive: true, force: true }); console.log(`Cleaned up temp directory: ${tempDir}`); } }); });
Debug
Known issues
gotchaHandlers registered with `afterThis` execute in reverse order of their registration within a test. The last `afterThis` call's handler will run first.
fix
Be mindful of handler order if dependencies exist between cleanup actions. If specific order is crucial, register dependent cleanups first.
affects: >=1.0.0
gotcha`afterThis` has a lower priority than `afterEach` hooks. This means all `afterEach` handlers will complete before the first `afterThis` handler begins execution.
fix
Consider what types of cleanup belong in `afterEach` (global, per-test setup teardown) versus `afterThis` (dynamic, test-specific side effect cleanup).
affects: >=1.0.0
Errors
Common errors & fixes
Error: `afterThis` can only be called from within a test function (defined using `it` or `test`).
The `afterThis` function was invoked outside the scope of an `it` or `test` block, for example, directly in a `describe` block or a global context.
fix
Ensure all calls to `afterThis` are made inside the callback function provided to `it()` or `test()`.
Upgrade
Version history
1.0.4latest on npm
Audit
Dependencies
jestrequiredPeer dependency as it's a Jest extension
Agent activity
6 hits · last 30 days
node
6
Resources
jest-after-this — npm install jest-after-this · libregistry