Last active
May 28, 2019 22:30
-
-
Save andreybleme/2f3c50b0ded2ab1ee9920bfcbe861381 to your computer and use it in GitHub Desktop.
andreybleme.com | Integration tests with Docker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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