Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add waitForSettled support and use latests ember-test-helpers #633

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not turn this rule off. The thing it is flagging is correct. Ember.testing should never be set during module evaluation.

},
overrides: [
// node files
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ 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:
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 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;
});
}
Comment on lines +13 to +26
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we explicitly dropping IE11 support in this release? IMHO, that should be decoupled from updating @ember/test-helpers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue would the babel transpiler not handle this for us?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue would the babel transpiler not handle this for us?

No, not by default. It is possible to configure this to transpile into core-js poly fills, but that requires the application has the polyfill installed/configured (which we can't assume). Additionally, we can't force the app to add the polyfill only in tests because then tests would pass in browsers that would never actually work in production/deployment.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So unfortunately with the Proxy solution in order to support the latest test-helpers we will either have to drop IE support or use https://github.com/GoogleChrome/proxy-polyfill. LMK if it still makes sense to decouple these.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rwjblue let me know what you would like to do regarding this? Should I use the proxy-polyfill or should we just drop IE support in this PR?

});
});

Expand Down
8 changes: 4 additions & 4 deletions config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ module.exports = function() {
useYarn: true,
scenarios: [
{
name: 'ember-lts-3.4',
name: 'ember-lts-3.16',
npm: {
devDependencies: {
'ember-source': '~3.4.0'
'ember-source': '~3.16.0'
}
}
},
{
name: 'ember-lts-3.8',
name: 'ember-lts-3.20',
npm: {
devDependencies: {
'ember-source': '~3.8.0'
'ember-source': '~3.20.0'
}
}
},
Expand Down
4 changes: 0 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ module.exports = {
let targetOptions = this.parent.options && this.parent.options['ember-mocha'];
// 2. check this.app.options['ember-mocha']
targetOptions = targetOptions || (this.app && this.app.options && this.app.options['ember-mocha']);
// 3. check this.parent.options['ember-cli-mocha']
targetOptions = targetOptions || (this.parent.options && this.parent.options['ember-cli-mocha']);
// 4. check this.app.options['ember-cli-mocha']
targetOptions = targetOptions || (this.app && this.app.options && this.app.options['ember-cli-mocha']);
this._targetOptions = targetOptions || {};
}

Expand Down
41 changes: 24 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,38 +31,45 @@
"test:all": "ember try:each"
},
"dependencies": {
"@ember/test-helpers": "^1.7.1",
"broccoli-funnel": "^2.0.2",
"broccoli-merge-trees": "^3.0.2",
"broccoli-funnel": "^3.0.3",
"broccoli-merge-trees": "^4.2.0",
"common-tags": "^1.8.0",
"ember-cli-babel": "^7.18.0",
"ember-cli-test-loader": "^2.2.0",
"mocha": "^2.5.3"
"ember-cli-test-loader": "^3.0.0"
},
"devDependencies": {
"ember-cli": "~3.16.0",
"@ember/optional-features": "^0.7.0",
"@ember/test-helpers": "^2.1.4",
"@glimmer/component": "^1.0.0",
"@glimmer/tracking": "^1.0.0",
"babel-eslint": "^10.1.0",
"ember-cli": "~3.24.0",
"ember-cli-chai": "^0.5.0",
"ember-cli-dependency-checker": "^3.2.0",
"ember-cli-htmlbars": "^4.2.2",
"ember-cli-htmlbars-inline-precompile": "^3.0.1",
"ember-cli-htmlbars": "^5.3.1",
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-pretender": "^3.2.0",
"ember-data": "~3.15.0",
"ember-cli-pretender": "^4.0.0",
"ember-data": "~3.24.0",
"ember-disable-prototype-extensions": "^1.1.2",
"ember-export-application-global": "^2.0.1",
"ember-load-initializers": "^2.1.1",
"ember-resolver": "^7.0.0",
"ember-source": "~3.16.2",
"ember-source-channel-url": "^2.0.1",
"ember-resolver": "^8.0.2",
"ember-source": "~3.24.0",
"ember-source-channel-url": "^3.0.0",
"ember-try": "^1.4.0",
"eslint": "^6.8.0",
"eslint-plugin-ember": "^7.8.1",
"eslint": "^7.19.0",
"eslint-plugin-ember": "^10.2.0",
"eslint-plugin-node": "^11.0.0",
"lerna-changelog": "^0.8.3",
"loader.js": "^4.7.0"
"loader.js": "^4.7.0",
"mocha": "^8.0.0"
},
"peerDependencies": {
"@ember/test-helpers": "^2.1.4",
"mocha": "^8.0.0"
},
"engines": {
"node": "8.* || >= 10.*"
"node": "10.* || >= 12"
},
"changelog": {
"repo": "emberjs/ember-mocha",
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/setup-application-test-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { expect } from 'chai';
import { setApplication, visit } from '@ember/test-helpers';
import Application from '../../app';
import config from '../../config/environment';
import hasEmberVersion from 'ember-test-helpers/has-ember-version';
import hasEmberVersion from '@ember/test-helpers/has-ember-version';

describe('setupApplicationTest', function() {
if (!hasEmberVersion(2, 4)) {
Expand Down
14 changes: 6 additions & 8 deletions tests/dummy/app/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import Application from '@ember/application';
import Resolver from './resolver';
import Resolver from 'ember-resolver';
import loadInitializers from 'ember-load-initializers';
import config from './config/environment';

const App = Application.extend({
modulePrefix: config.modulePrefix,
podModulePrefix: config.podModulePrefix,
Resolver
});
export default class App extends Application {
modulePrefix = config.modulePrefix;
podModulePrefix = config.podModulePrefix;
Resolver = Resolver;
}

loadInitializers(App, config.modulePrefix);

export default App;
3 changes: 0 additions & 3 deletions tests/dummy/app/resolver.js

This file was deleted.

10 changes: 4 additions & 6 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import EmberRouter from '@ember/routing/router';
import config from './config/environment';

const Router = EmberRouter.extend({
location: config.locationType,
rootURL: config.rootURL
});
export default class Router extends EmberRouter {
location = config.locationType;
rootURL = config.rootURL;
}

Router.map(function() {
this.route('foo');
});

export default Router;
1 change: 1 addition & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ module.exports = function(environment) {
ENV.APP.LOG_VIEW_LOOKUPS = false;

ENV.APP.rootElement = '#ember-testing';
ENV.APP.autoboot = false;
}

if (environment === 'production') {
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/resolver.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Ember from 'ember';
import AppResolver from '../../resolver';
import AppResolver from 'ember-resolver';
import config from '../../config/environment';
import { setResolver } from 'ember-test-helpers';
import { setResolver } from '@ember/test-helpers';

const Resolver = AppResolver.extend({
resolve: function(fullName) {
Expand Down
Loading