ember-qunit simplifies testing of Ember applications with QUnit by providing QUnit-specific wrappers around the helpers contained in ember-test-helpers.
ember-qunit
is an Ember CLI addon, so install it
as you would any other addon:
$ ember install ember-qunit
Some other addons are detecting the test framework based on the installed
addon names and are expecting ember-cli-qunit
instead. If you have issues
with this then ember install ember-cli-qunit
, which should work exactly
the same.
For instructions how to upgrade your test suite to the latest testing APIs, please read our Migration Guide.
The following section describes the use of ember-qunit with the latest modern Ember testing APIs, as laid out in the RFCs 232 and 268.
For the older APIs have a look at our Legacy Guide.
Your tests/test-helper.js
file should look similar to the following, to
correctly setup the application required by @ember/test-helpers
:
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
setApplication(Application.create(config.APP));
start();
Also make sure that you have set ENV.APP.autoboot = false;
for the test
environment in your config/environment.js
.
The setupTest()
function can be used to setup a unit test for any kind
of "module/unit" of your application that can be looked up in a container.
It will setup your test context with:
this.owner
to interact with Ember's Dependency Injection systemthis.set()
,this.setProperties()
,this.get()
, andthis.getProperties()
this.pauseTest()
method to allow easy pausing/resuming of tests
For example, the following is a unit test for the SidebarController
:
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('SidebarController', function(hooks) {
setupTest(hooks);
// Replace this with your real tests.
test('exists', function() {
let controller = this.owner.lookup('controller:sidebar');
assert.ok(controller);
});
});
The setupRenderingTest()
function is specifically designed for tests that
render arbitrary templates, including components and helpers.
It will setup your test context the same way as setupTest()
, and additionally:
- Initializes Ember's renderer to be used with the
Rendering helpers,
specifically
render()
- Adds
this.element
to your test context which returns the DOM element representing the wrapper around the elements that were rendered viarender()
- sets up the DOM Interaction Helpers
from
@ember/test-helpers
(click()
,fillIn()
, ...)
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('GravatarImageComponent', function(hooks) {
setupRenderingTest(hooks);
test('renders', async function() {
await render(hbs`{{gravatar-image}}`);
assert.ok(this.element.querySelector('img'));
});
});
The setupApplicationTest()
function can be used to run tests that interact
with the whole application, so in most cases acceptance tests.
On top of setupTest()
it will:
- Boot your application instance
- Set up all the DOM Interaction Helpers
(
click()
,fillIn()
, ...) as well as the Routing Helpers (visit()
,currentURL()
, ...) from@ember/test-helpers
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, currentURL } from '@ember/test-helpers';
module('basic acceptance test', function(hooks) {
setupApplicationTest(hooks);
test('can visit /', async function(assert) {
await visit('/');
assert.equal(currentURL(), '/');
});
});
git clone <repository-url>
cd ember-qunit
npm install
npm test
(Runsember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
ember serve
- Visit the dummy application at http://localhost:4200.
For more information on using ember-cli, visit https://ember-cli.com/.