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

Ember-Component: Failing test when throwing Errors in init() despite using workaround #18880

Closed
jnfingerle opened this issue Apr 17, 2020 · 1 comment · Fixed by #18878
Closed

Comments

@jnfingerle
Copy link

Hi,

I'm upgrading a large app to Octane. A number of our Ember.Components throw exceptions in init(). Our test have the workaround for render() not throwing: We have our own Ember.onerror for the test. This worked before. With current Ember this works at first, but in cases where the Component is the only thing in the template, this leads to another Error "TypeError: Cannot read property 'firstNode' of null".

For whatever reason I couldn't recreate this as a twiddle, but here's my code:

https://github.com/jnfingerle/ember-issue-component-test

test-component.js:

import Component from '@ember/component';

export default class TestComponent extends Component {
    init(){
        throw new Error('Error');
    }
} 

test-component-test.js:

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { hbs } from 'ember-cli-htmlbars';
import Ember from 'ember';

module('Integration | Component | test-component', function(hooks) {
  setupRenderingTest(hooks);

  test('rejects doesn\'t catch exception', async function(assert) {
      assert.expect(1);
    assert.rejects(render(hbs`<TestComponent />`));
  });

  test('Workaround: errorHandler works at first, but gets called twice (2nd:  TypeError: Cannot read property \'firstNode\' of null)', async function(assert) {
      assert.expect(1);
    const originalErrorHandler = Ember.onerror;
    try {
        Ember.onerror = function(error){
            assert.ok(true, 'On Error:' + error);
        }
        await render(hbs`<TestComponent />`);
    } finally {
        Error.onerror = originalErrorHandler;
    }
  });

  test('with additional <div>s all works as expected', async function(assert) {
      assert.expect(1);
    const originalErrorHandler = Ember.onerror;
    try {
        Ember.onerror = function(error){
            assert.ok(true, 'On Error:' + error);
        }
        await render(hbs`<div><TestComponent /></div>`);
    } finally {
        Error.onerror = originalErrorHandler;
    }
  });
});

package.json

{
  "name": "component-test",
  "version": "0.0.0",
  "private": true,
  "description": "Small description for component-test goes here",
  "repository": "",
  "license": "MIT",
  "author": "",
  "directories": {
    "doc": "doc",
    "test": "tests"
  },
  "scripts": {
    "build": "ember build --environment=production",
    "lint": "npm-run-all --aggregate-output --continue-on-error --parallel lint:*",
    "lint:hbs": "ember-template-lint .",
    "lint:js": "eslint .",
    "start": "ember serve",
    "test": "npm-run-all lint:* test:*",
    "test:ember": "ember test"
  },
  "devDependencies": {
    "@ember/optional-features": "^1.3.0",
    "@glimmer/component": "^1.0.0",
    "@glimmer/tracking": "^1.0.0",
    "babel-eslint": "^10.1.0",
    "broccoli-asset-rev": "^3.0.0",
    "ember-auto-import": "^1.5.3",
    "ember-cli": "~3.17.0",
    "ember-cli-app-version": "^3.2.0",
    "ember-cli-babel": "^7.18.0",
    "ember-cli-dependency-checker": "^3.2.0",
    "ember-cli-htmlbars": "^4.2.3",
    "ember-cli-inject-live-reload": "^2.0.2",
    "ember-cli-sri": "^2.1.1",
    "ember-cli-uglify": "^3.0.0",
    "ember-data": "~3.17.0",
    "ember-export-application-global": "^2.0.1",
    "ember-fetch": "^7.0.1",
    "ember-load-initializers": "^2.1.1",
    "ember-maybe-import-regenerator": "^0.1.6",
    "ember-qunit": "^4.6.0",
    "ember-resolver": "^7.0.0",
    "ember-source": "~3.17.0",
    "ember-template-lint": "^2.4.0",
    "ember-welcome-page": "^4.0.0",
    "eslint": "^6.8.0",
    "eslint-plugin-ember": "^7.10.1",
    "eslint-plugin-node": "^11.0.0",
    "loader.js": "^4.7.0",
    "npm-run-all": "^4.1.5",
    "qunit-dom": "^1.1.0"
  },
  "engines": {
    "node": "10.* || >= 12"
  },
  "ember": {
    "edition": "octane"
  }
}
@pzuraq
Copy link
Contributor

pzuraq commented Apr 17, 2020

This will be fixed by #18878

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants