Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2017,
sourceType: 'module'
ecmaVersion: 2018,
sourceType: 'module',
ecmafeatures: {
legacyDecorators: true
}
},
plugins: [
'ember'
Expand All @@ -17,6 +21,8 @@ module.exports = {
rules: {
'ember/new-module-imports': 'off',
'ember/no-restricted-resolver-tests': 'off',
'ember/no-test-import-export': 'off',
'ember/no-ember-testing-in-module-scope': 'off'
},
overrides: [
// node files
Expand Down
27 changes: 6 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ language: node_js
node_js:
# we recommend testing addons with the same minimum supported node version as Ember CLI
# so that your addon works for all apps
- "8"
- "10"

branches:
except:
- /^dependabot\/.*$/
only:
- master

addons:
chrome: stable
Expand All @@ -23,8 +23,8 @@ env:
matrix:
# we recommend new addons test the current and previous LTS
# as well as latest stable release (bonus points to beta/canary)
- EMBER_TRY_SCENARIO=ember-lts-3.4
- EMBER_TRY_SCENARIO=ember-lts-3.8
- EMBER_TRY_SCENARIO=ember-lts-3.16
- EMBER_TRY_SCENARIO=ember-lts-3.20
- EMBER_TRY_SCENARIO=ember-release
- EMBER_TRY_SCENARIO=ember-beta
- EMBER_TRY_SCENARIO=ember-canary
Expand All @@ -36,26 +36,11 @@ matrix:
- env: EMBER_TRY_SCENARIO=ember-canary

before_install:
- curl -o- -L https://yarnpkg.com/install.sh | bash
- export PATH=$HOME/.yarn/bin:$PATH
- npm i -g yarn

install:
- yarn install --no-lockfile --non-interactive

script:
- yarn lint
- yarn ember try:one $EMBER_TRY_SCENARIO

before_deploy:
- yarn global add auto-dist-tag@0.1
- auto-dist-tag --write

deploy:
- provider: npm
email: stefan.penner+ember-cli@gmail.com
api_key:
secure: PQRsq4JcFYPXOqov1jwPw63WnwYYpyKby/2akUjq8PHwwgCDEt34Z8yqQdJogqKmKRjijsiWu+b54fbIpZ6TMvqPL18F8FbCl6e2zoAY55VUmmVkx9wkvuPydTDs2fuPqk6LFvmIMiq+pjEPlSeWg3GpFSkFjfs0WZlO33xMyWw=
skip_cleanup: true
on:
tags: true
repo: emberjs/ember-mocha
497 changes: 0 additions & 497 deletions CHANGELOG.md

This file was deleted.

63 changes: 38 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,52 @@

ember-mocha
ember-mocha2
==============================================================================

[![Latest NPM release][npm-badge]][npm-badge-url]
[![TravisCI Build Status][travis-badge]][travis-badge-url]

[npm-badge]: https://img.shields.io/npm/v/ember-mocha.svg
[npm-badge-url]: https://www.npmjs.com/package/ember-mocha
[travis-badge]: https://img.shields.io/travis/emberjs/ember-mocha/master.svg
[travis-badge-url]: https://travis-ci.org/emberjs/ember-mocha
[npm-badge]: https://img.shields.io/npm/v/ember-mocha2.svg
[npm-badge-url]: https://www.npmjs.com/package/ember-mocha2
[travis-badge]: https://img.shields.io/travis/yads/ember-mocha/master.svg
[travis-badge-url]: https://travis-ci.org/yads/ember-mocha

ember-mocha simplifies testing of Ember applications with
ember-mocha2 simplifies testing of Ember applications with
[Mocha](https://mochajs.org/) by providing Mocha-specific wrappers around the
helpers contained in
[@ember/test-helpers](https://github.com/emberjs/ember-test-helpers).

*Upgrading from an earlier version? Have a look at our
[Migration Guide](docs/migration.md).*


Compatibility
------------------------------------------------------------------------------

- Ember.js v3.4 or above
- Ember CLI v2.13 or above
- Node.js 8 or above
- Ember.js v3.16 or above
- Ember CLI v3.16 or above
- Node.js 10 or above


Installation
------------------------------------------------------------------------------

`ember-mocha` is an [Ember CLI](http://www.ember-cli.com/) addon, so install it
`ember-mocha2` is an [Ember CLI](http://www.ember-cli.com/) addon, so install it
as you would any other addon:

```sh
$ ember install ember-mocha
$ ember install ember-mocha2
```

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.
or

```sh
$ yarn add ember-mocha2 --dev
```

if you want to keep `ember-mocha` you can point it at the ember-mocha-compat branch in your package.json.

```
"devDependencies": {
"ember-mocha": "yads/ember-mocha#ember-mocha-compat"
}
```

Usage
------------------------------------------------------------------------------
Expand All @@ -52,8 +57,6 @@ Ember testing APIs, as laid out in the RFCs
and
[268](https://github.com/emberjs/rfcs/blob/master/text/0268-acceptance-testing-refactor.md).

For the older APIs have a look at our [Legacy Guide](docs/legacy.md).

### Setting the Application

Your `tests/test-helper.js` file should look similar to the following, to
Expand All @@ -64,6 +67,9 @@ import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-mocha';
import chai from 'chai';
import chaiDom from 'chai-dom';
chai.use(chaiDom)

setApplication(Application.create(config.APP));
start();
Expand Down Expand Up @@ -115,6 +121,15 @@ This will make sure that in functions passed to `hooks.afterEach()` the
Mocha itself runs `afterEach` hooks in a different order than QUnit, which is
why this "workaround" is sometimes needed.

Sometimess you will need to specify that tests should not wait for settled state.
If you see timeout errors in an `afterEach` hook for your tests update your `setupTest`
cal:

```javascript
describe('SidebarController', function() {
setupTest({ waitForSettled: false });
```


### Setup Rendering Tests

Expand Down Expand Up @@ -192,23 +207,21 @@ test this library.
### Installation

```sh
npm install
yarn
```

### Testing

In order to test in the browser:

```sh
npm start
yarn test --server
```

... and then visit [http://localhost:4200/tests](http://localhost:4200/tests).

In order to perform a CI test:

```sh
npm test
yarn test
```


Expand Down
26 changes: 24 additions & 2 deletions addon-test-support/ember-mocha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,28 @@ import { loadTests } from './test-loader';
import setupTest from 'ember-mocha/setup-test';
import setupRenderingTest from 'ember-mocha/setup-rendering-test';
import setupApplicationTest from 'ember-mocha/setup-application-test';
import { it, afterEach } from 'mocha';
import { beforeEach, afterEach } from 'mocha';
import { setResolver, resetOnerror } from '@ember/test-helpers';
import Ember from 'ember';

/**
Ensures that `Ember.testing` is set to `true` before each test begins
(including `before` / `beforeEach`), and reset to `false` after each test is
completed. This is done via `beforeEach` and `afterEach`.
*/
export function setupEmberTesting() {
beforeEach(function() {
Ember.testing = true;
});

afterEach(function() {
Ember.testing = false;
});
}

function setupMocha(options) {
mocha.setup(options || {});
}

/**
* Instruct Mocha to start the tests.
Expand All @@ -27,9 +47,12 @@ function setupResetOnerror() {
* @param {Object} [options] Options to be used for enabling/disabling behaviors
* @param {Boolean} [options.loadTests] If `false` tests will not be loaded automatically.
* @param {Boolean} [options.startTests] If `false` tests will not be automatically started
* @param {Object} [options.mochaOptions] options to pass mocha setup method
* (you must run `startTests()` to kick them off).
*/
export function start(options = {}) {
setupMocha(options?.mochaOptions);

setupResetOnerror();

if (options.loadTests !== false) {
Expand All @@ -45,6 +68,5 @@ export {
setupTest,
setupRenderingTest,
setupApplicationTest,
it,
setResolver,
};
11 changes: 4 additions & 7 deletions addon-test-support/ember-mocha/setup-application-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {
setupApplicationContext,
teardownApplicationContext
setupApplicationContext
} from '@ember/test-helpers';
import setupTest from './setup-test';

export default function setupApplicationTest(options) {
export default function setupApplicationTest(_options) {
let options = _options === undefined ? { waitForSettled: true } : Object.assign({ waitForSettled: true }, _options);
let hooks = setupTest(options);

hooks.beforeEach(function() {
return setupApplicationContext(this);
});
hooks.afterEach(function() {
return teardownApplicationContext(this);
return setupApplicationContext(this._emberContext);
});

return hooks;
Expand Down
11 changes: 4 additions & 7 deletions addon-test-support/ember-mocha/setup-rendering-test.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {
setupRenderingContext,
teardownRenderingContext
setupRenderingContext
} from '@ember/test-helpers';
import setupTest from './setup-test';

export default function setupRenderingTest(options) {
export default function setupRenderingTest(_options) {
let options = _options === undefined ? { waitForSettled: true } : Object.assign({ waitForSettled: true }, _options);
let hooks = setupTest(options);

hooks.beforeEach(function() {
return setupRenderingContext(this);
});
hooks.afterEach(function() {
return teardownRenderingContext(this);
return setupRenderingContext(this._emberContext);
});

return hooks;
Expand Down
27 changes: 8 additions & 19 deletions addon-test-support/ember-mocha/setup-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,7 @@ import {
setupContext,
teardownContext
} from '@ember/test-helpers';
import { assign, merge } from '@ember/polyfills';
import { resolve } from 'rsvp';
import Ember from 'ember';

let teardownMandatorySetter;
if (Ember.__loader && Ember.__loader.registry && Ember.__loader.registry['@ember/-internals/utils/index']) {
teardownMandatorySetter = Ember.__loader.require('@ember/-internals/utils').teardownMandatorySetter;
}

const _assign = assign || merge;

function chainHooks(hooks, context) {
return hooks.reduce((promise, fn) => promise.then(fn.bind(context)), resolve());
Expand All @@ -30,37 +21,35 @@ function setupPauseTest(context) {
};
}

export default function setupTest(options) {
export default function setupTest(_options) {
let originalContext;
let beforeEachHooks = [];
let afterEachHooks = [];
let options = _options === undefined ? { waitForSettled: true } : Object.assign({ waitForSettled: true }, _options);

beforeEach(function() {
originalContext = _assign({}, this);
originalContext = Object.assign({}, this);
let context = new Proxy(this, {});
this._emberContext = context;

return setupContext(this, options)
return setupContext(this._emberContext, options)
.then(() => setupPauseTest(this))
.then(() => chainHooks(beforeEachHooks, this));
});

afterEach(function() {
return chainHooks(afterEachHooks, this)
.then(() => teardownContext(this))
.then(() => teardownContext(this._emberContext, options))
.then(() => {
// delete any extraneous properties
for (let key in this) {
if (!(key in originalContext)) {
// starting from Ember 3.13 this seems to be necessary
if (teardownMandatorySetter) {
teardownMandatorySetter(this, key);
}

delete this[key];
}
}

// copy over the original values
_assign(this, originalContext);
Object.assign(this, originalContext);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-mocha';
import { start } from 'ember-mocha2';
import chai from 'chai';
import chaiDom from 'chai-dom';
chai.use(chaiDom);

setApplication(Application.create(config.APP));

Expand Down
Loading