Skip to main content
version 1.0.0

Testing: Getting Started

Introduction

Athenna is built with testing in mind. In fact, support for testing with Japa is included out of the box and a main.js file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.

By default, your application's tests directory contains two directories: E2E and Unit. Unit tests are tests that focus on a very small, isolated portion of your code. In fact, most unit tests probably focus on a single method. Tests within your "Unit" test directory do not boot your Athenna application and therefore are unable to access your application's database or other framework services.

E2E tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint. Generally, most of your tests should be feature tests. These types of tests provide the most confidence that your system as a whole is functioning as intended.

When you scaffold a new Athenna project it will come with two tests implemented. One is inside E2E folder, the WelcomeTest.js file. And the other is the WelcomeServiceTest.js file that is inside Unit folder. Use the npm test command to run your tests.

Environment

When running tests, Athenna will automatically set the configuration environment (NODE_ENV) to testing. You are free to define other testing environment configuration values as necessary. The testing environment variables may be configured in your application's main.js file, or you can simply create a .env.testing file in the root of your application.

Creating tests

To create a new test case, use the make:test Artisan command. By default, tests will be placed in the tests/E2E directory:

node artisan make:test UserTest

If you would like to create a test within the tests/Unit directory, you may use the --unit option when executing the make:test command:

node artisan make:test UserTest --unit

The Athenna tests are defined as classes by default, if you want to use the normal definition of Japa you can set the --no-class flag:

node artisan make:test UserFnTest --no-class
info

In Athenna you can define tests as classes and as functions in the normal way of Japa, all tests will run perfectly!

Once the test has been generated, you may define your tests as you normally would using Japa. To run your tests, execute the npm start or node artisan test command from your terminal:

import { Test } from '@athenna/test'

export class ExampleTest extends Test {
/**
* Before all event. This method is executed
* before all tests.
*
* @return {void | Promise<void>}
*/
async beforeAll() {}

/**
* Before each event. This method is executed
* before each test.
*
* @return {void | Promise<void>}
*/
async beforeEach() {}

/**
* After all event. This method is executed
* after all tests.
*
* @return {void | Promise<void>}
*/
async afterAll() {}

/**
* After each event. This method is executed
* after each test.
*
* @return {void | Promise<void>}
*/
async afterEach() {}

/**
* Set the test timeout for all tests inside the group.
*
* @example
* Default is 2000
*
* @return {number}
*/
get timeout() {
return 2000
}

/**
* Set the test names that can run.
*
* @example
* Default is ['*']
*
* @return {string[]}
*/
get runOnly() {
return ['shouldBeAbleToRunTests']
}

/**
* Run your test.
*
* @param {import('@athenna/test').HttpTestContext} ctx
*/
async shouldBeAbleToRunTests({ assert }) {
assert.equal(2 + 2, 4)
}
}
caution

Any method/attribute defined in your test class will be considered a test. To get around this you just need to define your method/attribute with an _ in front of it. Example: _getMyProductId()

Running tests

In addition to the npm test command, you may use the test Artisan command to run your tests. The Artisan test runner provides verbose test reports in order to ease development and debugging:

node artisan test

Any arguments that can be passed to the Japa command may also be passed to the Artisan test command:

node artisan test Unit

But you can use the artisan options instead:

node artisan test --e2e --unit

Reporting tests coverage

caution

This feature requires the c8 library.

When running your application tests, you may want to determine whether your test cases are actually covering the application code and how much application code is used when running your tests. To accomplish this, you may provide the c8 cli when invoking the test command:

c8 node artisan test

To define a minimum test coverage threshold for your application you can use the "c8" property inside package.json file. The test suite will fail if this threshold is not met. By default, Athenna comes with 70% of coverage in all type of coverages:

"c8": {
"all": true,
"include": [
"app/**/*.js"
],
"reporter": [
"text-summary",
"html"
],
"report-dir": "./tests/Coverage",
"check-coverage": true,
"statements": "70", # <-
"branches": "70", # <-
"functions": "70", # <-
"lines": "70" # <-
},