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

assert.throws fails in component integration tests with ember 2.12 #256

Closed
sergproua opened this issue Mar 23, 2017 · 6 comments
Closed

Comments

@sergproua
Copy link

sergproua commented Mar 23, 2017

I installed Ember 2.12 and created new project with a component and a test to ensure it throws an error if required attribute is not provided. I can't get this test to pass.

npm install -g ember-cli@2.12
ember new ember-test
cd ember-test
ember g component dummy-component
...copy/paste code below...
ember test

dummy-component.js

import Ember from 'ember';

export default Ember.Component.extend({
  value: Ember.computed(() => {
    Ember.assert("Someone forgot to provide a required value attribute");
  })
});

dummy-component.hbs

{{value}}
{{yield}}

dummy-component-test.js

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('dummy-component', 'Integration | Component | dummy component', {
  integration: true
});

test('it throws', function(assert) {
  assert.throws(() => {
    this.render(hbs`{{dummy-component}}`);
  }, 'Error: Assertion Failed: Someone forgot to provide a required value attribute');
});

Error:

not ok 7 PhantomJS 2.1 - Integration | Component | dummy component: it throws
    ---
        actual: >
            false
        expected: >
            true
        stack: >
            exception@http://localhost:7357/assets/test-support.js:7664:49
            adapterDispatch@http://localhost:7357/assets/vendor.js:50288:22
            dispatchError@http://localhost:7357/assets/vendor.js:28557:23
            invokeWithOnError@http://localhost:7357/assets/vendor.js:10921:14
            flush@http://localhost:7357/assets/vendor.js:10977:15
            flush@http://localhost:7357/assets/vendor.js:11101:20
            end@http://localhost:7357/assets/vendor.js:11171:28
            run@http://localhost:7357/assets/vendor.js:11285:19
            run@http://localhost:7357/assets/vendor.js:33262:32
            render@http://localhost:7357/assets/test-support.js:8538:30
            http://localhost:7357/assets/tests.js:129:19
            throws@http://localhost:7357/assets/test-support.js:4609:17
            http://localhost:7357/assets/tests.js:128:18
            runTest@http://localhost:7357/assets/test-support.js:3696:34
            run@http://localhost:7357/assets/test-support.js:3682:13
            http://localhost:7357/assets/test-support.js:3860:15
            process@http://localhost:7357/assets/test-support.js:5094:26
            begin@http://localhost:7357/assets/test-support.js:5077:11
            http://localhost:7357/assets/test-support.js:4294:11
        message: >
            Error: Assertion Failed: Someone forgot to provide a required value attribute
        Log: |
            { type: 'error', text: 'null\n' }
    ...

Also asked here
http://stackoverflow.com/questions/42978212/ember-qunit-assert-throws-does-not-work

@sergproua
Copy link
Author

sergproua commented Mar 23, 2017

workarounds/solutions by @workmanw:

  1. use addon ember-qunit-assert-helpers
  2. this little helper
test('it throws', function (assert) {
  expectEmberAssert(assert, () => {
    this.render(hbs`{{dummy-component}}`);
  }, 'Someone forgot to provide a required value attribute');
});

@wmadden
Copy link

wmadden commented Jun 19, 2017

You can also solve this problem by using this.subject() instead of this.render():

import { moduleForComponent, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';

moduleForComponent('dummy-component', 'Unit | Component | dummy component', {
  unit: true,
});

test('it throws', function(assert) {
  assert.throws(() => {
    this.subject({ /* attributes */ });
  }, 'Error: Assertion Failed: Someone forgot to provide a required value attribute');
});

This has the advantage that it doesn't require an extra library, and uses a unit test instead of an integration test.

@Turbo87
Copy link
Member

Turbo87 commented Oct 14, 2017

It looks like https://github.com/workmanw/ember-qunit-assert-helpers is the best solution for now.

@Turbo87 Turbo87 closed this as completed Oct 14, 2017
danwenzel pushed a commit to danwenzel/ember-freestyle that referenced this issue Apr 9, 2018
@hoIIer
Copy link

hoIIer commented May 31, 2018

is there any way to do this w/o extra libraries using the newer test style?

@rwjblue
Copy link
Member

rwjblue commented Jun 1, 2018

@erichonkanen

https://discuss.emberjs.com/t/how-to-catch-errors-in-component-rendering-test/14854
emberjs/ember-test-helpers#310

@lvegerano
Copy link

This happens in utils test. I cant test for error throwing.

test('it throws if params are empty', function(assert) {
    const missingFirst = () => {
      throw new Error('foo');
    };
    assert.throws(missingFirst(), 'param 1 missing');
  });

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

No branches or pull requests

6 participants