Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-mocha

emberjs16.8kApache-2.0deprecated0.16.2TypeScript support: definitely-typed

Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.

Mocha helpers for testing Ember.js applications

ember-addon

readme

ember-mocha

Latest NPM release TravisCI Build Status

ember-mocha simplifies testing of Ember applications with Mocha by providing Mocha-specific wrappers around the helpers contained in @ember/test-helpers.

Upgrading from an earlier version? Have a look at our Migration Guide.

Compatibility

  • Ember.js v3.4 or above
  • Ember CLI v2.13 or above
  • Node.js 8 or above

Installation

ember-mocha is an Ember CLI addon, so install it as you would any other addon:

$ ember install ember-mocha

Some other addons are detecting the test framework based on the installed addon names and are expecting ember-cli-mocha instead. If you have issues with this then ember install ember-cli-mocha, which should work exactly the same.

Usage

The following section describes the use of Ember Mocha 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-mocha';

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 system
  • this.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 { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupTest } from 'ember-mocha';

describe('SidebarController', function() {
  setupTest();

  // Replace this with your real tests.
  it('exists', function() {
    let controller = this.owner.lookup('controller:sidebar');
    expect(controller).to.be.ok;
  });
});

If you find that test helpers from other addons want you to pass a hooks object you can do so like this:

let hooks = setupTest();
setupMirage(hooks);

This will make sure that in functions passed to hooks.afterEach() the this.owner and other things that setupTest() sets up are still available. Mocha itself runs afterEach hooks in a different order than QUnit, which is why this "workaround" is sometimes needed.

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 { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupRenderingTest } from 'ember-mocha';
import { render } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';

describe('GravatarImageComponent', function() {
  setupRenderingTest();

  it('renders', async function() {
    await render(hbs`{{gravatar-image}}`);
    expect(this.element.querySelector('img')).to.exist;
  });
});

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 { expect } from 'chai';
import { describe, it } from 'mocha';
import { setupApplicationTest } from 'ember-mocha';
import { visit, currentURL } from '@ember/test-helpers';

describe('basic acceptance test', function() {
  setupApplicationTest();

  it('can visit /', async function() {
    await visit('/');
    expect(currentURL()).to.equal('/');
  });
});

Upgrading

For instructions how to upgrade your test suite please read our Migration Guide.

Contributing

Contributions are welcome. Please follow the instructions below to install and test this library.

Installation

npm install

Testing

In order to test in the browser:

npm start

... and then visit http://localhost:4200/tests.

In order to perform a CI test:

npm test

Copyright 2014 Switchfly

