Skip to content

Instantly share code, notes, and snippets.

@andreybleme
Last active May 28, 2019 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreybleme/2f3c50b0ded2ab1ee9920bfcbe861381 to your computer and use it in GitHub Desktop.
Save andreybleme/2f3c50b0ded2ab1ee9920bfcbe861381 to your computer and use it in GitHub Desktop.
andreybleme.com | Integration tests with Docker
const knex = require('knex')({
client: 'pg',
connection: process.env.PG_URL,
});
let pgContainer = await new GenericContainer('postgres')
.withEnv('POSTGRES_USER', 'test')
.withEnv('POSTGRES_PASSWORD', 'test')
.withExposedPorts(5432)
describe('User tests', () => {
beforeAll(async () => {
/* init container */
pgContainer = await pgContainer.start();
/* change environment database URL to point to container URL */
process.env.PG_URL = `postgres://test:test@localhost:${pgContainer.getMappedPort(5432)}/test`;
/* create database schema in container */
await knex.migrate.latest();
});
afterAll(async () => {
/* delete schema and data from container */
await knex.destroy();
/* stop Postgres contrainer */
await pgContainer.stop();
});
test('User must be created', () => {
/* test with matchers */
...
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment