mardi 30 juin 2015

karma not finding angularjs directive in its own file

In a file dashboard.module.coffee I have the following declaration:

angular
  .module('app.dashboard', [])

In another file, stat.directive.coffee I have the following:

angular.module('app.dashboard')
  .directive('stat', ['$interval', statDirective])

statDirective contains the directive logic. This code works fine in a browser, i.e. the <stat> element works as expected, but the following Jasmine test does not render the element, it's simply an empty string:

describe "Stat", ->
  element = null
  scope = null

  beforeEach module 'app.dashboard'
  beforeEach module 'views/templates/dashboard/stat.html'
  beforeEach inject ($compile, $rootScope) ->
    scope = $rootScope.$new()
    element = $compile('<stat></stat>') scope

  it "contains some html", ->
    scope.$digest()

    expect(element.html()).toEqual('<div>hi</div>')

I've narrowed this down to the module being declared separately from the directive. If instead, the declaration was like this, the directive is found and rendered:

angular.module('app.dashboard', [])
  .directive('stat', ['$interval', statDirective])

Here, the only change is that the module and directive are being declared together, not in two files.

This looks to be an issue specifically with Karma as the code works just fine in a browser. Is there something missing in my Karma config to have this type of file structure work?

Thanks!

Aucun commentaire:

Enregistrer un commentaire