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.
npm install yeoman-testVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade your Node.js runtime to version 20.6.1 or higher. Alternatively, pin `yeoman-test` to a `10.x` version using `npm install --save-dev yeoman-test@^10`.
Always check the `peerDependencies` in `package.json` for `yeoman-test` to ensure compatibility with your installed `yeoman-environment` and `yeoman-generator` versions. Update all Yeoman-related packages together when upgrading `yeoman-test`.
Ensure your test files use ESM `import` syntax. Configure your `package.json` with `"type": "module"` for your test directory or use a `.mjs` extension for test files. If using a test runner, ensure it's configured to run tests as ESM.
For sequential or independent runs, ensure assertions are made immediately after each `await helpers.run(...)` or `await result.create(...).run()` call. For complex scenarios, consider wrapping runs in local variables if the global `result` object's behavior causes confusion.
Convert your test files to use ES Module `import` syntax (`import helpers from 'yeoman-test';`). Ensure your project or test files are configured for ESM (e.g., `"type": "module"` in `package.json` or `.mjs` file extensions).
Ensure that `await helpers.run(...)` has completed before attempting to use `result` for assertions. Verify `import { result } from 'yeoman-test';` is present and correctly scoped.Install or upgrade `yeoman-environment` to a compatible version as specified in `yeoman-test`'s `peerDependencies`. For `yeoman-test@11`, this typically means `yeoman-environment@^4.0.0 || ^5.0.0-beta.0 || ^6.0.0`.