Skip to content
tezvyn:
Menu

Writing a basic Jest unit test

Source: interviewEasyHow cards are made

Summary

Jest fundamentals.

Key points

import the function, group cases with describe, define each case with it or test, assert with expect and a matcher like toBe, covering normal and edge inputs.

What's really being asked

Whether you can structure a minimal, correct Jest test and choose the right matcher, including edge cases.

The full answer

First you import the function under test from utils.js. describe groups related test cases under a label, usually the function name, which keeps output readable. Each individual case uses it (or its alias test), which takes a descriptive name and a callback containing the actual test. Inside the callback you call the function with chosen inputs and assert the result using expect with a matcher: toBe for primitive equality, toEqual for deep object or array equality, and others like toThrow for error cases. A good unit test covers the normal case plus edge cases such as empty input or unusual characters, and each test should be independent so order does not matter. You run them with the jest command.

The mistakes people make

Forgetting to import the function, writing a test body with no expect call so nothing is actually asserted, using toBe for object comparison (which fails on reference inequality), or only testing the happy path.

What usually comes next

toBe versus toEqual, testing thrown errors, beforeEach setup, and why tests should be independent.

A concrete example

import { formatTitle } from './utils'; describe('formatTitle', () => { it('capitalizes the first letter', () => { expect(formatTitle('hello')).toBe('Hello'); }); it('handles an empty string', () => { expect(formatTitle('')).toBe(''); }); }); The describe block names the unit, each it states an expectation, and expect with toBe asserts the exact return value for both a normal and an edge input.

Interview question

Your Jest test compares the function's returned object using expect(result).toBe(expectedObject) and it fails despite identical contents. Why?

  • a.Jest cannot compare objects at all
  • b.toBe checks reference identity, so use toEqual for deep object comparisonCorrect
  • c.The test needs an await before expect
  • d.toBe is not a valid Jest matcher
Why?

toBe uses Object.is reference equality, so two distinct objects with equal contents are not the same reference and the test fails. toEqual performs a deep structural comparison and is the correct matcher for objects.

Just read this? Test yourself on what you have been reading.

Read the original → jestjs.io

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles