0

filterTest.js

'use strict';
describe('test the reverse string filter',function(){

  var customFilter;
  beforeEach(module('directiveApp'));
  beforeEach(inject(function($filter){
      customFilter = $filter('reversing');
  }));
  it('passing a filter test',function(){
        expect(customFilter('HELLO')).toBe('OLLEH');
  });
});

filterModule.js

angular.module('directiveApp').filter('reversing',function(text){
  return text.split("").reverse().join("");
});

filterTest.js is my test file in which i am trying to test the filter that i have created in 'directiveApp' module which reverses the string. When i run karma start it was giving an error:

TypeError: undefined is not a function (evaluating 'customFilter('HELLO')') in /var/www/html/Directive/tests/testFilter.js (line 10) /var/www/html/Directive/tests/testFilter.js:10:25

I am unable figure it out, what wrong with it. Any help is appreciated. Thanks,

1 Answer 1

1

Your filter is not defined correctly. It should be:

angular.module('directiveApp').filter('reversing', function() {
  return function(text) {
    return text.split("").reverse().join("");
  };
});
Sign up to request clarification or add additional context in comments.

3 Comments

Even thought i have replaced the with this, its giving the same error.
It's most probably caused by a karma misconfiguration. Have you configure karma to load your filter JS file?
files: [ 'node_modules/angular/angular.js', 'node_modules/angular-mocks/angular-mocks.js', 'app/*.js', 'test/*.js' ],

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.