Skip to content

Instantly share code, notes, and snippets.

@jgwhite
Last active December 4, 2019 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgwhite/3f5589be011d2cabd8fa9fb8b0d64ee0 to your computer and use it in GitHub Desktop.
Save jgwhite/3f5589be011d2cabd8fa9fb8b0d64ee0 to your computer and use it in GitHub Desktop.
Ember {{assert}} helper
import { helper } from '@ember/component/helper';
import { assert } from '@ember/debug';
/**
* Verify that a certain expectation is met, or throw a exception
* otherwise.
*
* Delegates directly to `assert` from `@ember/debug`.
*
* @param {string} description - Describes the expectation. This will
* become the text of the Error thrown if the assertion fails.
* @param {boolean} condition - Must be truthy for the assertion to
* pass. If falsy, an exception will be thrown.
*
* @example
* {{assert "Please pass @someRequiredArg" @someRequiredArg}}
* @example
* {{assert "Please ensure @amount is greater than 0" (gt @amount 0)}}
*/
export default helper(([description, condition]) => {
assert(description, condition);
});
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, setupOnerror } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('Integration | Helper | assert', function(hooks) {
setupRenderingTest(hooks);
test('performs an assertion', async function(assert) {
const errors = [];
setupOnerror(e => errors.push(e));
await render(hbs`
{{assert "Please set this.foo" this.foo}}
`);
assert.deepEqual(
errors.map(e => e.message),
['Assertion Failed: Please set this.foo']
);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment