Skip to content

Test utilities for Yeoman generators

License

Notifications You must be signed in to change notification settings

yeoman/yeoman-test

Folders and files

NameName
Last commit message
Last commit date
Feb 7, 2025
Aug 26, 2024
Oct 11, 2024
Oct 11, 2024
Aug 26, 2024
Dec 10, 2015
Jul 22, 2020
May 5, 2023
Aug 26, 2024
May 11, 2023
Feb 1, 2021
Nov 29, 2022
Dec 10, 2015
Oct 26, 2023
Aug 27, 2024
Feb 1, 2021
Feb 4, 2025
Jan 21, 2025
Aug 26, 2024
Aug 27, 2024

Repository files navigation

yeoman-test

NPM version NPM Test Integration Build Dependency Status Coverage percentage

Test utilities for Yeoman generators

Installation

$ npm install --save-dev yeoman-test

Install target environment and generator:

$ npm install --save-dev yeoman-generator@xxx yeoman-environment@xxx

Usage

Usage using the convenience last RunResult instance:

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

describe('generator test', () => {
  describe('test', () => {
    beforeEach(async () => {
      await helpers
        .run(                   // instantiates RunContext
          'namespace',             // namespace or generator
          {},                      // test options
          {}                       // environment options
        )
        [.cd(dir)]                  // runs the test inside a non temporary dir
        [.onTargetDirectory(dir => {})        // prepares the test dir
        [.withGenerators([])]       // registers additional generators
        [.withLookups({})]          // runs Environment lookups
        [.withOptions({})]          // passes options to the generator
        [.withLocalConfig({})]      // sets the generator config as soon as it is instantiated
        [.withAnswers()]            // simulates the prompt answers
        [.withMockedGenerators(['namespace', ...])]      // adds a mocked generator to the namespaces
        [.withFiles({
          'foo.txt': 'bar',
          'test.json', { content: true },
        })]                         // add files to mem-fs
        [.withYoRc({ 'generator-foo': { bar: {} } })]    // add config to .yo-rc.json
        [.withYoRcConfig('generator-foo.bar', { : {} })] // same as above
        [.commitFiles()]            // commit mem-fs files to disk
        [.onGenerator(gen => {})]   // do something with the generator
        [.onEnvironment(env => {})]; // do something with the environment

      [await result.create('another-generator').run();] // instantiates a new RunContext at the same directory
    );

    it('runs correctly', () => {
      // runs assertions using mem-fs.
      [result.assertFile('file.txt');]
      [result.assertNoFile('file.txt');]
      [result.assertFileContent('file.txt', 'content');]
      [result.assertEqualsFileContent('file.txt', 'content');]
      [result.assertNoFileContent('file.txt', 'content');]
      [result.assertJsonFileContent('file.txt', {});]
      [result.assertNoJsonFileContent('file.txt', {});]
    });
  });
});

Generator compose:

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

describe('my-gen', () => {
  before(() => helpers.run('my-gen').withMockedGenerator(['composed-gen']));
  it('should compose with composed-gen', () => {
    assert(result.mockedGenerators['composed-gen'].calledOnce);
  });
});

Generic test folder:

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

describe('generic test', () => {
  before(() => helpers.prepareTemporaryDir());
  it('test', () => {
    result.assert...;
  });
});

See our api documentation for latest yeoman-test release.

See our api documentation for yeoman-test 5.0.1. Use 5.x for yeoman-environment 2.x support.

See our api documentation for yeoman-test 2.x.

See our documentation for yeoman-test 2.x.

License

MIT © The Yeoman Team