@trabus
@jakebixby
father
ember-cli core team member
corgi food balancing enthusiast
Testing your blueprints will save you a lot of pain if you're an addon developer.
It will alert you to ember-cli changes that may inadvertently break your blueprints.
It's a great way to test all permutations of your complicated blueprints.
(cleanup for the next test)
The addonification of ember and ember-data prompted the extraction of blueprints, which included their tests.
We needed a way to quickly setup a temp project or addon, then generate and destroy from its context
(what's this all about then?)
ember-cli has had a form of blueprint test helpers from the beginning, but they weren't usable outside ember-cli.
Many thanks to Tobias Bieniek (@turbo87), who helped bring this project to completion!
We've developed a simple set of promise based test helpers to allow you to setup, generate, destroy, and assert in many flexible permutations.
To support testing the extracted blueprints, we also extracted much of the test helpers and setup utilities.
https://github.com/ember-cli/ember-cli-blueprint-test-helpers
Generate a clean test ember app/addon
Generate/Destroy blueprints in that test app/addon
Assert file contents with chai helpers
Assert cli output and errors
Addons only. Doesn't work for blueprints contained inside projects (but may work in the future)
Doesn't support generating inside engines (yet)
Generates tests with ES6 syntax, so Node 5.x or higher is recommended, or use babel
ember install ember-cli-blueprint-test-helpers
ember g blueprint-test my-blueprint
This will generate a blueprint-test inside your addon's node-tests folder. From there you can fill in your tests.
ember g ember-cli-blueprint-test-helpers --babel
This will install babel testing dependencies in your addon, including a .babelrc, and add an npm script which will run your tests with babel-register
describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
var 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
});
});describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
var 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);
});
});describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
var args = ['my-blueprint', 'foo'];
// create a new Ember.js app in the working directory
return emberNew()
// we only need to generate, since we're expecting an error
.then(() => expect(emberGenerate(args))
// use chai-as-promised rejectedWith helper
.to.be.rejectedWith(SilentError, /Foo is overused/));
});
});describe('Acceptance: ember generate and destroy my-blueprint', function() {
// create and destroy temporary working directories
setupTestHooks(this);
it('my-blueprint foo', function() {
var 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((result) => => {
let output = result.outputStream.join('');
// test ui output because we can't test for actual npm install modifying package.json
expect(output).to.contain('npm-package')
});
});
});mocha node-tests --recursive
If you installed the babel dependencies, your npm node-test script has been updated to:
mocha node-tests --recursive --require babel-register
Then you can run your tests through an npm script!
npm run nodetest
Use ember-cli-blueprint-test-helpers to test your generated blueprint output
Catch issues caused by upstream changes to ember-cli and prepare for future blueprints changes
Addon authors: the tools are available now, please test your addon's blueprints!
@trabus
@jakebixby