How I Fixed: TypeError: ‘null’ is not an object (evaluating ‘currentSpec.$injector’)

 

I hit upon a problem in testing an Angular project this week that had me stumped for a while. The problem was this:

Every time I ran the test suite as a whole, they failed.

But if I ran each test file on its own… individually, they would pass.

I find more and more of my time lately is spent dealing with these sorts of things – they aren’t ‘development’ tasks, just annoyances that keep me from doing the thing I enjoy most – writing code.

Anyway, here’s the error:

I’m aware the coverage isn’t so good – but actually this is not the true coverage as I’d stripped out a whole bunch of tests by bastardising the  karma.conf.js file to try and isolate the problem. No… seriously, I promise 🙂

Anyway, it turned out that the solution to this was actually pretty simple.

Of course, nearly all solutions to programming problem seem simple once you have figured out the problem. Hindsight is such a wonderful thing.

But in this case, the problem was that a bunch of variables had been declared inside on the of  describe blocks:

And the solution was to simply move all the setup stuff outside of the  describe block:

An easy fix.

The real annoyance here was that I went through this whole project alphabetically, and this particular module began with the letter ‘P’, so I’d been through over half the code before I spotted it. Hours I will never get back.

Still, it’s fixed now, and hopefully now you can save a few hours if you ever suffer from this problem yourself.

 

Shares

Published by

Code Review

Code Review

CodeReviewVideos is a video training site helping software developers learn Symfony faster and easier.

4 thoughts on “How I Fixed: TypeError: ‘null’ is not an object (evaluating ‘currentSpec.$injector’)”

  1. I am getting the same error for a similar test. My project is not an angular js project but simple JS. I tried your way of writing test but still it does not work for me. What I tried doing is put my test cases in an existing test suite, it works fine for me. But now I am unable to add any extra test suite in my project. As soon as I add an extra suite in my project I get

    Caused by: java.lang.RuntimeException: There were javascript console errors.
    [ERROR]
    [ERROR] JavaScript Console Errors:
    [ERROR]
    [ERROR] * TypeError: null is not an object (evaluating ‘alert.appendChild’)
    [ERROR]
    [ERROR]
    [ERROR] at com.github.searls.jasmine.runner.ConsoleErrorChecker.checkForConsoleErrors(ConsoleErrorChecker.java:17)
    [ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:42)

    Any idea what is the cause of the issue?

    1. If this only happens when you add a new file, is it possible that there’s a list of known test files, or a regex pattern that matches test files with a specific name? (e.g. *.test.js)

      My best guess is it’s something configuration related. What happens if you copy / paste an existing working test file, and rename it slightly? Does that one fail, too?

Leave a Reply

Your email address will not be published. Required fields are marked *