Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
ava-postgres
Advanced tools
`ava-postgres` is a test fixture for [AVA](https://github.com/avajs/ava) that provides you with nearly-instant access to a fresh Postgres database for every test.
ava-postgres
is a test fixture for AVA that provides you with nearly-instant access to a fresh Postgres database for every test.
npm install --save-dev ava-postgres
or
yarn add --dev ava-postgres
ava-postgres
's main export is a factory function, so you'll probably want to create a file like tests/fixtures/get-test-database.ts
:
import { getTestPostgresDatabaseFactory } from "ava-postgres"
export const getTestDatabase = getTestPostgresDatabaseFactory({
// Any tag for the official Postgres Docker image, defaults to "14"
postgresVersion: "14",
})
Then, in your tests, you can use the getTestDatabase()
function to get a fresh database for each test:
import test from "ava"
import { getTestDatabase } from "./fixtures/get-test-database"
test("foo bar", async (t) => {
const { pool } = await getTestDatabase()
await pool.query("SELECT 1")
t.pass()
})
Full list of connection details returned by getTestDatabase
ava-postgres
uses Postgres templates so you only pay the setup cost once. After a template has been created, Postgres can create a new database from it in milliseconds.
If you want to perform common database setup, you can use a hook and pass parameters to the getTestDatabase()
function:
import { getTestPostgresDatabaseFactory } from "ava-postgres"
type GetTestDatabaseParams = {
shouldMigrate?: boolean
shouldSeed?: boolean
}
export const getTestDatabase =
getTestPostgresDatabaseFactory<GetTestDatabaseParams>({
beforeTemplateIsBaked: async ({
connection: { pool },
params: { shouldMigrate, shouldSeed },
}) => {
if (shouldMigrate) {
await pool.query("CREATE TABLE foo (id int)")
}
if (shouldSeed) {
await pool.query("INSERT INTO foo VALUES (1)")
}
},
})
Then, in your tests, you can pass parameters to the getTestDatabase()
function:
import test from "ava"
import { getTestDatabase } from "./fixtures/get-test-database"
test("foo bar", async (t) => {
const { pool } = await getTestDatabase({
shouldMigrate: true,
shouldSeed: true,
})
await pool.query("SELECT * FROM foo")
t.pass()
})
exec
ing in the containerava-postgres
uses testcontainers under the hood to manage the Postgres container.
In some scenarios you might want to mount a SQL script into the container and manually load it using psql
.
You can do this with the bindMounts
option:
const getTestPostgresDatabase = getTestPostgresDatabaseFactory({
container: {
bindMounts: [
{
source: "/path/on/host",
target: "/test.sql",
},
],
},
beforeTemplateIsBaked: async ({
connection: { username, database },
containerExec,
}) => {
const { exitCode } = await containerExec(
`psql -U ${username} -d ${database} -f /test.sql`.split(" ")
)
if (exitCode !== 0) {
throw new Error(`Failed to load test file`)
}
},
})
FAQs
`ava-postgres` is a test fixture for [AVA](https://github.com/avajs/ava) that provides you with nearly-instant access to a fresh Postgres database for every test.
The npm package ava-postgres receives a total of 118 weekly downloads. As such, ava-postgres popularity was classified as not popular.
We found that ava-postgres demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.