Skip to content

Commit

Permalink
fix(test): fix test flakiness
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmer committed Oct 9, 2016
1 parent d723028 commit d8bd1bc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
40 changes: 18 additions & 22 deletions src/postgres/__tests__/fixtures/getTestPGClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,22 @@ const minify = require('pg-minify')
const pool = new Pool({
database: 'postgraphql_test',
port: 5432,
max: 10,
max: 20,
idleTimeoutMillis: 500,
})

const kitchenSinkSchema = new Promise((resolve, reject) => {
readFile(resolvePath(__dirname, '../../../../resources/kitchen-sink-schema.sql'), (error, data) => {
if (error) reject(error)
else resolve(minify(data.toString()))
let clients = []

beforeAll(async () => {
const kitchenSinkSchema = await new Promise((resolve, reject) => {
readFile(resolvePath(__dirname, '../../../../resources/kitchen-sink-schema.sql'), (error, data) => {
if (error) reject(error)
else resolve(minify(data.toString()))
})
})
})

let clients = []
await pool.query(kitchenSinkSchema)
})

/**
* Acquires a client that connects to our test database from a connection pool.
Expand All @@ -37,28 +41,16 @@ let clients = []
*
* @returns {Promise<Client>}
*/
export default async function getTestPGClient ({ noKitchenSinkSchema } = {}) {
export default async function getTestPGClient () {
const client = await pool.connect()
clients.push(client)
await client.query('begin')

// If the user does not opt out of the kitchen sink schema, let鈥檚 apply it.
if (!noKitchenSinkSchema) {
try {
await client.query(await kitchenSinkSchema)
}
catch (error) {
// Make sure we log any errors we might run into. If we don鈥檛 do this
// log, the thrown error is pretty cryptic.
console.error('Failed to execute kitchen sink SQL:', error.stack)
throw error
}
}

// Wrap the query function in a Jest mock so that users in tests can inspect
// what鈥檚 happening.
client.query = jest.fn(client.query)

clients.push(client)

return client
}

Expand All @@ -69,3 +61,7 @@ afterEach(async () => {
}))
clients = []
})

afterAll(async () => {
await pool.end()
})
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ test('create will insert new rows into the database', async () => {
const pgQueryResult = await client.query('select row_to_json(p) as object from c.person as p')

expect(pgQueryResult.rows.map(({ object }) => object))
.toEqual(values.map(mapToObject))
.toEqual(values.map(mapToObject))
})

test('paginator will have the same name and type', () => {
Expand Down

0 comments on commit d8bd1bc

Please sign in to comment.