Search⌘ K
AI Features

Arrange Act Assert

Explore the Arrange Act Assert pattern in Jasmine to structure JavaScript unit tests more clearly. This lesson helps you separate test setup, execution, and verification, making tests easier to read and maintain.

Explanation of the last unit test

Let’s take a look at the unit test from the previous lesson:

it('should create canvas, append it in the element, get a 2d context and draw the square', () => {
  const contextMock = { rect: jasmine.createSpy('rect') };
  const canvasMock = { getContext: jasmine.createSpy('canvasMock') };
  canvasMock.getContext.and.returnValue(contextMock);
  const documentSpy = spyOn(document, 'createElement').and.returnValue(canvasMock);
  const element = { appendChild: jasmine.createSpy('appendChild') };

  drawSquare(element, 10);

  expect(documentSpy).toHaveBeenCalledTimes(1);
  expect(element.appendChild).toHaveBeenCalledOnceWith(canvasMock);
  expect(canvasMock.getContext).toHa
...