Back to Repositories

Validating JSR Artifact Publishing Process in google/zx

This test suite validates the JSR (JavaScript Registry) artifact publishing functionality in the google/zx project. It focuses on verifying the build and dry-run publishing process, ensuring proper file preparation and package configuration for JSR deployment.

Test Coverage Overview

The test suite provides comprehensive coverage of JSR artifact preparation and publishing workflow.

Key areas tested include:
  • Temporary directory setup and file copying
  • Source file preparation and dependency linking
  • JSR publish dry-run verification
  • Build script execution validation
The tests ensure proper handling of project assets and configuration files necessary for JSR publishing.

Implementation Analysis

The testing approach utilizes Jest’s describe/it blocks for structured test organization, with before/after hooks managing test environment setup and cleanup.

Key implementation patterns include:
  • Async/await for asynchronous operations
  • File system operations with fs module
  • Path resolution and directory management
  • Command execution using zx’s $ utility

Technical Details

Testing tools and configuration:
  • Node.js test runner (node:test)
  • zx utility functions (tempdir, $)
  • File system operations (fs module)
  • Path manipulation utilities
  • JSR CLI tools
Setup includes temporary directory creation, file copying, and symlink creation for node_modules.

Best Practices Demonstrated

The test suite exemplifies several testing best practices for build and deployment workflows.

Notable practices include:
  • Isolated test environment using temporary directories
  • Proper cleanup after test execution
  • Comprehensive file system preparation
  • Use of dry-run mode for safe testing
  • Structured setup and teardown procedures

google/zx

test/it/build-jsr.test.js

            
// 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 { tempdir, $, path, fs } from '../../build/index.js'
import { describe, before, after, it } from 'node:test'

const __dirname = path.dirname(new URL(import.meta.url).pathname)
const root = path.resolve(__dirname, '../../')

describe('jsr artifact', () => {
  let tmp
  let t$

  before(async () => {
    tmp = tempdir()
    t$ = $({ cwd: tmp, quiet: true })

    // copy all for jsr publish
    await Promise.all(
      [
        'src/',
        'tsconfig.json',
        'LICENSE',
        'scripts/build-jsr.mjs',
        'package.json',
      ].map((filepath) =>
        fs.copy(
          path.resolve(path.join(root, filepath)),
          path.resolve(path.join(tmp, filepath))
        )
      )
    )
    await t$`ln -s ${path.resolve(root, 'node_modules')} ${path.resolve(tmp, 'node_modules')}`
  })
  after(() => fs.remove(tmp))

  it('publish --dry-run --allow-dirty`', async () => {
    await t$`node scripts/build-jsr.mjs`
    await t$({ quiet: false })`jsr publish --dry-run --allow-dirty`
  })
})