### Install Project Dependencies Source: https://github.com/emberjs/ember-qunit/blob/main/CONTRIBUTING.md Clone the repository and install project dependencies using pnpm. ```bash git clone cd ember-qunit pnpm install ``` -------------------------------- ### Install Dependencies Source: https://github.com/emberjs/ember-qunit/blob/main/test-buildtime-options-app/README.md Installs project dependencies using pnpm. Ensure you have pnpm installed globally. ```bash pnpm install ``` -------------------------------- ### Run the Dummy Application Source: https://github.com/emberjs/ember-qunit/blob/main/CONTRIBUTING.md Start the dummy application to view it in the browser. ```bash ember serve ``` -------------------------------- ### Install ember-qunit Addon Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Install the ember-qunit addon using the Ember CLI. This command adds the addon to your project. ```sh $ ember install ember-qunit ``` -------------------------------- ### Setup Application Tests with Ember-QUnit Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Employ `setupApplicationTest` for tests interacting with the entire application, such as acceptance tests. It boots the application instance and sets up DOM and routing helpers. ```javascript import { module, test } from 'qunit'; import { setupApplicationTest } from 'ember-qunit'; import { visit, currentURL } from '@ember/test-helpers'; module('basic acceptance test', function(hooks) { setupApplicationTest(hooks); test('can visit /', async function(assert) { await visit('/'); assert.equal(currentURL(), '/'); }); }); ``` -------------------------------- ### Setup `qunit-dom` in `test-helper.js` Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md If you encounter `assert.dom is not a function` errors after upgrading to ember-qunit v5.x.x, import and run `qunit-dom`'s `setup` function in your `tests/test-helper.js` file. ```javascript // tests/test-helper.js import * as QUnit from 'qunit'; import { setup } from 'qunit-dom'; //... setup(QUnit.assert); setApplication(Application.create(config.APP)); start(); //... ``` -------------------------------- ### Model Test Setup with Dependencies Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Sets up a model test for 'user' and specifies 'model:child' as a dependency. Use this when your model relies on other models. ```js import { test, moduleForModel } from 'ember-qunit'; moduleForModel('user', { needs: ['model:child'] }); test('It can set its child', function(assert) { assert.expect(1); var subject = this.subject(); var child = subject.store.createRecord('child'); subject.get('children').pushObject(child); assert.equal(subject.get('some-computed-value'), true); }); ``` -------------------------------- ### Install `qunit` and `@ember/test-helpers` Dependencies (yarn) Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md For migrating to v5.0.0 of ember-qunit, ensure `qunit` and `@ember/test-helpers` are installed as dev dependencies using yarn. ```sh # yarn users yarn add --dev qunit "@ember/test-helpers" ``` -------------------------------- ### Install `qunit` and `@ember/test-helpers` Dependencies (npm) Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md For migrating to v5.0.0 of ember-qunit, ensure `qunit` and `@ember/test-helpers` are installed as dev dependencies using npm. ```sh # npm users npm install --save-dev qunit "@ember/test-helpers" ``` -------------------------------- ### Setup Rendering Tests with Ember-QUnit Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Use `setupRenderingTest` for tests that render arbitrary templates, components, and helpers. It initializes Ember's renderer and sets up DOM interaction helpers. ```javascript import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { render, find } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; module('GravatarImageComponent', function(hooks) { setupRenderingTest(hooks); test('renders', async function() { await render(hbs`{{gravatar-image}}`); assert.ok(find('img')); }); }); ``` -------------------------------- ### Install ember-qunit Source: https://github.com/emberjs/ember-qunit/blob/main/docs/TEST_ISOLATION_VALIDATION.md Install the ember-qunit package to enable test isolation validation. Ensure you are using version 4.2.0 or higher. ```bash ember install ember-qunit ``` -------------------------------- ### Setup Unit Test with setupTest Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Use `setupTest()` in your unit tests to configure the test context with `this.owner` for Ember's Dependency Injection system, and provide methods like `this.set()`, `this.get()`, and `this.pauseTest()`. ```javascript import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; module('SidebarController', function(hooks) { setupTest(hooks); // Replace this with your real tests. test('exists', function() { let controller = this.owner.lookup('controller:sidebar'); assert.ok(controller); }); }); ``` -------------------------------- ### Setting Ember Resolver Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Configures the Ember resolver. Use the first example if you don't have a custom resolver. Otherwise, import and use your custom resolver. ```js // if you don't have a custom resolver, do it like this: setResolver(Ember.DefaultResolver.create({ namespace: App })); // otherwise something like: import Resolver from './path/to/resolver'; import { setResolver } from 'ember-qunit'; setResolver(Resolver.create()); ``` -------------------------------- ### Load Tests with AMD Loader Source: https://github.com/emberjs/ember-qunit/blob/main/README.md When using an AMD Loader, call `loadTests()` before `start()` in your `tests/test-helper.js` to ensure tests are loaded correctly. This is not necessary in ESM environments. ```javascript import Application from '../app'; import config from '../config/environment'; import { setApplication } from '@ember/test-helpers'; import { start } from 'ember-qunit'; import { loadTests } from 'ember-qunit/test-loader'; setApplication(Application.create(config.APP)); loadTests() start(); ``` -------------------------------- ### Enable Test Isolation Validation in ember-qunit Source: https://github.com/emberjs/ember-qunit/blob/main/docs/TEST_ISOLATION_VALIDATION.md Configure the `setupTestIsolationValidation: true` option within the `start` function in your `tests/test-helper.js` file to activate automatic detection of async leakage. ```javascript // tests/test-helper.js import Application from '../app'; import config from '../config/environment'; import { setApplication } from '@ember/test-helpers'; import { start } from 'ember-qunit'; setApplication(Application.create(config.APP)); start({ setupTestIsolationValidation: true }); ``` -------------------------------- ### Component Integration Test with ember-qunit Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Use for integration tests where the component is rendered as Ember would use it. Setup outer context values and actions using `this.set` and `this.on`. Render the component using `this.render(hbs`...`)` and interact with its template via `this.$()`. ```javascript import { hbs } from 'ember-cli-htmlbars'; import { test, moduleForComponent } from 'ember-qunit'; moduleForComponent('x-foo', { integration: true }); test('it renders', function(assert) { assert.expect(2); // setup the outer context this.set('value', 'cat'); this.on('action', function(result) { assert.equal(result, 'bar', 'The correct result was returned'); }); // render the component this.render(hbs` {{ x-foo value=value action="result" }} `); assert.equal(this.$('div>.value').text(), 'cat', 'The component shows the correct value'); this.$('button').click(); }); ``` -------------------------------- ### Original Test with `this.element` (Type Error) Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md This example demonstrates a test that would produce a type error in v6.1.0 of ember-qunit due to relying on `this.element` without explicit typing. ```typescript import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { hbs } from 'ember-cli-htmlbars'; module('', function (hooks) { setupRenderingTest(hooks); test('greets', async function (assert) { await render(hbs``); assert.equal(this.element.textContent?.trim(), 'Hello!'); }); }); ``` -------------------------------- ### Migrate Acceptance Tests from moduleForAcceptance to setupApplicationTest Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md Adopt `qunit`'s `module` and `test`, `setupApplicationTest()` from `ember-qunit`, and use `@ember/test-helpers` for `visit` and `currentURL`. Ensure `async`/`await` is used for asynchronous operations. ```javascript import { test } from 'qunit'; import moduleForAcceptance from 'app/tests/helpers/module-for-acceptance'; moduleForAcceptance('basic acceptance test'); test('can visit /', function() { visit('/'); andThen(() => { assert.equal(currentURL(), '/'); }); }); ``` ```javascript import { module, test } from 'qunit'; import { setupApplicationTest } from 'ember-qunit'; import { visit, currentURL } from '@ember/test-helpers'; module('basic acceptance test', function(hooks) { setupApplicationTest(hooks); test('can visit /', async function(assert) { await visit('/'); assert.equal(currentURL(), '/'); }); }); ``` -------------------------------- ### Migrate Component Tests from moduleForComponent to setupRenderingTest Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md Switch to `qunit`'s `module` and `test`, `setupRenderingTest()` from `ember-qunit`, and the `render()` helper from `@ember/test-helpers`. Use `async`/`await` for rendering and `this.element` for DOM access. ```javascript import { test, moduleForComponent } from 'ember-qunit'; import { hbs } from 'ember-cli-htmlbars'; moduleForComponent('GravatarImageComponent', { integration: true }); test('it renders', function(assert) { this.render(hbs`{{gravatar-image}}`); assert.equal(this.$('img').length, 1); }); ``` ```javascript import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { render } from '@ember/test-helpers'; import { hbs } from 'ember-cli-htmlbars'; module('GravatarImageComponent', function(hooks) { setupRenderingTest(hooks); test('renders', async function(assert) { await render(hbs`{{gravatar-image}}`); assert.ok(this.element.querySelector('img')); }); }); ``` -------------------------------- ### Migrate Unit Tests from moduleFor to setupTest Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md Use `qunit`'s `module` and `test` directly, and `setupTest()` from `ember-qunit` instead of `moduleFor()`. Access the owner object via `this.owner`. ```javascript import { test, moduleFor } from 'ember-qunit'; moduleFor('controller:sidebar', 'SidebarController', { // Specify the other units that are required for this test. // needs: ['controller:foo'] }); // Replace this with your real tests. test('exists', function(assert) { let controller = this.subject(); assert.ok(controller); }); ``` ```javascript import { module, test } from 'qunit'; import { setupTest } from 'ember-qunit'; module('SidebarController', function(hooks) { setupTest(hooks); // Replace this with your real tests. test('exists', function(assert) { let controller = this.owner.lookup('controller:sidebar'); assert.ok(controller); }); }); ``` -------------------------------- ### Build Application Source: https://github.com/emberjs/ember-qunit/blob/main/test-buildtime-options-app/README.md Builds the Ember application for deployment. Use the --environment production flag for a production-ready build. ```bash ember build ``` ```bash ember build --environment production ``` -------------------------------- ### Run Test Suite Source: https://github.com/emberjs/ember-qunit/blob/main/CONTRIBUTING.md Execute the test suite for the current Ember version or in watch mode. ```bash ember test ``` ```bash ember test --server ``` -------------------------------- ### Run Ember Tests Source: https://github.com/emberjs/ember-qunit/blob/main/addon/CONTRIBUTING.md Execute the test suite for the current Ember version. Use `--server` for watch mode or `ember try:each` for testing against multiple Ember versions. ```bash ember test ``` ```bash ember test --server ``` ```bash ember try:each ``` -------------------------------- ### Visit Application Tests Source: https://github.com/emberjs/ember-qunit/blob/main/test-app/README.md Access the application's test suite in the browser. Typically available at http://localhost:4200/tests. ```bash ember serve Visit your tests at [http://localhost:4200/tests](http://localhost:4200/tests). ``` -------------------------------- ### Run Tests Against Multiple Ember Versions Source: https://github.com/emberjs/ember-qunit/blob/main/CONTRIBUTING.md Use ember try:each to run the test suite against various Ember versions. ```bash ember try:each ``` -------------------------------- ### Configure Application for Testing Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Set up your `tests/test-helper.js` to correctly configure the Ember application for testing with `@ember/test-helpers`. Ensure `ENV.APP.autoboot` is set to `false` in your test environment configuration. ```javascript import Application from '../app'; import config from '../config/environment'; import { setApplication } from '@ember/test-helpers'; import { start } from 'ember-qunit'; setApplication(Application.create(config.APP)); start(); ``` -------------------------------- ### Lint Project Code Source: https://github.com/emberjs/ember-qunit/blob/main/CONTRIBUTING.md Run the linter to check for code style issues and fix them. ```bash pnpm lint ``` ```bash pnpm lint:fix ``` -------------------------------- ### Generate Code Source: https://github.com/emberjs/ember-qunit/blob/main/test-app/README.md Utilizes Ember CLI's code generation capabilities. Run `ember help generate` for a list of available generators. ```bash ember help generate ``` -------------------------------- ### Configure Ember-QUnit with Embroider Macros Source: https://github.com/emberjs/ember-qunit/blob/main/addon/README.md Configure ember-qunit in your `ember-cli-build.js` using `@embroider/macros`. Options include disabling container styles and setting the test runner theme. ```javascript 'use strict'; const EmberApp = require('ember-cli/lib/broccoli/ember-app'); module.exports = function (defaults) { const app = new EmberApp(defaults, { '@embroider/macros': { setConfig: { 'ember-qunit': { /** * default: false * * removes the CSS for the test-container (where the app and components are rendered to) */ disableContainerStyles: true, /** * default: 'qunit-default' * * options: 'qunit-default' | 'ember' * * Sets the theme for the Web UI of the test runner. Use a different value to disable loading any theme, allowing you to provide your own external one. */ theme: 'qunit-default', }, }, }, /* ... */ }); /* ... */ }; ``` -------------------------------- ### Add DOM Fixtures to `tests/index.html` Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md When upgrading to ember-qunit v5.0.0, you must manually add the QUnit and ember-testing DOM fixtures to your application's `tests/index.html` file. ```html
``` -------------------------------- ### Configure Ember-QUnit with Embroider Macros Source: https://github.com/emberjs/ember-qunit/blob/main/README.md Configure ember-qunit settings like `disableContainerStyles` and `theme` within your `ember-cli-build.js` file using `@embroider/macros`. ```javascript 'use strict'; const EmberApp = require('ember-cli/lib/broccoli/ember-app'); module.exports = function (defaults) { const app = new EmberApp(defaults, { '@embroider/macros': { setConfig: { 'ember-qunit': { /** * default: false * * removes the CSS for the test-container (where the app and components are rendered to) */ disableContainerStyles: true, /** * default: 'qunit-default' * options: 'qunit-default' | 'ember' * * Sets the theme for the Web UI of the test runner. Use a different value to disable loading any theme, allowing you to provide your own external one. */ theme: 'qunit-default', }, }, }, /* ... */ }); /* ... */ }; ``` -------------------------------- ### Component Unit Test with ember-qunit Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Use for unit tests to gain direct access to the component instance via `this.subject()`. Specify dependencies like helpers in the `needs: []` option. Render the component's template with `this.render()` or `this.$()`. ```javascript import { test, moduleForComponent } from 'ember-qunit'; moduleForComponent('x-foo', { unit: true, needs: ['helper:pluralize-string'] }); // run a test test('it renders', function(assert) { assert.expect(1); // creates the component instance var subject = this.subject(); // render the component on the page this.render(); assert.equal(this.$('.foo').text(), 'bar'); }); ``` -------------------------------- ### Controller Test with ember-qunit Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Use `moduleFor` for testing Ember Resolver objects like controllers or routes. Note that controllers and routes do not have rendering capabilities; use component or acceptance tests for rendering. ```javascript import { test, moduleFor } from 'ember-qunit'; moduleFor('controller:home'); test('It can calculate the result', function(assert) { assert.expect(1); var subject = this.subject(); subject.set('value', 'foo'); assert.equal(subject.get('result'), 'bar'); }); ``` -------------------------------- ### Async Test with Promise Rejection Handling Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Asserts that an asynchronous operation, represented by a Promise, is rejected. Catches the error and asserts its message. ```js test('sometimes async gets rejected', function(assert) { assert.expect(1); var myThing = MyThing.create() return myThing.exampleMethod().then(function() { assert.ok(false, "promise should not be fulfilled"); })['catch'](function(err) { assert.equal(err.message, "User not Authorized"); }); }); ``` -------------------------------- ### Test Using `qunit-dom` (No `this` Annotation Needed) Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md When using `qunit-dom`, tests that previously required `this` annotation for properties like `this.element` may no longer need it, as `qunit-dom` handles assertions differently. ```typescript import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { hbs } from 'ember-cli-htmlbars'; module('', function (hooks) { setupRenderingTest(hooks); test('greets', async function (assert) { await render(hbs``); assert.dom().hasText('Hello!'); }); }); ``` -------------------------------- ### Async Test with Promise Resolution Source: https://github.com/emberjs/ember-qunit/blob/main/docs/legacy.md Handles asynchronous operations within tests by returning a Promise. The test will automatically wait for the promise to resolve. Asserts that the promise is fulfilled. ```js // If you return a promise from a test callback it becomes an asyncTest. This // is a key difference between ember-qunit and standard QUnit. test('async is awesome', function(assert) { assert.expect(1); var myThing = MyThing.create(); // myThing.exampleMethod() returns a promise return myThing.exampleMethod().then(function() { assert.ok(myThing.get('finished')); }); }); ``` -------------------------------- ### Explicitly Type `this` for Rendering Tests Source: https://github.com/emberjs/ember-qunit/blob/main/docs/migration.md When using native TypeScript support in v6.1.0, explicitly type `this` as `RenderingTestContext` to resolve type errors related to properties like `this.element`. ```typescript import { module, test } from 'qunit'; import { setupRenderingTest } from 'ember-qunit'; import { hbs } from 'ember-cli-htmlbars'; import type { RenderingTestContext } from '@ember/test-helpers'; module('', function (hooks) { setupRenderingTest(hooks); test('greets', async function (this: RenderingTestContext, assert) { await render(hbs``); assert.equal(this.element.textContent?.trim(), 'Hello!'); }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.