Yeoman Generator Testing Utilities

11.3.1 · active · verified Tue Apr 21

Yeoman-test provides a comprehensive suite of utilities designed for unit testing Yeoman generators. It simplifies the process of testing generator logic, file system modifications, and user interactions. The current stable version is 11.3.1. Releases occur frequently, often in conjunction with updates to core Yeoman ecosystem packages like `@yeoman/adapter`, `yeoman-environment`, and `yeoman-generator`, ensuring compatibility and leveraging new features. Key differentiators include its tight integration with Yeoman's `mem-fs` virtual file system, enabling robust assertions on generated files and content without touching the actual disk. It facilitates setting up temporary test directories, simulating command-line arguments and user prompts, and asserting file creation, content, and deletion. It also offers mechanisms for mocking composed generators, which is crucial for testing complex generator flows. The library aims to provide a controlled, isolated environment for predictable and reliable generator testing.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up, run, and assert outcomes of a Yeoman generator test using `yeoman-test`. It covers running generators with options and prompts, asserting file system changes, and mocking composed generators.

import helpers, { result } from 'yeoman-test';
import assert from 'assert';

describe('generator test suite', () => {
  // Define a dummy generator namespace or point to a local generator path
  const generatorNamespace = 'my-generator:app';

  beforeEach(async () => {
    // Ensure your generator and its dependencies are installed:
    // npm install --save-dev yeoman-generator yeoman-environment
    // npm install --save-dev yeoman-test
    // This will run the generator in a temporary directory
    await helpers
      .run(generatorNamespace)
      .withOptions({ skipInstall: true, someOption: 'value' })
      .withPrompts({ name: 'my-project', features: ['featureA'] });
  });

  it('creates a project directory and a specific file', () => {
    // Assertions using the global result object from the last run
    result.assertNoFileContent('package.json', /"private": true/);
    result.assertFile('my-project/package.json');
    result.assertFileContent('my-project/package.json', /"name": "my-project"/);
    result.assertJsonFileContent('my-project/package.json', { dependencies: { lodash: '^4.0.0' } });
  });

  it('can create additional generators in the same context', async () => {
    // Use result.create to run another generator within the same temporary directory
    await result.create('another-generator:sub').run();
    result.assertFile('my-project/src/sub-feature.js');
  });

  it('handles composed generators correctly', async () => {
    // Mock a composed generator to verify interaction without running its full logic
    await helpers.run('my-generator:app').withMockedGenerators(['composed:feature']);
    assert(result.mockedGenerators['composed:feature'].calledOnce);
  });
});

view raw JSON →