Ember Qunit — A Test driven development
Google Images

Ember Qunit — A Test driven development


What is Test Driven Development?

Software testing plays a vital role in the life cycle of software and Test Driven Development. It is imperative to identify bugs and errors during software development and increase the quality of the product. Therefore, one must focus on software testing. There are many approaches, and Test Driven approach is one of them. It is a key practice for extreme programming; it suggests that the code is developed or changed exclusively by the unit testing. TDD is a software-driven process that includes test-first development. It means that the developer first writes a fully automated test case before writing the production code to fulfill that test and refactoring. It is a practice of writing a (failing) test before writing the code of a feature. Feature code is refined until it passes the unit/integration test.

The TDD and Agile

The TDD approach originates from the Agile manifesto principles for Extreme programming. As the name suggests, the test process drives SDLC. Moreover, it’s a structuring practice that allows developers and testers to obtain optimized code that proves resilient in the long term. Agile development demands frequent feedback to develop the expected outcome. In simple terms, one can also term Agile development as Feedback Driven Development.

Unit Testing is a modular approach followed in testing, single-single functionality or part of the code tested without waiting for other of code to be completed.

Steps for the same are given below –

  • Firstly, add a test.
  • Run all the tests and see if any new test fails.
  • Update the code to make it pass the new tests.
  • Rerun the test and if they fail, then refactor again and repeat.

Introduction to Ember Qunit

Ember gives you the power to write tests and be productive from day one. You can be confident that your app will be correct today and years from now. A question remains: How should you write tests?

Since tests are a core part of the Ember framework and your development cycle, we will dedicate several sections to learning how to write tests.

How to Run Tests

You have a few options for running tests.

First, you can run the test suite by entering the command ember test, or ember t, in your terminal. This will run the suite just once.

Suppose, instead, you want the suite to run after every file change. You can enter ember test --server, or ember t -s.

Lastly, if you are already running a local development server (through ember server), you can visit the /tests URI. This will render the tests/index.html template.

# Run all tests once
ember test
ember t# Run all tests after every file change
ember test --server
ember t -s        

How to Filter Tests


When you are working on a single component or page, you will want only a small subset of tests to run after every file change. To specify which tests to run, you can add --module or --filter option to your command.

The --module option allows you to select a module—a group of tests that you specified in module() in QUnit, or describe() in Mocha.

# Button component example
ember test --server --module="Integration | Component | simple-button"
# Run tests for a location service
ember t -s -m="Unit | Service | location"        

The --filter option is more versatile. You can provide a phrase to match against the modules and test descriptions. A test description is what appears in test() in QUnit, or it() in Mocha.

# Button component example
ember test --server --filter="should show icon and label"
# Test everything related to your dashboard
ember t -s -f="Dashboard"
# Run integration tests
ember t -s -f="Integration"        

In QUnit, you can exclude tests by adding an exclamation point to the beginning of the filter, e.g. ember test --filter="!Acceptance". In Mocha, ember test --filter="Acceptance" --invert.

To learn more about options for testing, you can visit Ember CLI Documentation or type ember help test in the command line.

Testing Components

In Ember JS — The vital role plays in the terms of component, where all the business logic driven into a Isolated components by the developers.

So skipping the test cases for the componets will not be fruitful the product to run.

Components can be tested easily with a rendering test. Let’s see how this plays out in a specific example:

Let’s assume we have a component with a style property that is updated whenever the value of the name property changes. The style attribute of the component is bound to its style property.

You can follow along by generating your own component with ember generate component pretty-color.
app/components/pretty-color.js
import Component from '@glimmer/component';export default class PrettyColorComponent extends Component {
  get style() {
    return `color: ${this.args.name}`;
  }
}
app/components/pretty-color.hbs

<div style={{this.style}}>
  Pretty Color: {{@name}}
</div>        

The module from QUnit will scope your tests into groups of tests which can be configured and run independently. Make sure to call the setupRenderingTest function together with the hooks parameter first in your new module. This will do the necessary setup for testing your component for you, including setting up a way to access the rendered DOM of your component later on in the test, and cleaning up once your tests in this module are finished.

tests/integration/components/pretty-color-test.js

import { module } from 'qunit';
import { setupRenderingTest } from 'my-app-name/tests/helpers';
module('Integration | Component | pretty-color', function(hooks) {
  setupRenderingTest(hooks);
});        

Inside of your module and after setting up the test, we can now start to create our first test case. Here, we can use the QUnit.test helper and we can give it a descriptive name:

tests/integration/components/pretty-color-test.js

import { module, test } from 'qunit';
import { setupRenderingTest } from 'my-app-name/tests/helpers';
module('Integration | Component | pretty-color', function(hooks) {
  setupRenderingTest(hooks);
test('should change colors', async function(assert) {
});
});        

What is code coverage

Istanbul: https://istanbul.js.org/

How Istanbul works

Istanbul instruments your ES5 and ES2015+ JavaScript code with line counters, so that you can track how well your unit-tests exercise your codebase.

The nyc command-line-client for Istanbul works well with most JavaScript testing frameworks: tap, mocha, AVA, etc.

#Features

  • First class support of ES6/ES2015+ using babel-plugin-istanbul.
  • A collection of reporters, providing both terminal and HTML output:
  • Support for the most popular JavaScript testing frameworks (see our tutorials).
  • Support for instrumenting subprocesses, using the nyc command-line-interface.

GitHub - kategengler/ember-cli-code-coverage: Code coverage for ember apps using Istanbul

Code coverage using Istanbul for Ember apps. If using Mocha, Testem >= 1.6.0 for which you need ember-cli > 2.4.3 If…github.com

Final thoughts

We need to embrace ourself with tons of interest, to do the Test driven developement. You can see its’ value once the project and team grows.

#happy_coding #TDD_approach

To view or add a comment, sign in

More articles by Anand Gopinath

Others also viewed

Explore content categories