Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/julien-r44/japa-database-plugin
- Owner: Julien-R44
- License: mit
- Created: 2022-09-17T22:44:54.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-06-12T17:59:35.000Z (over 1 year ago)
- Last Synced: 2024-10-05T11:46:28.987Z (3 months ago)
- Topics: database, japa, testing
- Language: TypeScript
- Homepage:
- Size: 413 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
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)