jviotti jviotti - 1 year ago 1096
Javascript Question

TypeError: Attempted to assign to readonly property

I'm facing this error when writing a test for a directive (using



'use strict';

angular.module('jviotti.string', []).filter('capitalize', function() {

return function(input) {
return input.replace(/\w\S*/g, function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();



'use strict';

describe('Filter: capitalize', function () {

// load the controller's module

var capitalize;
beforeEach(inject(function($filter) {
capitalize = $filter('capitalize');

it('should capitalize a string', function () {


However, when running Karma with PhantomJS I get:

PhantomJS 1.9.2 (Mac OS X) Filter: capitalize should capitalize a string FAILED
TypeError: Attempted to assign to readonly property.
at workFn (/Users/jviotti/Projects/angular-string/bower_components/angular-mocks/angular-mocks.js:2107)
TypeError: 'undefined' is not a function (evaluating 'capitalize('hello')')
at /Users/jviotti/Projects/angular-string/test/spec/capitalize.js:14
PhantomJS 1.9.2 (Mac OS X): Executed 2 of 2 (1 FAILED) (0.1 secs / 0.017 secs)

What am I missing?

Answer Source

Change in karma.conf.js

  logLevel: config.LOG_INFO,


  logLevel: config.LOG_DEBUG,

and PhantomJS to Firefox, then You will get more verbose error message