1.1.0-beta.4 • Published 11 months ago

@julr/japa-database-plugin v1.1.0-beta.4

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

@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: 'jul@japa.com' })
  })
})

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: 'jul@japa.com' })

  // Assert that we have 5 users with bonjour@ok.com in DB
  await database.assertHas('users', { email: 'bonjour@ok.com' }, 5)

  // Assert that the db does not have the given row
  await database.assertMissing('users', { email: 'shouldNotBe@inDatabase.com'})

  // Assert that the db 5 users rows
  await database.assertHasCount('users', 5)
})

License

MIT License © 2022 Julien Ripouteau

1.1.0-beta.4

11 months ago

1.1.0-beta.3

11 months ago

1.0.5

1 year ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago