Back to Repositories

Testing Command Execution Patterns in google/zx

This test suite validates core functionality of the zx library’s command execution capabilities in TypeScript. It focuses on both synchronous and asynchronous command processing, along with error handling mechanisms.

Test Coverage Overview

The test suite provides comprehensive coverage of zx’s command execution features.

Key areas tested include:
  • Asynchronous command execution with stdout validation
  • Synchronous command processing verification
  • Error handling and exit code capture
  • Command output pattern matching

Implementation Analysis

The testing approach utilizes TypeScript’s async/await patterns combined with Node’s assert module for verification. The implementation demonstrates both modern ECMAScript features and traditional command-line interaction testing.

Notable patterns include:
  • Self-executing async function structure
  • Template literal command execution
  • Regular expression matching for output validation

Technical Details

Testing infrastructure includes:
  • Node.js assert module for assertions
  • zx/globals import for command execution
  • TypeScript configuration for type safety
  • Error capture configuration via nothrow option

Best Practices Demonstrated

The test suite exemplifies several testing best practices for command-line utilities. It includes isolated test blocks, proper error handling validation, and comprehensive output verification.

Key practices include:
  • Separate test cases for sync and async operations
  • Explicit error condition testing
  • Clean and focused test scenarios
  • Proper assertion usage for output validation

google/zx

test/smoke/ts.test.ts

            
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as assert from 'node:assert'
import 'zx/globals'
;(async () => {
  // smoke test async
  {
    const p = await $`echo foo`
    assert.match(p.stdout, /foo/)
  }

  // smoke test sync
  {
    const p = $.sync`echo foo`
    assert.match(p.stdout, /foo/)
  }

  // captures err stack
  {
    const p = await $({ nothrow: true })`echo foo; exit 3`
    assert.match(p.message, /exit code: 3/)
  }
})()

console.log('smoke ts: ok')