Jasmine tests, how can I improve performance?

I am using jasmine

and karma

for unit tests of my application, with approx. 1000 tests so far, it takes about 10 seconds until they run out.

This is not a problem right now, but in a couple of months the number of tests could be much larger and I would like to know if there is anything I can do to make them run faster, at least locally.

I found out that using:

jasmine.any(Object)

      

much faster than comparing large objects.

Change:

expect(some.method).toHaveBeenCalledWith("xyz");

      

in

expect(some.method.calls.argsFor(0)[0]).toBe("xyz");

      

also looks a little faster.

Karma is fine, but it doesn't seem to have anything to improve performance, it's really useful for debugging ( reportSlowerThan

).

Any other ideas how I can improve my benchmark performance?

+3


source to share


1 answer


What performance improvements do you see when switching from toHaveBeenCalledWith

?

I appreciate what you are trying to achieve - you have a test suite that runs for 10 seconds and you are correct to try to improve this situation, but if the savings are in <500ms I would be careful as the readability and clarity of your tests are at risk.

toHaveBeenCalledWith

connects your intentions to others much better than the approach argsFor

, as does the message that will be displayed if this test fails;

Function expected to be called with "xyz"

vs

Expected undefined as "xyz"


With that said, some ideas ...

1



Find areas where you can safely replace calls beforeEach

;

The function beforeEach

is called once before each BOM in the description in which it is called

With challenges beforeAll

;

The function beforeAll

is called only once before running all specifications in the description.

But be careful not to introduce shared state between tests, which could skew your results (using Jasmine's option to run tests in random order might help here, but I'm not sure how to handle beforeAll

it that these specs still work together).

2

Keep using reportSlowerThan

as you were and culling whatever is really slow. If changes like the ones you suggested are unavoidable, put them behind well-named helper functions so that whatever you're trying to achieve is still clear to other developers. Or better yet, create custom mappings for these, because that will also lead to clear messages if the tests fail ( add-matchers can help make this easier).

3

Consider moving from Jasmine to Jest , tests are written in much the same way, but the test runner is much faster.

0


source







All Articles