Jest is an open-source JavaScript testing framework: write tests, make assertions with expect, run them, and receive a report from one tool. That all-in-one role avoids assembling separate test, assertion, runner, and reporter packages before learning testing patterns. section 2.1 section 2.2
A test can use either terse test(...) syntax or nested describe(...)/it(...) syntax. Use the terse form when the target and context are obvious; use describe groups when one scenario has several expected results. The nested form can improve expression, but it is not automatically better. section 2.5.8 section 2.12
A practical Jest workflow is:
- Initialize a JavaScript project with npm or Yarn; Jest reads configuration from package.json or jest.config.js, though defaults are enough to begin. section 2.1.2
- Run Jest from the command line. Its report names failures, shows the failed expectation and expected-versus-actual difference, points to the test line, and totals passed and failed tests. section 2.1.5
- Keep Jest running with jest --watch to rerun changed tests without repeatedly initializing the tool. section 2.1.5
- Structure individual tests with Arrange-Act-Assert, and name them around the unit, scenario, and expectation. section 2.12
Reduce repetition carefully. beforeEach() can share setup, but hidden setup creates scroll fatigue because readers must search for a test’s real context. Factory methods with plain tests can stay clearer. Parameterized tests reduce similar code, yet become less readable when generalized too far; parameterize inputs, not different output behaviors. section 2.12 section 2.12
Faking direct module dependencies
When a Password Verifier imports a configuration service and logger directly, Jest can fake both modules with jest.mock(...). Configure the configuration fake to return an INFO level, run verification, then assert that the logger’s info function received a passing message. This lets a test simulate an incoming value and verify an outgoing call without changing the production module imports. section 5.2
Listing 5.2 Faking the module APIs directly with jest.mock()
jest.mock("./complicated-logger"); #1
jest.mock("./configuration-service"); #1
const { stringMatching } = expect;
const { verifyPassword } = require("./password-verifier");
const mockLoggerModule = require("./complicated-logger"); #2
const stubConfigModule = require("./configuration-service"); #2
describe("password verifier", () => {
afterEach(jest.resetAllMocks); #3
test('with info log level and no rules,
it calls the logger with PASSED', () => {
stubConfigModule.getLogLevel.mockReturnValue("info"); #4
verifyPassword("anything", []);
expect(mockLoggerModule.info) #5
.toHaveBeenCalledWith(stringMatching(/PASS/)); #5
});
test('with debug log level and no rules,
it calls the logger with PASSED', () => {
stubConfigModule.getLogLevel.mockReturnValue("debug"); #6
verifyPassword("anything", []);
expect(mockLoggerModule.debug) #7
.toHaveBeenCalledWith(stringMatching(/PASS/)); #7
});
});
#1 Faking the modules
#2 Getting the fake instances of the modules
#3 Telling Jest to reset any fake module behavior between tests
#4 Configuring the stub to return a fake “info” value.
#5 Asserting that the mock was called correctly
#6 Changing the stub config
#7 Asserting on the mock logger as done previously
Inspect a compact test that resets mocks, stubs a configuration return value, and verifies a logger call. section 5.2
Jest is not xUnit- or TAP-compliant by default. For build tooling that expects those formats, configure a reporter such as jest-xunit or jest-tap-reporter through jest.config.js. Jest also supports mocks, stubs, and spies for isolation. section 2.3.2 section 2.2




