Skip to content

Conversation

@Maxiemad
Copy link

@Maxiemad Maxiemad commented Nov 6, 2025

Fixes #1297

Summary

This PR adds comprehensive documentation and working examples for testing zx scripts, addressing the question "How do I write tests for zx scripts?" raised in issue #1297.

Changes

  • ✅ Added complete testing documentation at docs/testing.md (499 lines)
  • ✅ Added working example script at examples/testing-example.mjs
  • ✅ Added complete test suite example at examples/testing-example.test.mjs (all 8 tests passing)
  • ✅ Updated VitePress config to include testing guide in navigation

What's Included

The documentation covers multiple approaches to testing zx scripts:

  1. Mocking $.spawn - For unit testing (as recommended by @antongolub in the issue)
  2. Using custom spawn functions - For more control over command mocking
  3. Extracting logic to testable functions - Refactoring approach
  4. Integration testing - Testing with real commands
  5. Context isolation with within() - Preventing test interference
  6. Best practices - Patterns and helper functions

Usage Example

import { $, within } from 'zx'
import { createMockSpawn } from './test-helpers.mjs'

test('deploy function', async () => {
await within(async () => {
$.spawn = createMockSpawn({
'git branch --show-current': { stdout: 'main\n', exitCode: 0 },
})

const result = await deploy()
assert.equal(result.branch, 'main')

})
})## Verification

  • ✅ All examples tested and verified to work
  • ✅ Test suite passes all 8 tests
  • ✅ Documentation builds correctly
  • ✅ Code examples are correct and tested

Related

Addresses #1297 - "How do I write tests for zx scripts?"


  • Build: I've run npm build before committing and verified the bundle updates correctly.
  • Tests: I've run npm test and confirmed all tests succeed.
  • Docs: I've added or updated relevant documentation as needed.
  • CoC: My changes follow the project's coding guidelines and Code of Conduct.
  • Review: This PR represents original work and is not solely generated by AI tools.

- Add complete testing documentation at docs/testing.md
- Add working example script at examples/testing-example.mjs
- Add complete test suite example at examples/testing-example.test.mjs
- Update VitePress config to include testing guide in navigation

Fixes google#1297

This PR addresses the issue asking for best practices on how to test zx scripts.
The documentation covers:
- Mocking $.spawn for unit testing
- Using custom spawn functions
- Extracting logic to testable functions
- Integration testing approaches
- Context isolation with within()
- Complete working examples

All examples have been tested and verified to work correctly.
@google-cla
Copy link

google-cla bot commented Nov 6, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How do I write tests for zx scripts?

2 participants