Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Attempted to assign to readonly property

I'm facing this error when writing a test for a directive (using generator-angular-module):

src/capitalize.js:

'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();
    });
  };

});

test/spec/capitalize.js:

'use strict';

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

  // load the controller's module
  beforeEach(module('jviotti.string'));

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

  it('should capitalize a string', function () {
    expect(capitalize('hello')).toBe('Hello');
  });

});

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?

like image 745
jviotti Avatar asked Nov 18 '13 18:11

jviotti


People also ask

Can not assign property to read only?

The error "Cannot assign to read only property of object" occurs when we try to change a property of an object that has been frozen or when a property has been defined with Object. defineProperties() . To solve the error, create a copy of the object or array, or set the property to writable .

Is read only error in JS?

The JavaScript strict mode-only exception "is read-only" occurs when a global variable or object property that was assigned to is a read-only property.

What is a read only property in JavaScript?

Definition and Usage The readOnly property sets or returns whether a text field is read-only, or not. A read-only field cannot be modified. However, a user can tab to it, highlight it, and copy the text from it. Tip: To prevent the user from interacting with the field, use the disabled property instead.

How do you assign a value to a readonly property in TypeScript?

Creating Read-Only Properties in TypeScript To create a read-only property, we prefix the keyword readonly before the property name. In the example below the price property is marked as readonly . We can assign a value to the price property when we initialize the object. However, we cannot change its value afterward.


2 Answers

Change in karma.conf.js

  logLevel: config.LOG_INFO,

to

  logLevel: config.LOG_DEBUG,

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

like image 178
Konrad Kuźnicki Avatar answered Oct 03 '22 07:10

Konrad Kuźnicki


An alternative to this (in Angular 1.2.2) is commenting out line 2107 inthe angular-mocks.js file:

if(e.stack && errorForStack) e.stack +=  '\n' + errorForStack.stack;

Apparently in PhantomJS the error stack is readonly.

Edit:

This error shouldn't occur any more in Angular 1.2.10+ (github.com/angular/angular.js/pull/5047) (Thanks sherlocken!)

like image 26
Akrikos Avatar answered Oct 03 '22 09:10

Akrikos