Error: 'ReferenceError: pauseTest is not defined' in integration tests with moduleForComponent syntax - ember.js

The pauseTest() function from ember-qunit does not work as expected in Integration tests with the old syntax
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
moduleForComponent('my-component, 'Integration | Component | MyComponent', {
integration: true
});
test('it renders', function(assert) {
return pauseTest();
this.render(hbs`{{my-component}}/>`);
...
}
Failed:
Died on test #1 at Module.callback (http://localhost:4200/assets/tests.js:118959:24)
at Module.exports (http://localhost:4200/assets/vendor.js:111:32)
at requireModule (http://localhost:4200/assets/vendor.js:32:18)
at EmberExamQUnitTestLoader.<anonymous> (http://localhost:4200/assets/test-support.js:29031:11)
at EmberExamQUnitTestLoader.require (http://localhost:4200/assets/test-support.js:29021:27)
at http://localhost:4200/assets/test-support.js:29545:90
at Array.forEach (<anonymous>): pauseTest is not defined# 698 ms
Source:
ReferenceError: pauseTest is not defined
at Object.<anonymous> (http://localhost:4200/assets/tests.js:118960:5)
at runTest (http://localhost:4200/assets/test-support.js:20889:30)
at Test.run (http://localhost:4200/assets/test-support.js:20875:6)
at http://localhost:4200/assets/test-support.js:21096:12
at advanceTaskQueue (http://localhost:4200/assets/test-support.js:20488:6)
at Object.advance (http://localhost:4200/assets/test-support.js:20469:4)
at begin (http://localhost:4200/assets/test-support.js:22241:20)
at http://localhost:4200/assets/test-support.js:21483:6
It's working fine in Acceptance tests, because of:
// ./tests/helpers/start-app.js
export default function startApp(attrs) {
...
application.injectTestHelpers();
...
}
How to make it works in Integration tests?
Note:
In modern syntax, it's working fine too:
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 | MyComponent', function(hooks) {
test('it renders', async function(assert) {
return this.pauseTest();
await render(hbs`{{my-component}}/>`);
});
}

workaround:
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import Ember from 'ember';
moduleForComponent('my-component, 'Integration | Component | MyComponent', {
integration: true
});
test('it renders', function(assert) {
return Ember.Test._helpers.pauseTest.method();
this.render(hbs`{{my-component}}/>`);
...
}
source: https://github.com/emberjs/ember-mocha/issues/75#issuecomment-263142520

Related

Create fake routes to test component's link-to

I have a component that gets menu items and renders navbar. So now I'm writing integration test, and I want to make sure, that component renders right links and labels. First of all, I added router initialization to make link-to display href prop:
moduleForComponent('main-menu', 'Integration | Component | main menu',
{
integration: true,
setup() {
const router = getOwner(this).lookup('router:main');
router.setupRouter();
}
});
Now I want to create some fake routes to test component, and to be independent from application router's setup. So I try to use map function:
moduleForComponent('main-menu', 'Integration | Component | main menu', {
integration: true,
setup() {
const router = getOwner(this).lookup('router:main');
router.map(function() {
this.route('link1');
this.route('link2');
});
router.setupRouter();
}
});
And I get Promise rejected before "it renders": router.map is not a function. So how should I implement "fake routes" for tests?
Ok, solved the problem. If someone will ever need something similar, here's how I did it:
import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import { getOwner } from '#ember/application';
import CustomRouter from 'app/lib/router';
moduleForComponent('main-menu', 'Integration | Component | main menu', {
integration: true,
setup() {
const application = getOwner(this),
Router = CustomRouter.extend()
;
Router.map(function() {
this.route('link1');
this.route('link2');
});
application.register('router:main', Router.extend());
application.lookup('router:main').setupRouter();
}
});
test('some awesome tests', function(assert) {
const menuItems = [
{url: 'link1', label: 'link1', href: '/link1'},
{url: 'link2', label: 'link2', href: '/link2'},
]
;
this.set('items', menuItems);
this.render(hbs`{{main-menu items=items}}`);
// some cool tests that now can check href attributes of links
// and don't depend on app's router setup
});

Upgrading Ember Simple Auth

I am having an issue upgrading ember simple auth.
TypeError: Cannot read property '__container__' of undefined
at authenticateSession (http://app.meetabe.dev:4200/assets/tests-a3c931e27860232e47d8de67d537cf75.js:298:24)
at Object.<anonymous> (http://app.meetabe.dev:4200/assets/tests-a3c931e27860232e47d8de67d537cf75.js:90:61)
I have properly imported the new helper methods using:
import { authenticateSession } from '../helpers/ember-simple-auth';
Any thoughts on what I need to do to get it working?
Here is my test, trying to visit an authenticated route.
import { test } from 'qunit';
import moduleForAcceptance from '../helpers/module-for-acceptance';
import { authenticateSession } from '../helpers/ember-simple-auth';
moduleForAcceptance('Acceptance | overview');
test('visiting /overview', function(assert) {
authenticateSession();
visit('/overview');
andThen(function() {
assert.equal(currentURL(), '/overview');
});
});
Pass the App to the authenticateSession method
import startApp from '../helpers/start-app';
let App;
moduleForAcceptance('Acceptance | home', {
beforeEach() {
App = startApp();
},
afterEach() {
Ember.run(App, App.destroy);
}
});
test('visiting /overview', function(assert) {
authenticateSession(App);
...
});

Ember component not updating in integration test when injected service is updated

I have a side-bar component which relies on side-bar service which is injected into it via initializer.
the component then has a computed property title which is tied to the same property on the service:
title: function () {
return this.get('sideBarService.title');
}.property('sideBarService.title'),
This works in the app itself but I cannot get the component to update in an integration test when the service is upated.
Here is my non working integration test:
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
container = application.__container__;
sideBarService = container.lookup('service:side-bar');
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
Ember.run(function () {
sideBarService.set('title', 'Hello');
});
this.render(hbs`
{{side-bar}}
`);
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
// fails
assert.deepEqual(content, serviceTitle);
});
Interestingly, if I debug in the test and grab the component with the console and then grab the sideBarService off of the component, it is aware of the updated title value and even the value title on the component itself seems to be updated but the dom never gets updated:
//debugged in browser console
var sb = container.lookup('component:side-bar')
undefined
sb.get('title')
"Hello"
sb.get('sideBarService.title')
"Hello"
this.$('.title').text().trim()
""
Is this a run loop issue? If so what do I need to do to set it off?
edit: In regards to Toran's comment. Does this look right?
var done = assert.async();
var content = this.$('.side-bar-content .title').text().trim();
var serviceTitle = sideBarService.get('title');
setTimeout(function() {
assert.deepEqual(content, serviceTitle);
done();
});
I would probably go about fixing this by avoiding the injection in the initializer and instead using the Ember.inject.service helper.
// component
import Ember from 'ember'
const { Component, inject, computed } = Ember;
const { service } = inject;
const { alias } = computed;
export default Component.extend({
sideBarService: service('side-bar'),
title: alias('sideBarService.title')
});
Then in your test, you can pass the service when you use the component.
import Ember from 'ember';
import startApp from '../helpers/start-app';
import hbs from 'htmlbars-inline-precompile';
import { moduleForComponent, test } from 'ember-qunit';
var application, container, sideBarService;
moduleForComponent('side-bar', 'Integration | side-bar',{
integration: true,
beforeEach: function() {
application = startApp();
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('it displays the correct title', function(assert) {
assert.expect(1);
this.set('sideBarService', Ember.Object.create({
title: 'hello'
}));
this.render(hbs`
{{side-bar sideBarService=sideBarService}}
`);
var title = this.$('.side-bar-content .title').text().trim();
assert.equal(title, 'hello'); // Hopefully passes
});

Ember test - Failed: Calling set on destroyed object

I'm currently trying to get started with writing tests (I know, I know, I should have done that before I wrote the application), but I'm currently stuck trying to test the login functionality.
Here is the test:
/* jshint expr:true */
import {
describe,
it,
beforeEach,
afterEach
} from 'mocha';
import { expect } from 'chai';
import Ember from 'ember';
import startApp from '../helpers/start-app';
describe('Integration: Authentication', function() {
var application;
beforeEach(function() {
application = startApp();
});
afterEach(function() {
Ember.run(application, 'destroy');
});
it('User can sign in', function() {
visit('/signin').then(function() {
fillIn('input[name="identification"]', 'username');
fillIn('input[name="password"]', 'password');
click('button[type="submit"]');
andThen(function() {
expect(currentPath()).to.equal('welcome');
})
});
});
});
Im getting this error:
Error: Assertion Failed: calling set on destroyed object (http://localhost:7357/a
ssets/vendor.js:19994)
Does anyone know how to solve this?
Thanks

Ember Test Failing For Unknown Reason

I'm new to Ember testing and have been trying to write up some tests.
My tests are written as such
// Ember Qoute Flow Test
import Ember from 'ember';
import { test, module } from 'qunit';
import startApp from '../helpers/start-app';
var App;
module("EmberQouteFlow-test", {
setup: function() {
App = startApp();
},
teardown: function() {
Ember.run(App, App.destroy);
}
});
test("simple test", function(assert) {
assert.expect(1);
visit('#/login');
// fillIn('input.everyWeek', 'My new post');
click('everyWeek', "everyWeek was selected");
click('button.submit');
// Wait for asynchronous helpers above to complete
});
test("Transaction Test", function(assert) {
assert.equal(1, 2, "This is a bad test");
});
test("A bad test", function(assert) {
assert.equal(1, 2, "This is a bad test");
});
Ember gives me this warning when I input "ember s" in the command line.
not ok 53 PhantomJS 1.9 - TestLoader Failures: client-consumer/tests/integration/QouteFlow/EmberQouteFlow-test: could not be loaded
actual: >
null
message: >
Died on test #1 at http://localhost:4200/assets/test-support.js:2809
at http://localhost:4200/assets/test-support.js:5574
at http://localhost:4200/assets/test-loader.js:31
at http://localhost:4200/assets/test-loader.js:21
at http://localhost:4200/assets/test-loader.js:40
at http://localhost:4200/assets/test-support.js:5578: 'undefined' is not a function (evaluating 'ember_qunit.module')
Log: >