Skip to content

Testing

Accella projects come with Vitest and its configuration included from the start. This allows you to begin testing immediately.

Running Tests

To run tests, use the following command:

Terminal window
npm run test

This command will execute all tests within the project.

A sample test is included in tests/sample.test.ts. Use this file as a reference to add your own tests.

Configuring the Test Database

You can change the test database configuration by specifying the value of DATABASE_URL in .env.test.

.env.test
DATABASE_URL="file:../test${VITEST_POOL_ID}.db"

Vitest typically runs tests in multiple threads. To use different databases for each thread, use VITEST_POOL_ID to separate the databases.

If you are using a database other than SQLite (such as MySQL or PostgreSQL), you will need to change the test database configuration in .env.test just as you did in .env for the main Database Configuration.

Example for MySQL

.env.test
DATABASE_URL="mysql://root:@localhost:3306/test${VITEST_POOL_ID}"

Example for PostgreSQL

.env.test
DATABASE_URL="postgresql://test:password@localhost:5432/test${VITEST_POOL_ID}"

During test execution, migrations will be run for each database. Additionally, database operations will be rolled back after each test, ensuring no data is shared between tests. These processes are invoked in tests/setup.ts. Refer to this file as needed.

Model Factory

We provide the Accel Record Factory library for initializing models and saving them to the database. When you define a schema and add a model, a corresponding factory is automatically generated in the tests/factories/ directory.

For example, if you define a User model, tests/factories/user.ts containing the UserFactory definition will be generated.

tests/factories/user.ts
import { defineFactory } from "accel-record-factory";
import { User } from "../../src/models/index.js";
export const UserFactory = defineFactory(User, {
// firstName: "MyString",
// lastName: "MyString",
// age: 1,
});
export { UserFactory as $User };

Using the factory, you can easily create models in your test code as follows:

tests/user.test.ts
import { $User } from "./factories/user";
const newUser = $User.build();
const user = $User.create({
firstName: "John",
lastName: "Doe",
age: 20,
});

You can set default values, use sequences, generate relationships, and utilize traits. For more details, refer to the Accel Record Factory README.

Testing Astro Components

Astro supports component testing with Vitest. For more details, refer to the Vitest and Container API.

End-to-end Testing

End-to-end (E2E) testing is not set up by default but can be added to your project. For instructions, refer to the End-to-end tests section of the Astro documentation. It includes information on integrating Playwright, Cypress, NightwatchJS, and more.