Skip to content

Add Arrange-Act-Assert comments inside tests #96

@MatanYadaev

Description

@MatanYadaev

The AAA (Arrange, Act Assert) pattern's purpose is to organize your test in a way that is easier to read and understand. You can read about it here: https://github.com/goldbergyoni/javascript-testing-best-practices#-%EF%B8%8F-12-structure-tests-by-the-aaa-pattern

In this repo, all of the tests are following this pattern. But it's not perfect, because no comments are separating the test into these 3 sections.

This task is about adding these comments.

Example:

// ❌ Before
it('calculates sum', function (): void {
  $firstNumber = 1;
  $secondNumber = 2;

  $result = sum($firstNumber, $secondNumber);

  expect($result)->toBe(3);
});

// ✅ After
it('calculates sum', function (): void {
  // Arrange
  $firstNumber = 1;
  $secondNumber = 2;

  // Act
  $result = sum($firstNumber, $secondNumber);

  // Assert
  expect($result)->toBe(3);
});

Real example:

it('calculates geometry centroid', function (): void {
// Arrange
$polygon = Polygon::fromJson('{"type":"Polygon","coordinates":[[[-1,-1],[1,-1],[1,1],[-1,1],[-1,-1]]]}');
TestPlace::factory()->create(['polygon' => $polygon]);
// Act
/** @var TestPlace $testPlace */
$testPlace = TestPlace::query()
->withCentroid('polygon')
->withCasts(['centroid' => Point::class])
->firstOrFail();
// Assert
$expectedCentroid = new Point(0, 0);
expect($testPlace->centroid)->toEqual($expectedCentroid);
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions