liveBook · Manning

Arrange act assert pattern

As explained in The Art of Unit Testing 3e

The Arrange-Act-Assert pattern structures a test into setup of test conditions, execution of the behavior under test, and verification of the outcome.

When a test mixes setup, behavior, and verification, it becomes harder to read. The Arrange-Act-Assert (AAA) pattern gives every test a simple, uniform layout: prepare the conditions, run the behavior, and check the result. section 2.12 section 2.5.1

Separate the test into three deliberate phases:

  • Arrange: create the system under test and its dependencies, such as a fake rule that returns a known result. section 2.12 section 2.5.1
  • Act: call the method under test with the prepared input and capture its output. section 2.12
  • Assert: verify that the output matches the expected behavior, such as an error containing the fake rule’s reason. section 2.12 section 2.5.1

A small AAA test

test('reports the rule reason', () => { // Arrange const fakeRule = input => ({ passed: false, reason: 'fake reason' }); const verifier = new PasswordVerifier(); verifier.addRule(fakeRule); // Act const errors = verifier.verify('any value'); // Assert expect(errors[0]).toContain('fake reason'); }); section 2.5.1

AAA does not forbid reuse, but hiding Arrange and Act inside beforeEach() can create scroll fatigue and obscure each test’s actual setup. Keep phases visible when that improves understanding; use factory methods or limited reuse rather than placing unrelated state in shared setup. section 2.12 section 2.6.1

Listing 2.14 Pushing the arrange and act parts into beforeEach()
describe('PasswordVerifier', () => {
  let verifier;
  beforeEach(() => verifier = new PasswordVerifier1());
  describe('with a failing rule', () => {
    let fakeRule, errors;
    beforeEach(() => {
      fakeRule = input => ({passed: false, reason: 'fake reason'});
      verifier.addRule(fakeRule);
      errors = verifier.verify('any value');
    });
    it('has an error message based on the rule.reason', () => {
      expect(errors[0]).toContain('fake reason');
    });
    it('has exactly one error', () => {
      expect(errors.length).toBe(1);
    });
  });
});

Compare a compact shared setup with the resulting loss of local visibility when Arrange and Act move into beforeEach(). section 2.6.1

How The Art of Unit Testing 3e covers it

How deep it goes
Defines three phases and demonstrates them with a fake rule, but defers broader test organization to Jest features.
Examples
Uses PasswordVerifier, a fake rule, an error message, and Jest’s toContain assertion.
What you can do after
Apply it via Jest to write and read tests with explicit setup, behavior execution, and verification.
How it connects
Links unit tests to Jest, assertions, fake dependencies, and beforeEach().

Also explained in

Compare how 6 books explain arrange act assert pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Unit Testing 3e. The section links point to the parts of the book it draws on.