question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'this' undefined from component integration test?

See original GitHub issue

Also posted on SO: https://stackoverflow.com/questions/48359509/this-undefined-only-from-component-integration-test

import Ember from 'ember';

const { Component, computed, inject: { service } } = Ember;

export default Component.extend({
  currentUser: service(),

  classNames: ['list-group-item'],
  tagName: 'li',

  visibleReagents: computed.filter('inventory.reagents', function(reagent) {
    return this.get('currentUser.showArchivedReagents') || reagent.get('archivedAt');
           ^ undefined only in tests
  })
});

This would work well ^, but not as part of the component integration tests where this would be undefined:

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
import startMirage from '../../../../helpers/start-mirage';

moduleForComponent('inventories/left-menu-item', 'Integration | Component | inventories/left menu item', {
  integration: true,
  setup() {
    startMirage(this.container);
  },
  afterEach() {
    window.server.shutdown();
  }
});

test('it renders', function(assert) {
  let inventory = window.server.create('inventory', {
    title: 'My inventory', createdAt: '12/01/2017'
  });
  inventory.reagents = window.server.createList('reagent', 15, { inventory });

  this.set('inventory', inventory);
  this.render(hbs`{{inventories/left-menu-item inventory=inventory}}`);
  assert.equal(this.$('li').length, 1);
  assert.equal(this.$('.badge').length, '1');
});

Why isn’t this accessible from the test?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Serabecommented, Jan 11, 2018

Hi, @stephanebruckert,

Your demo project adds a ton of deps, both in package.json and in bower.json. Can you try reproducing this with a brand new project (ember new bug-reproduction) and just using Mirage (ember install ember-cli-mirage) and add the model, route, etc.?

There are too many deps to track down what is happening easily.

Thanks!

0reactions
stephanebruckertcommented, Jan 27, 2018

Thanks @luxferresum, as you suggested it can be worked around by doing:

filteredSubs: computed('dummy.subs', function() {
    return this.get('dummy.subs').filter(s => s);
})

Not an Ember issue, maybe an ember-cli-mirage issue being discussed here https://github.com/samselikoff/ember-cli-mirage/issues/1256

Thanks everyone

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Testing in Angular, Test Component is undefined
With my current code I get the error "Cannot read property 'dishForm' of undefined". So, it seems it does the DishAddComponent is not...
Read more >
Testing Components depending on Services - Testing Angular
An integration test that includes a real CounterService . This guide will demonstrate both. For your Components, you need to make a decision...
Read more >
Component testing scenarios - Angular
Error: This test module uses the component BannerComponent which is using a "templateUrl" or "styleUrls", but they were never compiled. Please call "TestBed....
Read more >
angular test cannot read properties of undefined - You.com
The issue most likely arrises from the html file rather than the unit test or component itself. Within your html file: Go to...
Read more >
React Unit Testing Using Enzyme and Jest - Toptal
In this post, we will use Jest and Enzyme to create a React.js component with basic functionality using TDD.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found