docs: Add comprehensive testing guide for zx scripts#1364
Open
Maxiemad wants to merge 1 commit intogoogle:mainfrom
Open
docs: Add comprehensive testing guide for zx scripts#1364Maxiemad wants to merge 1 commit intogoogle:mainfrom
Maxiemad wants to merge 1 commit intogoogle:mainfrom
Conversation
- 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.
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.