https://github.com/ember-cli/ember-cli-blueprint-test-helpers
Test helpers for testing ember-cli blueprints
https://github.com/ember-cli/ember-cli-blueprint-test-helpers
ember-cli
Last synced: 7 months ago
JSON representation
Test helpers for testing ember-cli blueprints
- Host: GitHub
- URL: https://github.com/ember-cli/ember-cli-blueprint-test-helpers
- Owner: ember-cli
- Created: 2015-10-07T05:52:12.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2025-01-28T16:14:17.000Z (12 months ago)
- Last Synced: 2025-06-14T04:38:44.139Z (8 months ago)
- Topics: ember-cli
- Language: JavaScript
- Size: 1.14 MB
- Stars: 23
- Watchers: 14
- Forks: 15
- Open Issues: 23
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
ember-cli-blueprint-test-helpers
==============================================================================
[](https://badge.fury.io/js/ember-cli-blueprint-test-helpers)
[](https://github.com/ember-cli/ember-cli-blueprint-test-helpers/actions/workflows/ci.yml)
test helpers for [ember-cli](https://github.com/ember-cli/ember-cli) blueprints
Installation
------------------------------------------------------------------------------
```
ember install ember-cli-blueprint-test-helpers
```
It should be noted that `ember-cli-blueprint-test-helpers` currently
[only works for testing blueprints inside addon projects](https://github.com/ember-cli/ember-cli-blueprint-test-helpers/issues/56).
Usage
------------------------------------------------------------------------------
### Running Tests
The blueprint tests can be run by:
```
node_modules/.bin/mocha node-tests --recursive
```
For convenience you should add the following to your `package.json`:
```json
"scripts": {
"nodetest": "mocha node-tests --recursive"
}
```
to be able to use `npm run nodetest` to run the tests.
### Generating Tests
Generate a blueprint test scaffold using the `blueprint-test` generator:
```
ember generate blueprint-test my-blueprint
```
which will generate a test file at `node-tests/blueprints/my-blueprint-test.js`.
### Example Usage
```js
const {
setupTestHooks,
emberNew,
emberGenerateDestroy,
} = require('ember-cli-blueprint-test-helpers/helpers');
const {
expect
} = require('ember-cli-blueprint-test-helpers/chai');
describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
const args = ['my-blueprint', 'foo'];
// create a new Ember.js app in the working directory
return emberNew()
// then generate and destroy the `my-blueprint` blueprint called `foo`
.then(() => emberGenerateDestroy(args, (file) => {
// and run some assertions in between
expect(file('path/to/file.js'))
.to.contain('file contents to match')
.to.contain('more file contents\n');
}));
// magically done for you: assert that the generated files are destroyed again
});
});
```
or more explicitly:
```js
const {
setupTestHooks,
emberNew,
emberGenerate,
emberDestroy,
} = require('ember-cli-blueprint-test-helpers/helpers');
const {
expect,
file,
} = require('ember-cli-blueprint-test-helpers/chai');
describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
const args = ['my-blueprint', 'foo'];
// create a new Ember.js app in the working directory
return emberNew()
// then generate the `my-blueprint` blueprint called `foo`
.then(() => emberGenerate(args))
// then assert that the files were generated correctly
.then(() => expect(file('path/to/file.js'))
.to.contain('file contents to match')
.to.contain('more file contents\n'))
// then destroy the `my-blueprint` blueprint called `foo`
.then(() => emberDestroy(args))
// then assert that the files were destroyed correctly
.then(() => expect(file('path/to/file.js')).to.not.exist);
});
});
```
API Reference
------------------------------------------------------------------------------
This project exports two major API endpoints for you to use:
- `require('ember-cli-blueprint-test-helpers/chai')`
This endpoint exports the [Chai](http://chaijs.com/) assertion library
including the [chai-as-promised](https://github.com/domenic/chai-as-promised)
and [chai-files](https://github.com/Turbo87/chai-files) plugins
- `require('ember-cli-blueprint-test-helpers/helpers')`
This endpoint exports the functions mentioned in the following API reference
---
### `setupTestHooks(scope, options)`
Prepare the test context for the blueprint tests.
**Parameters:**
- `{Object} scope` the test context (i.e. `this`)
- `{Object} [options]` optional parameters
- `{Number} [options.timeout=20000]` the test timeout in milliseconds
- `{Object} [options.tmpenv]` object containing info about the temporary directory for the test.
- `{String} [options.cliPath='ember-cli']` path to the `ember-cli` dependency
- `{Boolean} [options.disabledTasks=['addon-install', 'bower-install', 'npm-install']]` override the mocked installs
Defaults to [`lib/helpers/tmp-env.js`](lib/helpers/tmp-env.js)
**Returns:** `{Promise}`
---
### `emberNew(options)`
Create a new Ember.js app or addon in the current working directory.
**Parameters:**
- `{Object} [options]` optional parameters
- `{String} [options.target='app']` the type of project to create (`app`, `addon` or `in-repo-addon`)
- `{string[]} [options.extraCliArgs=[]]` any extra arguments you want to pass to ember-cli
**Returns:** `{Promise}`
---
### `emberGenerate(args)`
Run a blueprint generator.
**Parameters:**
- `{Array.} args` arguments to pass to `ember generate` (e.g. `['my-blueprint', 'foo']`)
**Returns:** `{Promise}`
---
### `emberDestroy(args)`
Run a blueprint destructor.
**Parameters:**
- `{Array.} args` arguments to pass to `ember destroy` (e.g. `['my-blueprint', 'foo']`)
**Returns:** `{Promise}`
---
### `emberGenerateDestroy(args, assertionCallback)`
Run a blueprint generator and the corresponding blueprint destructor while
checking assertions in between.
**Parameters:**
- `{Array.} args` arguments to pass to `ember generate` (e.g. `['my-blueprint', 'foo']`)
- `{Function} assertionCallback` the callback function in which the assertions should happen
**Returns:** `{Promise}`
---
### `modifyPackages(packages)`
Modify the dependencies in the `package.json` file of the test project.
**Parameters:**
- `{Array.} packages` the list of packages that should be added,
changed or removed
---
### `setupPodConfig(options)`
Setup `usePods` in `.ember-cli` and/or `podModulePrefix` in `environment.js`.
**Parameters:**
- `{Object} [options]` optional parameters
- `{Boolean} [options.usePods]` add `usePods` in `.ember-cli`
- `{Boolean} [options.podModulePrefix]` set `npodModulePrefix` to `app/pods`
in `config/environment.js`
Used by
------------------------------------------------------------------------------
- https://github.com/emberjs/ember.js
- https://github.com/emberjs/data
- https://github.com/ember-cli/ember-cli-legacy-blueprints
- https://github.com/simplabs/ember-simple-auth
- https://github.com/DockYard/ember-suave
License
------------------------------------------------------------------------------
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).