Skip to content

t.skip does not invoke test.afterEach #61462

@jsumners-nr

Description

@jsumners-nr
'use strict'

const test = require('node:test')

test.afterEach(() => {
  process._rawDebug('!!! afterEach')
})

test('one', (t) => {
  t.assert.ok('one is good')
})

test('two', (t) => {
  if (process.env.TEST_SKIP === 'false') {
    t.assert.ok('two is good')
  } else {
    t.skip('two gets skipped after some logic')
  }
})

I expect that !!! afterEach will be printed twice when this test suite is run. However, on Node.js 20.20.0 and 24.13.0, that is not the case. Instead, the result is:

❯ node index.js
!!! afterEach
✔ one (1.120209ms)
﹣ two (0.08625ms) # two gets skipped after some logic
ℹ tests 2
ℹ suites 0
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 1
ℹ todo 0
ℹ duration_ms 5.285417

This is a problem when the test being skipped depends upon logic internal to the test. As an example, we may have a test setup like:

test.beforeEach((ctx) => {
  ctx.testNamespace = {
    foo: 'bar'
    // other more complicated bootstrapping
  }
})

test.afterEach((ctx) => {
  cleanUp(ctx.testNamespace.somethingThatNeedsCleaningAfterEachTest)
})

test('something', (t) => {
  const { foo } = t.testNamespace
  if (foo !== 'bar') return t.skip('reason')
  t.assert.ok('whatever')
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions