JavaScript Unit Testing
Click ★ if you like the project. Your contributions are heartily ♡ welcome.
Unit = Unit of work
This could involve multiple methods and classes invoked by some public API that can:
A unit test should test the behaviour of a unit of work: for a given input, it expects an end result that can be any of the above.
Unit tests are isolated and independent of each other
The code is designed to support this independence (see "Design principles" below).
Unit tests are lightweight tests
Unit tests are code too
They should meet the same level of quality as the code being tested. They can be refactored as well to make them more maintainable and/or readable.
The key to good unit testing is to write testable code. Applying simple design principles can help, in particular:
The goal of these guidelines is to make your tests:
These are the 3 pillars of good unit testing.
All the following examples assume the usage of the Jasmine framework.
TDD is a design process, not a testing process. TDD is a robust way of designing software components ("units") interactively so that their behaviour is specified through unit tests.
How? Why?
Note that code written without a test-first approach is often very hard to test.
Don't hesitate to nest your suites to structure logically your tests in subsets.
:(
describe('A set of functionalities', () => {
it('a set of functionalities should do something nice', () => {
});
it('a subset of functionalities should do something great', () => {
});
it('a subset of functionalities should do something awesome', () => {
});
it('another subset of functionalities should also do something great', () => {
});
});
:)
describe('A set of functionalities', () => {
it('should do something nice', () => {
});
describe('A subset of functionalities', () => {
it('should do something great', () => {
});
it('should do something awesome', () => {
});
});
describe('Another subset of functionalities', () => {
it('should also do something great', () => {
});
});
});
Tests names should be concise, explicit, descriptive and in correct English. Read the output of the spec runner and verify that it is understandable! Keep in mind that someone else will read it too. Tests can be the live documentation of the code.
:(
describe('MyGallery', () => {
it('init set correct property when called (thumb size, thumbs count)', () => {
});
// ...
});
:)
describe('The Gallery instance', () => {
it('should properly calculate the thumb size when initialized', () => {
});
it('should properly calculate the thumbs count when initialized', () => {
});
// ...
});
In order to help you write test names properly, you can use the "unit of work - scenario/context - expected behaviour" pattern:
describe('[unit of work]', () => {
it('should [expected behaviour] when [scenario/context]', () => {
});
});
Or whenever you have many tests that follow the same scenario or are related to the same context:
describe('[unit of work]', () => {
describe('when [scenario/context]', () => {
it('should [expected behaviour]', () => {
});
});
});
For example:
:) :)
describe('The Gallery instance', () => {
describe('when initialized', () => {
it('should properly calculate the thumb size', () => {
});
it('should properly calculate the thumbs count', () => {
});
});
// ...
});
Never. Ever. Tests have a reason to be or not.
Don't comment them out because they are too slow, too complex or produce false negatives. Instead, make them fast, simple and trustworthy. If not, remove them completely.
Always use simple statements. Don't use loops and/or conditionals. If you do, you add a possible entry point for bugs in the test itself:
:(
it('should properly sanitize strings', () => {
let result;
const testValues = {
'Avion' : 'Avi' + String.fromCharCode(243) + 'n',
'The-space' : 'The space',
'Weird-chars-' : 'Weird chars!!',
'file-name.zip' : 'file name.zip',
'my-name.zip' : 'my.name.zip'
};
for (result in testValues) {
expect(sanitizeString(testValues[result])).toBe(result);
}
});
:)
it('should properly sanitize strings', () => {
expect(sanitizeString('Avi'+String.fromCharCode(243)+'n')).toBe('Avion');
expect(sanitizeString('The space')).toBe('The-space');
expect(sanitizeString('Weird chars!!')).toBe('Weird-chars-');
expect(sanitizeString('file name.zip')).toBe('file-name.zip');
expect(sanitizeString('my.name.zip')).toBe('my-name.zip');
});
Better: write a test for each type of sanitization. It will give a nice output of all possible cases, improving maintainability.
:) :)
it('should sanitize a string containing non-ASCII chars', () => {
expect(sanitizeString('Avi'+String.fromCharCode(243)+'n')).toBe('Avion');
});
it('should sanitize a string containing spaces', () => {
expect(sanitizeString('The space')).toBe('The-space');
});
it('should sanitize a string containing exclamation signs', () => {
expect(sanitizeString('Weird chars!!')).toBe('Weird-chars-');
});
it('should sanitize a filename containing spaces', () => {
expect(sanitizeString('file name.zip')).toBe('file-name.zip');
});
it('should sanitize a filename containing more than one dot', () => {
expect(sanitizeString('my.name.zip')).toBe('my-name.zip');
});
Remember, unit tests are a design specification of how a certain behaviour should work, not a list of observations of everything the code happens to do.
:(
it('should multiply the number passed as parameter and subtract one', () => {
const multiplySpy = spyOn(Calculator, 'multiple').and.callThrough();
const subtractSpy = spyOn(Calculator, 'subtract').and.callThrough();
const result = Calculator.compute(21.5);
expect(multiplySpy).toHaveBeenCalledWith(21.5, 2);
expect(subtractSpy).toHaveBeenCalledWith(43, 1);
expect(result).toBe(42);
});
:)
it('should multiply the number passed as parameter and subtract one', () => {
const result = Calculator.compute(21.5);
expect(result).toBe(42);
});
This will improve maintainability. Your test is no longer tied to implementation details.
:(
describe('Saving the user profile', () => {
let profileModule;
let notifyUserSpy;
let onCompleteSpy;
beforeEach(() => {
profileModule = new ProfileModule();
notifyUserSpy = spyOn(profileModule, 'notifyUser');
onCompleteSpy = jasmine.createSpy();
});
it('should send the updated profile data to the server', () => {
jasmine.Ajax.install();
profileModule.save();
const request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe('/profiles/1');
expect(request.method).toBe('POST');
expect(request.data()).toEqual({ username: 'mawrkus' });
jasmine.Ajax.uninstall();
});
it('should notify the user', () => {
jasmine.Ajax.install();
profileModule.save();
expect(notifyUserSpy).toHaveBeenCalled();
jasmine.Ajax.uninstall();
});
it('should properly execute the callback passed as parameter', () => {
jasmine.Ajax.install();
profileModule.save(onCompleteSpy);
jasmine.Ajax.uninstall();
expect(onCompleteSpy).toHaveBeenCalled();
});
});
The setup code should apply to all the tests:
:)
describe('Saving the user profile', () => {
let profileModule;
beforeEach(() => {
jasmine.Ajax.install();
profileModule = new ProfileModule();
});
afterEach( () => {
jasmine.Ajax.uninstall();
});
it('should send the updated profile data to the server', () => {
profileModule.save();
const request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toBe('/profiles/1');
expect(request.method).toBe('POST');
});
it('should notify the user', () => {
spyOn(profileModule, 'notifyUser');
profileModule.save();
expect(profileModule.notifyUser).toHaveBeenCalled();
});
it('should properly execute the callback passed as parameter', () => {
const onCompleteSpy = jasmine.createSpy();
profileModule.save(onCompleteSpy);
expect(onCompleteSpy).toHaveBeenCalled();
});
});
Consider keeping the setup code minimal to preserve readability and maintainability.
Factories can:
There's a trade-off to find here between applying the DRY principle and readability.
:(
describe('User profile module', () => {
let profileModule;
let pubSub;
beforeEach(() => {
const element = document.getElementById('my-profile');
pubSub = new PubSub({ sync: true });
profileModule = new ProfileModule({
element,
pubSub,
likes: 0
});
});
it('should publish a topic when a new "like" is given', () => {
spyOn(pubSub, 'notify');
profileModule.incLikes();
expect(pubSub.notify).toHaveBeenCalledWith('likes:inc', { count: 1 });
});
it('should retrieve the correct number of likes', () => {
profileModule.incLikes();
profileModule.incLikes();
expect(profileModule.getLikes()).toBe(2);
});
});
:)
describe('User profile module', () => {
function createProfileModule({
element = document.getElementById('my-profile'),
likes = 0,
pubSub = new PubSub({ sync: true })
}) {
return new ProfileModule({ element, likes, pubSub });
}
it('should publish a topic when a new "like" is given', () => {
const pubSub = jasmine.createSpyObj('pubSub', ['notify']);
const profileModule = createProfileModule({ pubSub });
profileModule.incLikes();
expect(pubSub.notify).toHaveBeenCalledWith('likes:inc');
});
it('should retrieve the correct number of likes', () => {
const profileModule = createProfileModule({ likes: 40 });
profileModule.incLikes();
profileModule.incLikes();
expect(profileModule.getLikes()).toBe(42);
});
});
Factories are particularly useful when dealing with the DOM:
:(
describe('The search component', () => {
describe('when the search button is clicked', () => {
let container;
let form;
let searchInput;
let submitInput;
beforeEach(() => {
fixtures.inject(`<div id="container">
<form class="js-form" action="/search">
<input type="search">
<input type="submit" value="Search">
</form>
</div>`);
container = document.getElementById('container');
form = container.getElementsByClassName('js-form')[0];
searchInput = form.querySelector('input[type=search]');
submitInput = form.querySelector('input[type=submith]');
});
it('should validate the text entered', () => {
const search = new Search({ container });
spyOn(search, 'validate');
search.init();
input(searchInput, 'peace');
click(submitInput);
expect(search.validate).toHaveBeenCalledWith('peace');
});
// ...
});
});
:)
function createHTMLFixture() {
fixtures.inject(`<div id="container">
<form class="js-form" action="/search">
<input type="search">
<input type="submit" value="Search">
</form>
</div>`);
const container = document.getElementById('container');
const form = container.getElementsByClassName('js-form')[0];
const searchInput = form.querySelector('input[type=search]');
const submitInput = form.querySelector('input[type=submith]');
return {
container,
form,
searchInput,
submitInput
};
}
describe('The search component', () => {
describe('when the search button is clicked', () => {
it('should validate the text entered', () => {
const { container, form, searchInput, submitInput } = createHTMLFixture();
const search = new Search({ container });
spyOn(search, 'validate');
search.init();
input(searchInput, 'peace');
click(submitInput);
expect(search.validate).toHaveBeenCalledWith('peace');
});
// ...
});
});
The API documentation of the testing framework/library should be your bedside book!
Having a good knowledge of the API can help you in reducing the size/complexity of your test code and, in general, help you during development. A simple example:
:(
it('should call a method with the proper arguments', () => {
const foo = {
bar: jasmine.createSpy(),
baz: jasmine.createSpy()
};
foo.bar('qux');
expect(foo.bar).toHaveBeenCalled();
expect(foo.bar.calls.argsFor(0)).toEqual(['qux']);
});
/*it('should do more but not now', () => {
});
it('should do much more but not now', () => {
});*/
:)
fit('should call once a method with the proper arguments', () => {
const foo = jasmine.createSpyObj('foo', ['bar', 'baz']);
foo.bar('baz');
expect(foo.bar).toHaveBeenCalledWith('baz');
});
it('should do something else but not now', () => {
});
it('should do something else but not now', () => {
});
The handy fit
function used in the example above allows you to execute only one test without having to comment out all the tests below. fdescribe
does the same for test suites. This could help save a lot of time when developing.
More information on the Jasmine website.
If a method has several end results, each one should be tested separately. Whenever a bug occurs, it will help you locate the source of the problem.
:(
it('should send the profile data to the server and update the profile view properly', () => {
// expect(...)to(...);
// expect(...)to(...);
});
:)
it('should send the profile data to the server', () => {
// expect(...)to(...);
});
it('should update the profile view properly', () => {
// expect(...)to(...);
});
Beware that writing "AND" or "OR" when naming your test smells bad...
"Strange behaviour" usually happens at the edges... Remember that your tests can be the live documentation of your code.
:(
it('should properly calculate a RPN expression', () => {
const result = RPN('5 1 2 + 4 * - 10 /');
expect(result).toBe(-0.7);
});
:)
describe('The RPN expression evaluator', () => {
it('should return null when the expression is an empty string', () => {
const result = RPN('');
expect(result).toBeNull();
});
it('should return the same value when the expression holds a single value', () => {
const result = RPN('42');
expect(result).toBe(42);
});
it('should properly calculate an expression', () => {
const result = RPN('5 1 2 + 4 * - 10 /');
expect(result).toBe(-0.7);
});
it('should throw an error whenever an invalid expression is passed', () => {
const compute = () => RPN('1 + - 1');
expect(compute).toThrow();
});
});
:(
it('should suppress all chars that appear multiple times', () => {
expect(keepUniqueChars('Hello Fostonic !!')).toBe('HeFstnic');
});
:)
it('should return an empty string when passed an empty string', () => {
expect(keepUniqueChars('')).toBe('');
});
From there, start building the functionalities incrementally.
Build your tests suite from the simple case to the more complex ones. Keep in mind the incremental design. Deliver software fast, incrementally, and in short iterations.
:(
it('should return null when the expression is an empty string', () => {
const result = RPN('');
expect(result).toBeNull();
});
it('should properly calculate a RPN expression', () => {
const result = RPN('5 1 2 + 4 * - 10 /');
expect(result).toBe(-0.7);
});
:)
describe('The RPN expression evaluator', () => {
it('should return null when the expression is an empty string', () => {
const result = RPN('');
expect(result).toBeNull();
});
it('should return the same value when the expression holds a single value', () => {
const result = RPN('42');
expect(result).toBe(42);
});
describe('Additions-only expressions', () => {
it('should properly calculate a simple addition', () => {
const result = RPN('41 1 +');
expect(result).toBe(42);
});
it('should properly calculate a complex addition', () => {
const result = RPN('2 9 + 15 3 + + 7 6 + +');
expect(result).toBe(42);
});
});
// ...
describe('Complex expressions', () => {
it('should properly calculate an expression containing all 4 operators', () => {
const result = RPN('5 1 2 + 4 * - 10 /');
expect(result).toBe(-0.7);
});
});
});
:(
it('should add a user in memory', () => {
userManager.addUser('Dr. Falker', 'Joshua');
expect(userManager._users[0].name).toBe('Dr. Falker');
expect(userManager._users[0].password).toBe('Joshua');
});
A better approach is to test at the same level of the API:
:)
it('should add a user in memory', () => {
userManager.addUser('Dr. Falker', 'Joshua');
expect(userManager.loginUser('Dr. Falker', 'Joshua')).toBe(true);
});
Pro:
Con:
Here, a balance has to be found, unit-testing some key parts can be beneficial.
:(
describe('when the user has already visited the page', () => {
// storage.getItem('page-visited', '1') === '1'
describe('when the survey is not disabled', () => {
// storage.getItem('survey-disabled') === null
it('should display the survey', () => {
const storage = jasmine.createSpyObj('storage', ['setItem', 'getItem']);
storage.getItem.and.returnValue('1'); // ouch.
const surveyManager = new SurveyManager(storage);
spyOn(surveyManager, 'display');
surveyManager.start();
expect(surveyManager.display).toHaveBeenCalled();
});
});
// ...
});
This test fails, because the survey is considered disabled. Let's fix this:
:)
describe('when the user has already visited the page', () => {
// storage.getItem('page-visited', '1') === '1'
describe('when the survey is not disabled', () => {
// storage.getItem('survey-disabled') === null
it('should display the survey', () => {
const storage = jasmine.createSpyObj('storage', ['setItem', 'getItem']);
storage.getItem.and.callFake(key => {
switch (key) {
case 'page-visited':
return '1';
case 'survey-disabled':
return null;
}
return null;
}); // ouch.
const surveyManager = new SurveyManager(storage);
spyOn(surveyManager, 'display');
surveyManager.start();
expect(surveyManager.display).toHaveBeenCalled();
});
});
// ...
});
This will work... but needs a lot of code. Let's try a simpler approach:
:(
describe('when the user has already visited the page', () => {
// storage.getItem('page-visited', '1') === '1'
describe('when the survey is not disabled', () => {
// storage.getItem('survey-disabled') === null
it('should display the survey', () => {
const storage = window.localStorage; // ouch.
storage.setItem('page-visited', '1');
const surveyManager = new SurveyManager();
spyOn(surveyManager, 'display');
surveyManager.start();
expect(surveyManager.display).toHaveBeenCalled();
});
});
// ...
});
We created a permanent storage of data. What happens if we do not properly clean it? We might affect the other tests. Let's fix this:
:) :)
describe('when the user has already visited the page', () => {
// storage.getItem('page-visited', '1') === '1'
describe('when the survey is not disabled', () => {
// storage.getItem('survey-disabled') === null
it('should display the survey', () => {
const storage = new MemoryStorage(); // see https://github.com/tatsuyaoiw/webstorage
storage.setItem('page-visited', '1');
const surveyManager = new SurveyManager(storage);
spyOn(surveyManager, 'display');
surveyManager.start();
expect(surveyManager.display).toHaveBeenCalled();
});
});
});
The MemoryStorage
used here does not persist data. Nice and easy, with no side effects.
The idea to keep in mind is that dependencies can still be "real" objects. Don't mock everything because you can. In particular, consider using the "real" version of the objects if:
Whenever a bug is found, create a test that replicates the problem before touching any code. From there, you can apply TDD as usual to fix it.
Examples of complex user interactions:
These interactions might involve many units of work and should be handled at a higher level by functional tests. They will take more time to execute. They could be flaky (false negatives) and they need debugging whenever a failure is reported.
For functional testing, consider using a test automation framework (Selenium, ...) or QA manual testing.
Example of simple user actions:
These actions can be easily tested by simulating DOM events, for example:
describe('clicking on the "Preview profile" link', () => {
it('should show the profile preview if it is hidden', () => {
const previewLink = document.createElement('a');
const profileModule = createProfileModule({ previewLink, previewIsVisible: false });
spyOn(profileModule, 'showPreview');
click(previewLink);
expect(profileModule.showPreview).toHaveBeenCalled();
});
it('should hide the profile preview if it is displayed', () => {
const previewLink = document.createElement('a');
const profileModule = createProfileModule({ previewLink, previewIsVisible: true });
spyOn(profileModule, 'hidePreview');
click(previewLink);
expect(profileModule.hidePreview).toHaveBeenCalled();
});
});
Note how simple the test is because the UI (DOM) layer does not mix with the business logic layer:
The next step could be to test the business logic implemented in "showPreview()" or "hidePreview()".