36
loading...
This website collects cookies to deliver better user experience
generate
commands to get started with writing your tests.my-feature
:# create a pre-configured application test file for 'my-feature'
ember generate acceptance-test my-feature
// tests/acceptance/my-feature-test.js
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
module('Acceptance | my feature', function(hooks) {
setupApplicationTest(hooks);
test('visiting /my-feature', async function(assert) {
await visit('/my-feature');
assert.equal(currentURL(), '/my-feature');
});
});
generate
command to create a custom test file, instead of the default one.ember generate acceptance-test xyz
, the cli will create your test file based on your command-line input and the framework blueprint that is associated with the acceptance-test
parameter:// blueprints/acceptance-test/qunit-rfc-232-files/tests/acceptance/__name__-test.js
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
module('<%= friendlyTestName %>', function(hooks) {
setupApplicationTest(hooks);
test('visiting /<%= dasherizedModuleName %>', async function(assert) {
await visit('/<%= dasherizedModuleName %>');
assert.equal(currentURL(), '/<%= dasherizedModuleName %>');
});
});
index.js
:// blueprints/acceptance-test/index.js
'use strict';
const fs = require('fs');
const path = require('path');
const pathUtil = require('ember-cli-path-utils');
const stringUtils = require('ember-cli-string-utils');
const useTestFrameworkDetector = require('../test-framework-detector');
module.exports = useTestFrameworkDetector({
description: 'Generates an acceptance test for a feature.',
locals: function (options) {
let testFolderRoot = stringUtils.dasherize(options.project.name());
if (options.project.isEmberCLIAddon()) {
testFolderRoot = pathUtil.getRelativeParentPath(options.entity.name, -1, false);
}
let destroyAppExists = fs.existsSync(
path.join(this.project.root, '/tests/helpers/destroy-app.js')
);
let friendlyTestName = [
'Acceptance',
stringUtils.dasherize(options.entity.name).replace(/[-]/g, ' '),
].join(' | ');
return {
testFolderRoot: testFolderRoot,
friendlyTestName,
destroyAppExists,
};
},
});
acceptance-test
blueprint in your project, you can now extend this functionality with your own, custom acceptance test setup in a single generate
command.ember generate
while in your Ember application's directory again:ember generate blueprint acceptance-test
blueprints/acceptance-test-index.js
:'use strict';
module.exports = {
description: ''
// locals(options) {
// // Return custom template variables here.
// return {
// foo: options.entity.options.foo
// };
// }
// afterInstall(options) {
// // Perform extra work here.
// }
};
index.js
could look like this:// blueprints/acceptance-test/index.js
'use strict';
const fs = require('fs');
const path = require('path');
const stringUtils = require('ember-cli-string-utils');
module.exports = {
description: 'Generates an acceptance test for a feature.',
locals: function (options) {
let destroyAppExists = fs.existsSync(
path.join(this.project.root, '/tests/helpers/destroy-app.js')
);
let friendlyTestName = [
'Acceptance',
stringUtils.dasherize(options.entity.name).replace(/[-]/g, ' '),
].join(' | ');
return {
testFolderRoot: 'tests/acceptance/',
friendlyTestName,
destroyAppExists,
};
},
};
blueprints/acceptance-test/files
directory in your project, including the default template __name__-test.js
.# copy from framework blueprints file layout...
-- qunit-rfc-232-files
|-- tests
|-- acceptance
|-- __name__-test.js
# ...to your project's file layout
-- files
|-- tests
|-- acceptance
|-- __name__-test.js
// blueprints/acceptance-test/files/tests/acceptance/__name__-test.js
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
module('<%= friendlyTestName %>', function(hooks) {
setupApplicationTest(hooks);
test('visiting /<%= dasherizedModuleName %>', async function(assert) {
await visit('/<%= dasherizedModuleName %>');
assert.equal(currentURL(), '/<%= dasherizedModuleName %>');
});
});
blueprints/acceptance-test/files/tests/acceptance/__name__-test.js
to your needs.// blueprints/acceptance-test/files/tests/acceptance/__name__-test.js
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { sinon } from 'sinon';
import { setupStripe } from 'my-ember-app/tests/helpers/stripe-mock';
module('<%= friendlyTestName %> ✨✨✨', function(hooks) {
setupApplicationTest(hooks);
setupStripe(hooks);
hooks.beforeEach(function() {
this.testStub = sinon.stub();
});
test('visiting /<%= dasherizedModuleName %>', async function(assert) {
await visit('/<%= dasherizedModuleName %>');
assert.equal(currentURL(), '/<%= dasherizedModuleName %>');
});
});
generate
command for creating an acceptance test again, the cli will use our custom testing blueprint configuration and modify our test file accordingly. Check it out:ember generate acceptance-test my-feature
// tests/acceptance/my-feature-test.js
import { module, test } from 'qunit';
import { visit, currentURL } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';
import { sinon } from 'sinon';
import { setupStripe } from 'my-ember-app/tests/helpers/stripe-mock';
module('Acceptance | my feature ✨✨✨', function(hooks) {
setupApplicationTest(hooks);
setupStripe(hooks);
hooks.beforeEach(function() {
this.testStub = sinon.stub();
});
test('visiting /my-feature', async function(assert) {
await visit('/my-feature');
assert.equal(currentURL(), '/my-feature');
});
});
generate
commands, such as ember generate acceptance-test
or ember generate component-test
.generate
commands the framework already offers, you can add your own generate
commands, too. If you always wanted to make it easier to write documentation for your project, why not create a ember generate docs
blueprint today?