Yeoman test is a core tool for writing and running tests for Yeoman generators. Mastering its setup and workflow empowers you to ensure your generators work reliably, speeding up development and improving code quality.
What is Yeoman Test?
Yeoman test is a specialized utility for testing Yeoman generators. Generators scaffold new projects and modules, and testing ensures they behave as expected across updates. yeoman-test provides a set of helpers and an isolated environment for safely running generator commands and validating results.
This tool works seamlessly with modern testing frameworks such as Mocha, allowing you to automate project creation scenarios and check that your generator output is correct every time.
| Aspect | Details |
|---|---|
| Tool | yeoman-test Node.js package |
| Purpose | Testing Yeoman generators in isolation |
| Core Features | Test environment, directory handling, helpers for prompts and options |
| Typical Users | Generator authors, open-source developers |
| Supported Frameworks | Works with Mocha, Jest, and others |
Why You Should Test Yeoman Generators
Automated testing for Yeoman generators prevents unexpected failures, regressions, and ensures a smooth developer experience for users. Without tests, changes to your codebase risk breaking generator output or installer logic, especially as code grows or is maintained by teams.
Testing also provides a safety net when you refactor, add features, or update your dependencies. With yeoman-test, you can run these tests quickly as part of your continuous integration or pre-publish steps.
Essential Yeoman Test Setup
Getting started requires several key ingredients. Let’s break down the environment you need and how to prepare your project for effective generator testing.
Prerequisites
- Node.js & npm installed (LTS version recommended)
- An existing Yeoman generator project
- Familiarity with Mocha or a similar testing framework
Install Required Packages
In your generator’s root directory, run:
npm install --save-dev yeoman-test mocha
This command adds yeoman-test and Mocha as development dependencies, preparing your project for structured tests.
Writing Your First Yeoman Test
What’s the basic structure of a Yeoman test? Let’s walk through a simple real-world example to demonstrate the main workflow:
Example: Basic Test Structure
// test/app.js
const path = require('path');
const helpers = require('yeoman-test');
const assert = require('assert');
describe('my-generator:app', function () {
it('creates expected files', async function () {
await helpers
.run(path.join(__dirname, '../generators/app'))
.withPrompts({ someAnswer: true });
assert.ok(fs.existsSync('expected-file.js'));
});
});
This test checks that running the generator scaffolds an expected file. You can add more assertions to check file content or generated configuration.
- helpers.run: Sets up a sandbox for running your generator
- withPrompts: Simulates answers to prompts
- assert.ok: Validates generator results
Working with the Yeoman Test API
The yeoman-test API offers convenient methods for handling directories, simulating user inputs, and more. Familiarize yourself with these for robust tests.
Core API Methods
- helpers.run(generatorPath): Bootstraps a test environment for your generator
- withOptions(options): Passes CLI options (e.g.,
--skip-install) - withPrompts(answers): Supplies answers to inquirer.js prompts
- withLocalConfig(config): Sets initial local configuration
- inTmpDir(callback): Runs the generator in a fresh temporary directory
- cd(): Changes the working directory before running the generator
Example: Testing Multiple Scenarios
A well-structured test suite covers different user inputs and outcomes. For example:
it('creates file with custom name', async function () {
await helpers
.run(path.join(__dirname, '../generators/app'))
.withPrompts({ filename: 'example.js' });
assert.ok(fs.existsSync('example.js'));
});
This ensures your generator adapts correctly to user choices.
Common Pitfalls and How to Avoid Them
Testing generators isn’t always smooth. Typical issues include test pollution (leftover files), misconfigured paths, and unexpected prompt failures. You may also run into permission or environment errors on CI platforms.
Most problems stem from not using temporary directories (via inTmpDir()) or not properly simulating prompts/CLI options. Always review your setup and ensure tests are isolated and resetting state between runs for reliability.
- Use inTmpDir to guarantee a clean slate for each test
- Check your generator’s root path is correct
- Mock dependencies as needed to isolate logic
- Clean up after each test if you write outside temp directories
Yeoman Test Best Practices
Level up your test strategy by following widely-adopted practices. This will help you maintain fast, reliable, and readable tests for your generator projects.
Key Best Practices
- Automate tests: Integrate with CI tools so every push runs tests automatically
- Keep tests independent: Use temp directories to prevent state leaks
- Document your tests: Comment intentions for clarity, especially with complex prompts
- Test real user scenarios: Simulate genuine input and edge cases, not just the happy path
- Review and update: Keep up with yeoman-test releases and update your suite as APIs evolve
FAQ
- What is yeoman-test used for?
- It provides a testing harness for running and automating tests on Yeoman generators, making development safer and more efficient.
- Can I use yeoman-test with Jest?
- Yes. While most examples use Mocha, yeoman-test works with any Node.js testing framework.
- Does yeoman-test support mocking user prompts?
- Yes, the
withPromptshelper lets you simulate all user input, making tests fully automated. - How do I ensure a clean state between tests?
- Use
inTmpDirfor every test to isolate your file system environment and avoid pollution. - Is yeoman-test actively maintained?
- As of 2024, yes, it’s part of the official Yeoman toolchain and receives regular updates.