What is @ember/test-helpers?
@ember/test-helpers is a package that provides a set of utilities to facilitate testing in Ember.js applications. It offers a variety of helpers to interact with the DOM, handle asynchronous operations, and manage application state during tests.
What are @ember/test-helpers's main functionalities?
Rendering Helpers
Rendering helpers allow you to render components or templates in the test environment. This is useful for testing the output and behavior of your UI components.
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
await render(hbs`<MyComponent />`);
DOM Interaction
DOM interaction helpers enable you to simulate user interactions such as clicking buttons or filling in input fields. This helps in testing how your application responds to user actions.
import { click, fillIn } from '@ember/test-helpers';
await click('.my-button');
await fillIn('.my-input', 'new value');
Asynchronous Helpers
Asynchronous helpers like `settled` ensure that all asynchronous operations have completed before proceeding with the test. This is crucial for testing asynchronous behavior in your application.
import { settled } from '@ember/test-helpers';
await settled();
Routing Helpers
Routing helpers allow you to navigate to different routes within your application during tests. This is useful for testing the behavior of your application in different states and routes.
import { visit } from '@ember/test-helpers';
await visit('/my-route');
Other packages similar to @ember/test-helpers
ember-qunit
ember-qunit provides QUnit-specific helpers for testing Ember.js applications. It integrates seamlessly with @ember/test-helpers and offers additional utilities for setting up and tearing down tests.
ember-mocha
ember-mocha offers Mocha-specific helpers for testing Ember.js applications. Similar to ember-qunit, it works well with @ember/test-helpers and provides a Mocha-based testing environment.
ember-cli-page-object
ember-cli-page-object is a library for creating page objects in Ember.js tests. It provides a higher-level API for interacting with your application's UI, making tests more readable and maintainable.
@ember/test-helpers
A test-framework-agnostic set of helpers for testing Ember.js applications.
Usage
This library is best used by way of a test-framework-specific
wrapper, such as ember-qunit or
ember-mocha.
Test Helpers
The exports of this library are intended to be utility functions that can be used to bring the
standard Ember testing experience to any testing framework.
A quick summary of the exports from the ember-test-helpers
module:
setApplication
- This function is used to allow the rest of the setup
functions to build a valid container/registry which can be used to lookup
instances and factories from your application.setResolver
- When setApplication
has not been ran, this function is used to allow the other
functions build a valid container/registry that is able to look objects up
from your application (just as a running Ember application would).setContext
- Invoked by the host testing framework to set the current testing context (generally
the this
within a running test).getContext
- Used to retrieve the current testing context.unsetContext
- Used to ensure that all handles on the testing context are released (allowing GC).setupContext
- Sets up a given testing context with owner
, get
, set
, etc properties.teardownContext
- Cleans up any objects created as part of the owner created in setupContext
.setupRenderingContext
- Sets up the provided context with the ability to render template
snippets by adding render
, clearRender
, etc.teardownRenderingContext
- Cleans up any work done in a rendering test.settled
- Returns a promise which will resolve when all async from AJAX, test waiters, and
scheduled timers have completed.validateErrorHandler
- Used to ensure that the Ember.onerror
error handler properly re-throws any errors during testing.
Collaborating
Installation
git clone <repository-url>
this repositorycd ember-test-helpers
yarn install
Running
Running Tests
yarn test
(Runs ember try:each
to test your addon against multiple Ember versions)ember test
ember test --server
Building
For more information on using ember-cli, visit https://ember-cli.com/.
Attribution
Much of ember-test-helpers
was extracted from the original ember-qunit
,
which was written by Stefan Penner, Robert Jackson, and Ryan Florence.
Copyright and License
Copyright 2015 Switchfly and contributors.
Dual-licensed under the Apache License, Version 2.0 and
the MIT License.