Ember CLI Blueprint Test Helpers
ember-cli-blueprint-test-helpers provides a dedicated suite of testing utilities for `ember-cli` blueprint authors. It enables comprehensive testing of blueprint generation and destruction logic by mocking the `ember-cli` environment, allowing developers to simulate `ember generate` and `ember destroy` commands within a controlled context. The current stable version is `0.19.2`, with releases occurring periodically to address bugs and introduce minor enhancements, rather than a fixed cadence. A key differentiator is its specialized focus on blueprint testing within Ember addon projects, providing helpers like `setupTestHooks`, `emberNew`, `emberGenerate`, `emberDestroy`, and `emberGenerateDestroy` to streamline the assertion process for generated and removed files, including support for Module Unification setups. This package is crucial for ensuring the correctness and stability of `ember-cli` blueprints.
Common errors
-
TypeError: (0 , _emberCliBlueprintTestHelpers.setupTestHooks) is not a function
cause This error typically occurs when `setupTestHooks` is imported or required incorrectly, or when attempting to destructure it from a module that doesn't export it in the expected way.fixEnsure you are using the correct CommonJS `require` or ES module `import` syntax for the helper functions. For CommonJS: `const { setupTestHooks } = require('ember-cli-blueprint-test-helpers');` or for ESM: `import { setupTestHooks } from 'ember-cli-blueprint-test-helpers';`. -
AssertionError: expected 'path/to/file.js' to contents 'some content'
cause This error indicates that an outdated assertion helper `contents` is being used, which was renamed in a previous version of the library.fixReplace `contents` with `contain`. The correct assertion syntax is `expect(file('path/to/file.js')).to.contain('some content');`. -
Error: Blueprint tests for apps are not supported. Only addons.
cause Attempting to run blueprint tests using this library within an Ember application project rather than an Ember addon project, which is an unsupported use case.fixRelocate your blueprint tests to an Ember addon project. This library is specifically designed to test blueprints contained within addons.
Warnings
- gotcha This library is specifically designed for and explicitly 'only works for testing blueprints inside addon projects'. Attempting to use it for blueprint tests within an Ember application project will result in unexpected behavior or failures.
- breaking Version `0.12.0` removed 'obsolete test runner and fixtures' and 'obsolete helpers'. This change will break existing tests that relied on these deprecated utilities.
- breaking In `v0.10.2`, the example assertion method `contents` was renamed to `contains`. Tests using the older `contents` method for `expect(file(...))` assertions will fail.
- gotcha The `EMBER_CLI_MODULE_UNIFICATION` environment variable, crucial for testing Module Unification blueprints, was restored in `v0.19.2` after being implicitly problematic or missing in some previous versions. Tests relying on this variable might have failed or behaved inconsistently in versions prior to `0.19.2` if they expected this variable to be correctly handled.
Install
-
npm install ember-cli-blueprint-test-helpers -
yarn add ember-cli-blueprint-test-helpers -
pnpm add ember-cli-blueprint-test-helpers
Imports
- setupTestHooks
const setupTestHooks = require('ember-cli-blueprint-test-helpers').setupTestHooks;import { setupTestHooks } from 'ember-cli-blueprint-test-helpers'; - emberGenerate
emberGenerate(); // without explicit import after setupTestHooks(this);
import { emberGenerate } from 'ember-cli-blueprint-test-helpers'; - emberGenerateDestroy
const { emberGenerateDestroy } = require('ember-cli-blueprint-test-helpers/helpers');import { emberGenerateDestroy } from 'ember-cli-blueprint-test-helpers'; - file
expect(file('path/to/file.js')); // No import statementimport { file } from 'ember-cli-blueprint-test-helpers';
Quickstart
import { setupTestHooks, emberNew, emberGenerate, emberDestroy, file } from 'ember-cli-blueprint-test-helpers';
import { expect } from 'chai';
describe('Acceptance: ember generate and destroy my-blueprint', function() {
// Create and destroy temporary working directories for each test
setupTestHooks(this);
it('my-blueprint foo generates and cleans up files', async function() {
const args = ['my-blueprint', 'foo'];
// Create a new Ember.js app in the working directory
await emberNew();
// Generate the `my-blueprint` blueprint with name `foo`
await emberGenerate(args);
// Assert that the files were generated correctly
expect(file('app/components/foo.js')).to.exist;
expect(file('app/components/foo.js'))
.to.contain('import Component from '@glimmer/component';')
.to.contain('export default class FooComponent extends Component {}');
// Destroy the `my-blueprint` blueprint with name `foo`
await emberDestroy(args);
// Assert that the generated files were destroyed correctly
expect(file('app/components/foo.js')).to.not.exist;
});
it('my-blueprint bar with Module Unification', async function() {
const args = ['my-blueprint', 'bar'];
// Create a new Ember.js app with Module Unification structure
await emberNew({ isModuleUnification: true });
// Generate and destroy the `my-blueprint` blueprint called `bar`
await emberGenerateDestroy(args, (fileContents) => {
// Assertions run between generate and destroy
expect(fileContents('src/ui/components/bar/component.js')).to.exist;
expect(fileContents('src/ui/components/bar/component.js'))
.to.contain('// Module Unification component content');
});
});
});