1

I'm trying to build some tests in karma for an Angular app, and I am dabbling in ES6. I am trying to use arrow functions instead of the old format, but I am getting an error when I try to pass a function with multiple params into the inject function.

This works fine:

it('this is a test', () => {
    .....
});

This throws 'TypeError: Cannot read property '1' of null':

it('this is another test',inject(($state, $q, $httpBackend)=> {
    ....
});

although this works fine:

it('this is another test',inject(function($state, $q, $httpBackend) {
    ....
});

Any thoughts on why this would be? Thanks a bunch.

russdub
  • 245
  • 4
  • 9
  • Note that the other difference between your "working" and "not working" code is the use of the `inject` function. So perhaps it's that, rather than the multiple arguments, which is the difference causing the trouble. – T.J. Crowder Oct 02 '15 at 17:34
  • **All:** Russ and I have established that it isn't that `this` is wrong (through the simple expedient of checking whether `it('this is another test',inject(function($state, $q, $httpBackend) {/*..*/}.bind(this)));` works, and he says it does). – T.J. Crowder Oct 02 '15 at 17:36
  • Your second example is missing a closing `)`, both the ES5 and ES6 versions. Are you sure you didn't miss a transpilation error related to that, causing you to test old code? – T.J. Crowder Oct 02 '15 at 17:37
  • I think you are correct that it is something about the inject. I double checked my actual code, and it has the correct amount of closing parens, so I don't think that is an issue. – russdub Oct 02 '15 at 18:03
  • Checked in [Babel REPL](http://babeljs.io/repl/#?code=describe('suite'%2C%20function()%20%7B%0A%20%20it('this%20is%20another%20test'%2Cinject((%24state%2C%20%24q%2C%20%24httpBackend)%3D%3E%20%7B%0A%20%20%20%20expect(true).toBe(true)%3B%0A%20%20%7D))%3B%0A%7D)). Your ES6 code transpilled exactly to same code, which you say that works fine. Maybe issue inside content of the test function? – just-boris Oct 04 '15 at 11:04

1 Answers1

3

For anybody else running into this (https://xkcd.com/979), Angular (<1.5) injection is incompatible with ES6 arrow functions. Upgrade or use plain functions for inject/module.

It apparently relies on the toString to get the parameter names, and is broken by arrow functions :/ https://stackoverflow.com/a/32700361