@julr/japa-database-plugin
v1.1.0-beta.4
Published
Database assertions and testing helpers for Japa
Downloads
14
Maintainers
Readme
@julr/japa-database-plugin
This plugin for Japa provides some utility functions to make it easier for you to test a database. Built on top of knex.
Features
- Support Mysql, Sqlite, PostgreSQL, MSSQL
- Support expect, and assert
Installation
pnpm install @julr/japa-database-plugin
Configuration
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
Usage
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.
Assertions
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 [email protected] 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)
})
License
MIT License © 2022 Julien Ripouteau