Search code examples
ember.jsember-cliember-qunit

Having trouble unit testing a helper from an addon project


https://github.com/stefanpenner/ember-cli/issues/2421

ember-cli: 1.2

I have a boilerplate addon project that has a title-case helper as follows:

My Helper app/helpers/title-case.js

import Ember from 'ember';

export default Ember.Handlebars.makeBoundHelper(function(string) {

    if (typeof string === 'string') {
        //replace dashes with spaces
        var str = string.dasherize().replace(/-/g, ' ');

        return str.replace(/\w\S*/g, function(word){
            return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase();
        });
    } else {
        return string;
    }

});

I Generated the test for the helper using ember-cli

ember g helper-test title-case

This was the output:

import {
  titleCase
} from 'boilerplate/helpers/title-case';

module('TitleCaseHelper');

// Replace this with your real tests.
test('it works', function() {
  var result = titleCase(42);
  ok(result);
});

Now running tests from ember-cli

ember test

Getting the following error:

Build failed.
File: dummy/tests/unit/helpers/title-case-test.js
ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'
Error: ENOENT, no such file or directory '/home/me/git/ember/boilerplate/tmp/tree_merger-tmp_dest_dir-PL6HFkuw.tmp/boilerplate/helpers/title-case.js'

UPDATE

I tried changing the following to "dummy" instead of the autogenerated "boilerplate" and removed the curly brackets.

//import {
//  titleCase
//} from 'dummy/helpers/title-case';
import titleCase from 'dummy/helpers/title-case';

it gets further now and into the test method but failed when calling titleCase(42) with:

TypeError: Cannot read property 'isUnbound' of undefined

UPDATE #2

I was able to get this working but it is ugly, I needed to access the ._rawFunction property and change the format of the import statement.

import titleCaseHelper from 'dummy/helpers/title-case';

var titleCase = titleCaseHelper._rawFunction;
module('TitleCaseHelper');

test('Title case lower case letters', function() {
      var result = titleCase('hello world');
      equal(result, 'Hello World');
});

I am still confused as to why the original generated test by ember-cli didn't work.


Solution

  • The helper-test generator expects the helper file to have a particlular format, which you can see if you generate the helper:

    ember generate helper hello
    

    That will generate the file app/helpers/hello.js, which contains

    import Ember from 'ember';
    
    export function hello(input) {
      return input;
    };
    
    export default Ember.Handlebars.makeBoundHelper(hello);
    

    It will also generate a unit test for the above in tests/unit/helpers/hello-test.js with

    import {
      hello
    } from 'ember-empty/helpers/hello';
    
    module('HelloHelper');
    
    // Replace this with your real tests.
    test('it works', function() {
      var result = hello(42);
      ok(result);
    });
    

    In other words, the helper-test generator expects that you also export the raw function itself, in addition to the bound helper.