Skip to content

Conversation

@eablack
Copy link
Contributor

@eablack eablack commented Jan 6, 2026

This PR is part of a series migrating test files from @oclif/test v2 to v4 following the package upgrade in the foundation PR. This is PR 3 of 11 in the incremental migration plan.

Descripion

Migrates 12 test files in the Configuration and Features/Labs category to be compatible with @oclif/test v4.1.15, including tests for config variable management, feature flags, and labs features. This PR introduces a special case pattern for commands with external dependencies that require the custom runCommand helper.

Files Migrated (12 total)

  • test/unit/commands/config/get.unit.test.ts (1 test)
  • test/unit/commands/config/index.unit.test.ts (6 tests)
  • test/unit/commands/config/set.unit.test.ts (4 tests)
  • test/unit/commands/config/unset.unit.test.ts (1 test)
  • test/unit/commands/config/edit.unit.test.ts (4 tests) *
  • test/unit/commands/features/disable.unit.test.ts (2 tests)
  • test/unit/commands/features/enable.unit.test.ts (2 tests)
  • test/unit/commands/features/index.unit.test.ts (3 tests)
  • test/unit/commands/features/info.unit.test.ts (2 tests)
  • test/unit/commands/labs/disable.unit.test.ts (2 tests)
  • test/unit/commands/labs/enable.unit.test.ts (2 tests)
  • test/unit/commands/labs/index.unit.test.ts (3 tests)

Total: 32 tests migrated ✅

* Special case: Uses custom runCommand helper (see notes below)

Migration Changes

Most tests were updated to use the new @oclif/test v4 API:

Before (v2):

test
  .stdout()
  .nock('https://api.heroku.com', api => api
    .get('/apps/myapp/config-vars')
    .reply(200, {FOO: 'bar'}))
  .command(['config:get', 'FOO', '--app=myapp'])
  .it('gets a config var', ctx => {
    expect(ctx.stdout).to.contain('bar')
  })

After (v4):
it('gets a config var', async () => {
  nock('https://api.heroku.com')
    .get('/apps/myapp/config-vars')
    .reply(200, {FOO: 'bar'})

  const {stdout} = await runCommand(['config:get', 'FOO', '--app=myapp'])

  expect(stdout).to.contain('bar')
})

Key Pattern Changes

- API: Replaced chaining test API with runCommand() function
- Async: Converted callback-style tests to async/await
- Context: Destructured {stdout, stderr, error} from result instead of ctx parameter
- Cleanup: Added afterEach(() => nock.cleanAll()) hooks
- Nock Setup: Moved nock setup inside each test for better isolation

Special Case: config/edit Tests

The config/edit tests required using the custom runCommand helper (from test/helpers/runCommand.ts) instead of @oclif/test's runCommand():

Pattern:
import runCommand from '../../../helpers/runCommand.js'
import {EditorFactory} from '../../../../src/lib/config/util.js'

beforeEach(function () {
  sinon.stub(EditorFactory.prototype, 'createEditor').callsFake(() => ({
    edit: () => Promise.resolve(editedConfig),
  }))
})

it('updates config values', async () => {
  await runCommand(Cmd, ['--app=myapp', 'KEY'])
  expect(updated).to.deep.equal({KEY: 'value'})
})

Why this was necessary:
- The config:edit command depends on the Editor class which spawns vim/VISUAL/EDITOR
- We need to mock the Editor to avoid opening vim during tests
- @oclif/core's internal module loader performs dynamic import() at runtime
- This dynamic import bypasses all ES module mocking attempts (sinon stubs, esmock, etc.)
- The custom helper directly instantiates the command class, allowing sinon stubs to work

This pattern is already used successfully in addons/docs.unit.test.ts for similar reasons (mocking URL opener).

Supporting refactoring:
- Added EditorFactory class in src/lib/config/util.ts with createEditor() method for testability
- Updated src/commands/config/edit.ts to use new EditorFactory().createEditor() instead of direct new Editor()

Testing

 All 32 migrated tests pass locally✅ Tests pass in CI✅ No regressions in other test suites✅ Build passes

Test Results:
- Config tests: 15 passing
- Features tests: 9 passing
- Labs tests: 7 passing
- stringToConfig utility: 1 passing

Notes

This PR demonstrates handling of commands with external dependencies that cannot be mocked using standard @oclif/test patterns. The
custom runCommand helper approach provides a reliable solution for these edge cases while maintaining test coverage.

Related

- Part of the @oclif/test v2  v4 migration plan outlined in OCLIF_TEST_UPGRADE_PR_PLAN.md
- Follows foundation PR: https://github.com/heroku/cli/pull/3447

@eablack eablack mentioned this pull request Jan 6, 2026
12 tasks
@eablack eablack changed the title test: convert config to oclif/test 4 test: convert Config and Features/Labs to oclif/test 4 Jan 6, 2026
@eablack eablack marked this pull request as ready for review January 7, 2026 22:54
@eablack eablack requested a review from a team as a code owner January 7, 2026 22:54
Copy link
Contributor

@k80bowman k80bowman left a comment

Choose a reason for hiding this comment

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

I had a question, but it's not blocking. It looks like maybe you rewrote the test and left these commented out by accident? Otherwise, this looks good!

eablack added 12 commits January 9, 2026 15:14
Migrates config/edit.unit.test.ts from @oclif/test v2 to v4, completing
the last test file in the Configuration tests migration.

Key changes:
- Add EditorFactory class with instance method to enable testability
- Update config/edit command to use EditorFactory pattern
- Migrate tests to use custom runCommand helper with sinon stubs
- All 4 tests passing (stringToConfig, deleting vars, blanks, specific var)

Uses custom runCommand helper (not @oclif/test's runCommand) because
@oclif/core's dynamic module loading is incompatible with ES module
mocking via sinon or esmock.
@eablack eablack force-pushed the eb/upgrade-oclif-test-config branch from df8a775 to 47cf387 Compare January 9, 2026 23:14
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:14 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:14 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:14 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:14 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:49 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:49 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:49 — with GitHub Actions Inactive
@eablack eablack temporarily deployed to AcceptanceTests January 9, 2026 23:49 — with GitHub Actions Inactive
@eablack eablack merged commit 67abfc8 into eb/upgrade-oclif-test Jan 10, 2026
13 checks passed
@eablack eablack deleted the eb/upgrade-oclif-test-config branch January 10, 2026 00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants