Pragmatic Testing in AdonisJS with Japa #3.0

Testing a Simple Service

In This Lesson

We'll focus on Unit Testing in AdonisJS using Japa. Learn how to write isolated tests for a service's business logic, including asserting correct results and handling expected exceptions.

Created by
@tomgobich
Published

Notes Used to Craft this Lesson

So far, we've used services to aid our tests by mocking real-world behaviors, but now we're going to pivot to actually testing the service.

Let's put our service-specific tests within a services folder so they're all grouped.

node ace make:test services/calculator_service --suite=unit
Copied!

This is a simple service that contains methods for addition, subtraction, multiplication, and division. When attempting to divide by 0, an exception is thrown. We'll want to test each of those scenarios, including division by 0.

Let's start by confirming that our CalculatorService should successfully add two numbers.

test.group("Services calculator service", () => {
  test("successfully add two numbers", async ({ assert }) => {
    const calculatorService = new CalculatorService();

    assert.strictEqual(calculatorService.add(2, 3), 5);
    assert.strictEqual(calculatorService.add(-1, 1), 0);
  });
});
Copied!

The type we get back here is pertinent to what the methods are doing. So, we'll want to perform a strict equality check so we know the types match as well.

Additionally, when it comes to our test name, here we're using our group to specify what is being tested, allowing us to omit that from the test name itself.

So, we know our add method works; let's add tests for the others.

test.group("Services calculator service", () => {
  // ...

  test("successfully subtract two numbers", async ({ assert }) => {
    const calculatorService = new CalculatorService();

    assert.strictEqual(calculatorService.subtract(5, 3), 2);
    assert.strictEqual(calculatorService.subtract(2, 4), -2);
  });

  test("successfully multiply two numbers", async ({ assert }) => {
    const calculatorService = new CalculatorService();

    assert.strictEqual(calculatorService.multiply(4, 3), 12);
    assert.strictEqual(calculatorService.multiply(-2, 3), -6);
  });

  test("successfully divide two numbers", async ({ assert }) => {
    const calculatorService = new CalculatorService();

    assert.strictEqual(calculatorService.divide(10, 2), 5);
    assert.strictEqual(calculatorService.divide(100, 5), 20);
  });
});
Copied!

Finally, we know that division by zero isn't allowed, and our divide method should throw an exception in that case.

test.group("Services calculator service", () => {
  // ...

  test("throw exception when dividing by zero", async ({ assert }) => {
    const calculatorService = new CalculatorService();
	const division = () => calculatorService.divide(7, 0);

	assert.throws(division, 'Division by zero is not allowed');
  });
});
Copied!

Join the Discussion 0 comments

Create a free account to join in on the discussion
robot comment bubble

Be the first to comment!