karinfdez
karinfdez

Reputation: 71

Unit test for helpers Ember-cli

When I create a new helper on Ember CLI, a test is created inside tests/unit/helpers. I would like to know how to test this helpers using the unit test?. I tried to find a documentation, but nothing helps. I need to test the function createArray in order to pass the test coverage to a 100%.

Now, this is my helper:

import Ember from 'ember';

export function createArray(array) {
    return array;
}

export default Ember.Helper.helper(createArray);

This is my unit test: tests/unit/helpers/create-array-test.js

module('Unit | Helper | create array');

// Replace this with your real tests.
test('it works', function(assert) {
  let result = createArray([42]);
  assert.ok(result);
});

Hope someone can guide me.

Upvotes: 2

Views: 279

Answers (2)

karinfdez
karinfdez

Reputation: 71

The code posted on twiddle helped me understand how to solve the helpers unit test. This is how I tested it, and it works like a charm.

test('Return the argument is passed', function(assert) {
  assert.ok(createArray([8,4,5,6]));
  assert.ok(createArray(['test1','test2','test3']));
});

Upvotes: 0

locks
locks

Reputation: 6577

What version of Ember CLI are you using? The blueprint should generate a test file with an import of the named export so you can use it directly.

What export function createArray does is create a named export named createArray. This means you can import the function directly and use it like you a normal function:

import { createArray } from 'app-name/helpers/create-array'

createArray(arrayOfArguments);

I modified @ykaragol's Twiddle to demonstrate how to do this in a test:

import { createArray } from 'app-name/helpers/create-array';
import { module, test } from 'qunit';

module('Unit | Helper | create array');

test('it works', function(assert) {
  let result = createArray([42]);
  assert.ok(result);
});

Replace app-name with the name of your application.

Upvotes: 2

Related Questions