3 Common Problems When Writing Unit Tests In Jasmine

Learn about some of the most common problems you may face when writing unit tests in Jasmine and how to solve them.

Neil Smith
4 min readMay 29, 2021

In this article I’ll share 3 of the most common problems me and my colleagues have faced when writing unit tests for Angular Apps and how to solve them.

Photo by Yan Krukov from Pexels

We will go over things like

Why are my unit tests failing in pipelines but not locally? Why am I getting an error saying a function is not defined when it is? Why does my test only pass when I run that unit test by itself?

1. Unit Tests Fail In Pipelines But Not Locally

Sometimes you may run into a problem with the unit tests all passing locally but not when they run in a pipeline like on Azure. This is because you need to merge the latest changes to the branch you are trying to merge into, back into your local branch.

When you run tests on a PR pipeline they run the tests as if the changes have already been merged into that branch. Its testing whether the tests will pass after the merge. If you don’t have the latest changes to that branch then you will end up with different results, so make sure to keep your local branch updated when you are writing unit tests.

2. Functions Are Undefined

Sometimes when adding new unit tests you might get some errors with functions not being defined, especially when first adding a new .spec file for a component. For example something like

You may ask, why am I getting this error, that function does exist on the navigation service. This is because it doesn’t exist on the version of the navigation service your unit tests are using. Usually the real service is replaced with a mocked version of the navigation service and you may have forgotten to write that function on the mocked version of the service.

When setting up the unit tests you replace all the services with simpler versions where all the functions return a fixed value. Find the mocked version of the service being used in the providers section of the Test Bed at the top of the file and add that missing function to it.

3. The Unit Test Only Passes When Its The Only Test You Run

Sometimes we like to define some reusable test data that we use in several different unit tests. But be careful when using test data that has an Object type. Object’s get copied by reference instead of by value in assignment statements and when passed into functions. So when you use it in your tests it modifies the original version of the Object and doesn’t pass in a copy of the data. For example

const testData = { key: 'value' };
...
it('should not change the resuable test data in assignments', () => {
component.data = testData;
...
});
...
it('should not change the resuable test data in functions', () => {
component.changeData(testData);
...
});

If you are using this test data in multiple tests then each subsequent test can keep changing that test data and you’ll keep getting different results as the tests each affect each other.

If you forget to specify the type of your test data and use parentheses, then it defaults to the Object type, so make sure to define the data type of your test data.

const testData = { key: 'value' };

Otherwise you can just use the function cloneDeep() from the lodash library. This makes a copy of the object instead of copying its reference.

import cloneDeep from 'lodash/cloneDeep';const testData = { key: 'value' };
...
it('should not change the resuable test data in assignments', () => {
component.data = cloneDeep(testData);
...
});
...
it('should not change the resuable test data in functions', () => {
component.changeData(cloneDeep(testData));
...
});

Summary

1. Unit Tests Fail In Pipelines But Not Locally

Make sure to keep your branch up to date with the branch you are going to merge into.

2. Functions Are Undefined

Make sure to define all the functions and properties in your mocked services.

3. The Unit Test Only Passes When Its The Only Test You Run

Make sure to define the data types of your test data and be careful using objects.

--

--