docs: Add comprehensive testing guide for zx scripts #1364
+737
−0
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
docs/testing.md(499 lines)examples/testing-example.mjsexamples/testing-example.test.mjs(all 8 tests passing)What's Included
The documentation covers multiple approaches to testing zx scripts:
$.spawn- For unit testing (as recommended by @antongolub in the issue)within()- Preventing test interferenceUsage 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 },
})
})
})## Verification
Related
Addresses #1297 - "How do I write tests for zx scripts?"
npm buildbefore committing and verified the bundle updates correctly.npm testand confirmed all tests succeed.