What is ember-qunit?
ember-qunit is an Ember.js testing framework that integrates QUnit with Ember.js applications. It provides a set of helpers and utilities to make it easier to write and run tests for Ember.js applications.
What are ember-qunit's main functionalities?
Module Setup
This feature allows you to set up a test module for unit testing. The `setupTest` helper sets up the necessary environment for testing an Ember.js service.
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Service | my-service', function(hooks) {
setupTest(hooks);
test('it exists', function(assert) {
let service = this.owner.lookup('service:my-service');
assert.ok(service);
});
});
Rendering Tests
This feature allows you to write rendering tests for Ember.js components. The `setupRenderingTest` helper sets up the environment for rendering a component and the `render` function is used to render the component in the test.
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('Integration | Component | my-component', function(hooks) {
setupRenderingTest(hooks);
test('it renders', async function(assert) {
await render(hbs`<MyComponent />`);
assert.equal(this.element.textContent.trim(), 'expected text');
});
});
Application Tests
This feature allows you to write application tests for Ember.js routes. The `setupApplicationTest` helper sets up the environment for testing an entire Ember.js application, and the `visit` function is used to navigate to a specific route in the test.
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { visit, currentURL } from '@ember/test-helpers';
module('Acceptance | my route', function(hooks) {
setupApplicationTest(hooks);
test('visiting /my-route', async function(assert) {
await visit('/my-route');
assert.equal(currentURL(), '/my-route');
});
});
Other packages similar to ember-qunit
ember-mocha
ember-mocha is an alternative testing framework for Ember.js that integrates Mocha with Ember.js applications. It provides similar functionality to ember-qunit but uses Mocha as the testing framework instead of QUnit. This can be useful for developers who prefer Mocha's syntax and features.
ember-cli-qunit
ember-cli-qunit is another package that integrates QUnit with Ember.js applications. It provides a similar set of helpers and utilities as ember-qunit but is specifically designed to work with the Ember CLI build system.
ember-test-helpers
ember-test-helpers is a low-level library that provides a set of helpers for testing Ember.js applications. It is used internally by both ember-qunit and ember-mocha and can be used directly for more fine-grained control over test setup and execution.
ember-qunit
ember-qunit simplifies testing of Ember applications with
QUnit by providing QUnit-specific wrappers around the
helpers contained in
ember-test-helpers.
Requirements
- Ember.js v3.28 or above
- Ember CLI v3.28 or above
- Node.js v14 or above
- TypeScript 4.8 and 4.9
- SemVer policy: simple majors
- The public API is defined by the [Usage][#usage] section below.
If you need support for Node 13 or older Ember CLI versions please use v4.x
of this addon.
Installation
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.
Upgrading
For instructions how to upgrade to the latest version, please read our
Migration Guide.
Usage
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.
Setting the Application
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
.
Setup Tests
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()
, and this.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);
test('exists', function() {
let controller = this.owner.lookup('controller:sidebar');
assert.ok(controller);
});
});
Setup Rendering Tests
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 via
render()
- 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'));
});
});
Setup Application Tests
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:
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(), '/');
});
});
Contributing
Installation
git clone <repository-url>
cd ember-qunit
npm install
Running tests
npm test
(Runs ember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
Running the dummy application
For more information on using ember-cli, visit https://ember-cli.com/.