Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/julien-r44/japa-database-plugin

💽 Database assertions and testing helpers for Japa
https://github.com/julien-r44/japa-database-plugin

database japa testing

Last synced: about 1 month ago
JSON representation

💽 Database assertions and testing helpers for Japa

Awesome Lists containing this project

README

        



# @julr/japa-database-plugin

This plugin for [Japa](http://japa.dev) provides some utility functions to make it easier for you to test a database. Built on top of [knex](https://knexjs.org/).

## Features
- Support Mysql, Sqlite, PostgreSQL, MSSQL
- Support expect, and assert

## Installation
```bash
pnpm install @julr/japa-database-plugin
```

## Configuration
The first step is to register the plugin in your Japa configuration :

```ts
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](https://knexjs.org/guide/#configuration-options)

## Usage

The plugin provides the DatabaseUtils class which you can use for refreshing the database between your tests :

```ts
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 :

```ts
test.group('My tests', group => {
test('My test', async ({ database }) => {
await database.assertHas('users', { email: '[email protected]' })
})
})
```

The plugin proposes the following assertions:

```ts
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.md) License © 2022 [Julien Ripouteau](https://github.com/Julien-R44)