0
votes

I am currently busy with jasmine unit testing Angular. I have put this unit test in test.js

describe('The countInput filter', function(){
    var $filter;

    beforeEach(function(){
        module('countInputFilter');

        inject(function(_$filter_){
            $filter = _$filter_;
        });
    });

    it('Should not give an output when the length of the input is > 3', function(){

        var input = 'test';

        result = $filter('countInputFilter')(input);

        expect(result).toBe(null);
    });


});

When i run it with karma start karma.conf.js i get the error: Module is not defined.

In my karma.conf.js file i have the following files code:

files: [
  '../bower_components/angular/angular.js',
  '../bower_components/angular-mocks.js',
  '../bower_components/angular-route/angular-route.js',
  '../scripts/**/*.js',
  '../scripts/filters.js',
  'spec/**/*.js',
  'spec/*.js'
],

And the structure of my project is:

scripts
--filters.js
test
--spec
----test.js
--karma.conf.js

First time trying to unit test something with Jasmine but i don't see the error.

26 03 2017 11:29:10.103:WARN [watcher]: Pattern "C:/Users/user/Desktop/angular/bower_components/angular-mocks.js" does not match any file. 26 03 2017 11:29:10.134:WARN [karma]: No captured browser, open http://localhost:9876/ 26 03 2017 11:29:10.150:INFO [karma]: Karma v1.5.0 server started at http://0.0.0.0:9876/ 26 03 2017 11:29:10.150:INFO [launcher]: Launching browser Chrome with unlimited concurrency 26 03 2017 11:29:10.181:INFO [launcher]: Starting browser Chrome

1

1 Answers

1
votes

Check the module name.

The module name and the filter name can't be same.

I.e module('countInputFilter');

and

result = $filter('countInputFilter')(input);

If the module name in filter.js is countInput and the filtername is countInputFilter

Try below code :

describe('The countInput filter', function(){
  var $filter;

  beforeEach(function(){
    module('countInput');

    inject(function(_$filter_){
        $filter = _$filter_;
    });
  });

  it('Should not give an output when the length of the input is > 3', function(){

    var input = 'test';

    result = $filter('countInputFilter')(input);

    expect(result).toBe(null);
  });


});

Also you filter.js should be something like below :

angular.module('countInput', [])
  .filter('countInputFilter', function () {
    return function (x) {
        ...
    };
});

Can you also share your filter.js code.

Here is the sample jsfiddle link for unit testing filter