Precompile template strings within the tests of an Ember project via tagged template strings:
// ember-cli-project/test/unit/components/my-component-test.js
import { module, test } from 'qunit';
import { render } from '@ember/test-helpers';
import { setupRenderingTest, test } from 'ember-qunit';
import hbs from 'htmlbars-inline-precompile';
module('my-component', function(hooks) {
setupRenderingTest(hooks);
test('it renders', async function(assert) {
await render(hbs`hello ember testing!`);
assert.equal(this.element.innerText, 'hello ember testing!');
});
});
- Ember 2.12+
- Node 8+
- Babel 7+
Install the addon via ember install ember-cli-htmlbars-inline-precompile
Keep in mind that the source files are transformed, so the inline template
definitions are replaced with Ember.HTMLBars.template(…)
statements. This
means that you can't do fancy stuff like string interpolation within the
templates:
test('string interpolation within templates is NOT supported', async function(assert) {
let valuePath = 'greeting';
await render(hbs`
${valuePath}: <span>{{value}}</span>
`);
// the template will be "${valuePath}: <span>{{value}}</span>"
});
If you need stuff like this, you need to include ember-template-compiler.js
in your test-build and use Ember.HTMLBars.compile("…")
within your tests.