This product includes software developed at Switchfly (http://www.switchfly.com).

NOTICE: Only our own original work is licensed under the terms of the Apache License Version 2.0. The licenses of some libraries might impose different redistribution or general licensing terms than those stated in the Apache License. Users and redistributors are hereby requested to verify these conditions and agree upon them.

changelog

Changelog

v0.16.2 (2019-11-23)

:rocket: Enhancement

  • #459 Prevent leakage of position fixed elements from the testing container (@nickschot)

:bug: Bug Fix

  • #462 Fix compatibility with Ember 3.13+ (@kobsy)

:house: Internal

Committers: 3

v0.16.1 (2019-10-08)

:rocket: Enhancement

Committers: 1

v0.16.0 (2019-06-16)

:boom: Breaking Change

:memo: Documentation

:house: Internal

Committers: 2

v0.15.1 (2019-05-11)

:rocket: Enhancement

  • #366 Deprecate setupTest(), setupComponentTest(), setupModelTest() and setupAcceptanceTest() functions (@Turbo87)

:house: Internal

Committers: 1

v0.15.0 (2019-05-11)

:boom: Breaking Change

  • #281 Replace automatic test start via timeout with explicit start() call (@Turbo87)
  • #219 Drop Node 4 support (@Turbo87)

:rocket: Enhancement

  • #332 Update @ember/test-helpers to v1.5.0
  • #310 Add resetOnerror() from @ember/test-helpers (@scalvert)
  • #280 Add ember-mocha Blueprint (@Turbo87)
  • #222 Update minimum version of @ember/test-helpers to 0.7.26. (@rwjblue)

Committers: 3

v0.14.0 (2018-06-05)

:rocket: Enhancement

:memo: Documentation

:house: Internal

Committers: 4

v0.13.1 (2018-02-05)

:bug: Bug Fix

  • #188 Adjust import paths for getContext() and setResolver(). (@Turbo87)

:house: Internal

  • #187 CI: Remove --skip-cleanup from try:one command. (@Turbo87)

Committers: 1

v0.13.0 (2018-02-03)

:boom: Breaking Change

:rocket: Enhancement

:bug: Bug Fix

:memo: Documentation

  • #175 Use lerna-changelog to generate friendly changelog. (@Turbo87)

:house: Internal

Committers: 1

v0.13.0-beta.4 (2018-02-03)

:bug: Bug Fix

  • #183 Add missing overrideTestCommandFilter() method. (@Turbo87)

:house: Internal

Committers: 1

v0.13.0-beta.3 (2017-12-22)

:rocket: Enhancement

:house: Internal

Committers: 1

v0.13.0-beta.2 (2017-12-15)

:bug: Bug Fix

Committers: 1

v0.13.0-beta.1 (2017-12-06)

:boom: Breaking Change

:memo: Documentation

  • #175 Use lerna-changelog to generate friendly changelog. (@Turbo87)

:house: Internal

Committers: 2

v0.12.0 (2017-05-26)

:rocket: Enhancement

  • #123 Improved async behavior deprecation message. (@Turbo87)
  • #124 README: Add instructions for a world with async/await. (@Turbo87)

:bug: Bug Fix

:house: Internal

Committers: 2

v0.11.1 (2017-05-07)

:bug: Bug Fix

:house: Internal

  • #147 Convert "loader.js" from bower to npm dependency. (@Turbo87)

Committers: 1

v0.11.0 (2016-12-09)

:rocket: Enhancement

Committers: 1

v0.10.0 (2016-11-29)

:house: Internal

  • #114 Update "ember-test-helpers" to v0.6.0-beta.1. (@Turbo87)

Committers: 1

v0.9.4 (2016-11-27)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 1

v0.9.3 (2016-11-23)

:rocket: Enhancement

  • #108 mocha-module: Add upgrade guide URL to the deprecation message. (@Turbo87)
  • #107 package.json: Add "files" section. (@Turbo87)

Committers: 1

v0.9.2 (2016-11-23)

:rocket: Enhancement

:bug: Bug Fix

  • #101 Fix tests by using the ESLint nodes correctly. (@Turbo87)

:memo: Documentation

:house: Internal

Committers: 1

v0.9.1 (2016-09-12)

:house: Internal

  • #91 Share Mocha's test context in setup-test-factory.. (@dgeb)

Committers: 1

  • Dan Gebhardt (dgeb)

v0.9.0 (2016-09-12)

:rocket: Enhancement

:memo: Documentation

:house: Internal

Committers: 2

v0.8.1 (2016-02-01)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 5

v0.8.10 (2016-02-01)

:bug: Bug Fix

  • #72 Ensure builds of ember-test-helpers are properly transpiled.. (@rwjblue)

Committers: 1

v0.8.6 (2015-10-27)

:rocket: Enhancement

Committers: 1

v0.8.4 (2015-10-02)

:rocket: Enhancement

Committers: 2

v0.8.3 (2015-09-13)

:rocket: Enhancement

  • #58 Update minimum version of ember-test-helpers.. (@rwjblue)
  • #57 Make beforeEach and afterEach use Ember.run. (@cowboyd)

Committers: 2

v0.8.2 (2015-08-27)

:rocket: Enhancement

  • #55 Update ember-test-helpers minimum version.. (@rwjblue)

Committers: 1

v0.8.0 (2015-06-19)

:rocket: Enhancement

Committers: 1

v0.6.3 (2015-05-13)

:rocket: Enhancement

  • #35 Bump ember-test-helpers, mocha, and chai. (@dgeb)

Committers: 1

  • Dan Gebhardt (dgeb)

v0.6.1 (2015-04-04)

:rocket: Enhancement

  • #32 Update to use newer ember-mocha-adapter. (@ef4)

Committers: 1

  • Edward Faulkner (ef4)

v0.6.0 (2015-03-24)

:rocket: Enhancement

Committers: 1

v0.4.4 (2015-02-22)

:rocket: Enhancement

Committers: 1

v0.4.1 (2015-02-08)

:house: Internal

  • #20 Switch from broccoli-cli to ember-cli.. (@dgeb)
  • #18 Use git-repo-version instead of git-repo-info directly.. (@dgeb)
  • #19 Upgrade ember to 1.10.0 and remove handlebars.. (@dgeb)

Committers: 1

  • Dan Gebhardt (dgeb)

v0.2.2 (2015-01-25)

:house: Internal

Committers: 1

v0.2.1 (2014-12-11)

:rocket: Enhancement

  • #9 toString method of wrapper of it should forward to callback.toString ins.... (@lyonlai)

Committers: 1

v0.1.3 (2014-11-26)

:rocket: Enhancement

  • #7 Add mocha to generated bower.json.. (@rwjblue)

Committers: 1

v0.1.2 (2014-11-26)

:rocket: Enhancement

  • #6 preserve mocha test context in both setup and assertion blocks. (@cowboyd)
  • #4 allow it, xit, it.skip and it.only forms.. (@cowboyd)

:bug: Bug Fix

Committers: 1