This plugin for Japa provides some utility functions to make it easier for you to test a database. Built on top of knex.
- Support Mysql, Sqlite, PostgreSQL, MSSQL
- Support expect, and assert
pnpm install @julr/japa-database-plugin
The first step is to register the plugin in your Japa configuration :
import { database } from '@julr/japa-database-plugin'
configure({
...processCliArgs(process.argv.slice(2)),
...{
plugins: [
expect(),
database({
database: {
client: 'pg',
connection: {
host: 'localhost',
user: 'japa',
password: 'password',
database: 'japa',
}
}
}),
],
// ...
},
})
You can find more information about the configuration of your database in the knex documentation
The plugin provides the DatabaseUtils class which you can use for refreshing the database between your tests :
import { DatabaseUtils } from '@julr/japa-database-plugin'
test.group('My tests', group => {
group.each.teardown(async () => DatabaseUtils.refreshDatabase())
// ...
})
This will truncate all tables in your database.
You can access the main object from the test context as follows :
test.group('My tests', group => {
test('My test', async ({ database }) => {
await database.assertHas('users', { email: '[email protected]' })
})
})
The plugin proposes the following assertions:
test('My test', async ({ database }) => {
// Assert that an user with jul@japa.com in DB
await database.assertHas('users', { email: '[email protected]' })
// Assert that we have 5 users with [email protected] in DB
await database.assertHas('users', { email: '[email protected]' }, 5)
// Assert that the db does not have the given row
await database.assertMissing('users', { email: '[email protected]'})
// Assert that the db 5 users rows
await database.assertHasCount('users', 5)
})
MIT License © 2022 Julien Ripouteau