How do I set up a Rails Angular project to test JS?

You can use teaspoon it runs your favorite test suite (default is jasmine) and they have a wiki to integrate with angular. Teaspoon integrates with the asset pipeline, so you can throw all the angular gems in there and require them in the teaspoon generated spec_helper.js. The other cool thing is: they have a guard … Read more

jasmine spies on inherited methods (with typescript) not working as expected with toHaveBeenCalled()

You have to understand the mechanics behind Typescript and the spying. First on Typescript … I’m ignoring the extra parens in class Parent(). Typescript uses prototypal inheritance behind the curtain. Thus a prototype will copy the referenced properties from the “base class” to the new class. This is what the for loop does in the … Read more

.detectChanges() not working within Angular test

It turns out this is due to using ChangeDetectionStrategy.OnPush in the component. Using OnPush only allows you to call .detectChanges() one time, so subsequent calls will fail to do anything. I’m not familiar enough with Angular to fully understand why. I was able to produce the required behaviour by overriding the ChangeDetectionStrategy in my TestBed … Read more

How do I add svg files via MatIconRegistry in unit tests?

can just do: import { MatIcon } from ‘@angular/material/icon’; import { MatIconTestingModule } from ‘@angular/material/icon/testing’; describe(‘MyComponent’, () => { beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [MyComponent, MatIcon], imports: [MatIconTestingModule], }).compileComponents(); })); … }); This will generate a test icon rendering without the HTTP request. NOTE: Newer versions of Angular, e.g., >16, may produce an error unless … Read more

Debugging jasmine-node tests with node-inspector

In short, just debug jasmine-node: node –debug-brk node_modules/jasmine-node/lib/jasmine-node/cli.js spec/my_spec.js If you look at the source of the jasmine-node script, it just invokes cli.js, and I found I could debug that script just fine. I wanted to use node-inspector to debug a CoffeeScript test. Just adding the –coffee switch worked nicely, e.g. node –debug-brk node_modules/jasmine-node/lib/jasmine-node/cli.js –coffee … Read more

How to test ‘private’ functions in an angular service with Karma and Jasmine

There is no way to test those functions. Their scope is the function that comprises your BracketService factory and they are invisible anyplace else. If you want to test them, then you have to expose them somehow. You can move them into their own service (which seems like overkill) or you can black box test … Read more

Testing AngularJS promises in Jasmine 2.0

After your call to promise.resolve(): Call $timeout.flush(). This will force a digest cycle and propagate the promise resolution Call done(). This tells Jasmine the async tests have completed Here’s an example (Demo on Plunker): describe(‘AngularJS promises and Jasmine 2.0’, function() { var $q, $timeout; beforeEach(inject(function(_$q_, _$timeout_) { // Set `$q` and `$timeout` before tests run … Read more

How do you debug Jasmine tests with Resharper?

Since I didn’t got debugger; to work I found another solution. By adding the following to my test, resharper won’t be notified that the test has finished so we can set debug breakpoints in the opened browser (I use chrome) and update (F5) the page. jasmine.getEnv().currentRunner_.finishCallback = function () {}; Since Jasmine 2.0 you need … Read more

tech