diff --git a/content/guides/testing/introduction.md b/content/guides/testing/introduction.md index e553eb4..7bd7d52 100644 --- a/content/guides/testing/introduction.md +++ b/content/guides/testing/introduction.md @@ -203,6 +203,8 @@ Make sure you have `@adonisjs/lucid` installed for the following examples to wor ### Migrating database +#### Reset database after each run cycle + You can migrate the database before running all the tests and roll it back after the tests. This can be done by registering the `TestUtils.db().migrate()` hook within the `tests/bootstrap.ts` file. ```ts @@ -218,6 +220,32 @@ export const runnerHooks: Required> = { } ``` +#### Truncate database after each run cycle + +An alternative to the above approach is to truncate all tables in the database after each run cycle instead of rolling it back. This can be done by registering the `TestUtils.db().truncate()` hook within the `tests/bootstrap.ts` file. + +```ts +// title: tests/bootstrap.ts +export const runnerHooks: Required> = { + setup: [ + () => TestUtils.ace().loadCommands(), + // highlight-start + () => TestUtils.db().truncate() + // highlight-end + ], + teardown: [], +} +``` + +Before running your tests, the hook will migrate the database if necessary. After the tests are run, all the tables in your database will be kept but truncated. + +So next time you run your tests, your database will be empty but will not need to be migrated again. This may be a better approach and will save you some time if you have a lot of migrations. + +:::tip +Note that the hook internally calls the `node ace db:truncate` command that you can also run manually. Also, note that this command will truncate all tables **except** `adonis_schema` and `adonis_schema_versions` tables. +::: + + ### Seeding database You can also run database seeders by calling the `TestUtils.db().seed()` method.