Skip to content

Commit b4e7750

Browse files
committed
Improve test cleanup
1 parent 90865cd commit b4e7750

File tree

2 files changed

+59
-64
lines changed

2 files changed

+59
-64
lines changed

tests/index.js

+58-58
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ t('Result has command', async() =>
7070
)
7171

7272
t('Create table', async() =>
73-
['CREATE TABLE', (await sql`create table test(int int)`).command]
74-
, () => sql`drop table test`)
73+
['CREATE TABLE', (await sql`create table test(int int)`).command, await sql`drop table test`]
74+
)
7575

7676
t('Drop table', async() => {
7777
await sql`create table test(int int)`
@@ -143,25 +143,25 @@ t('Escapes', async() => {
143143

144144
t('null for int', async() => {
145145
await sql`create table test (x int)`
146-
return [1, (await sql`insert into test values(${ null })`).count]
147-
}, () => sql`drop table test`)
146+
return [1, (await sql`insert into test values(${ null })`).count, await sql`drop table test`]
147+
})
148148

149149
t('Transaction throws', async() => {
150150
await sql`create table test (a int)`
151151
return ['22P02', await sql.begin(async sql => {
152152
await sql`insert into test values(1)`
153153
await sql`insert into test values('hej')`
154-
}).catch(x => x.code)]
155-
}, () => sql`drop table test`)
154+
}).catch(x => x.code), await sql`drop table test`]
155+
})
156156

157157
t('Transaction rolls back', async() => {
158158
await sql`create table test (a int)`
159159
await sql.begin(async sql => {
160160
await sql`insert into test values(1)`
161161
await sql`insert into test values('hej')`
162162
}).catch(() => { /* ignore */ })
163-
return [0, (await sql`select a from test`).count]
164-
}, () => sql`drop table test`)
163+
return [0, (await sql`select a from test`).count, await sql`drop table test`]
164+
})
165165

166166
t('Transaction throws on uncaught savepoint', async() => {
167167
await sql`create table test (a int)`
@@ -172,8 +172,8 @@ t('Transaction throws on uncaught savepoint', async() => {
172172
await sql`insert into test values(2)`
173173
throw new Error('fail')
174174
})
175-
}).catch(() => 'fail'))]
176-
}, () => sql`drop table test`)
175+
}).catch(() => 'fail')), await sql`drop table test`]
176+
})
177177

178178
t('Transaction throws on uncaught named savepoint', async() => {
179179
await sql`create table test (a int)`
@@ -184,8 +184,8 @@ t('Transaction throws on uncaught named savepoint', async() => {
184184
await sql`insert into test values(2)`
185185
throw new Error('fail')
186186
})
187-
}).catch(() => 'fail'))]
188-
}, () => sql`drop table test`)
187+
}).catch(() => 'fail')), await sql`drop table test`]
188+
})
189189

190190
t('Transaction succeeds on caught savepoint', async() => {
191191
await sql`create table test (a int)`
@@ -198,8 +198,8 @@ t('Transaction succeeds on caught savepoint', async() => {
198198
await sql`insert into test values(3)`
199199
})
200200

201-
return [typeof BigInt === 'undefined' ? '2' : 2n, (await sql`select count(1) from test`)[0].count]
202-
}, () => sql`drop table test`)
201+
return [typeof BigInt === 'undefined' ? '2' : 2n, (await sql`select count(1) from test`)[0].count, await sql`drop table test`]
202+
})
203203

204204
t('Savepoint returns Result', async() => {
205205
let result
@@ -217,17 +217,17 @@ t('Parallel transactions', async() => {
217217
return ['11', (await Promise.all([
218218
sql.begin(sql => sql`select 1`),
219219
sql.begin(sql => sql`select 1`)
220-
])).map(x => x.count).join('')]
221-
}, () => sql`drop table test`)
220+
])).map(x => x.count).join(''), await sql`drop table test`]
221+
})
222222

223223
t('Transactions array', async() => {
224224
await sql`create table test (a int)`
225225

226226
return ['11', (await sql.begin(sql => [
227227
sql`select 1`.then(x => x),
228228
sql`select 1`
229-
])).map(x => x.count).join('')]
230-
}, () => sql`drop table test`)
229+
])).map(x => x.count).join(''), await sql`drop table test`]
230+
})
231231

232232
t('Transaction waits', async() => {
233233
await sql`create table test (a int)`
@@ -243,8 +243,8 @@ t('Transaction waits', async() => {
243243
return ['11', (await Promise.all([
244244
sql.begin(sql => sql`select 1`),
245245
sql.begin(sql => sql`select 1`)
246-
])).map(x => x.count).join('')]
247-
}, () => sql`drop table test`)
246+
])).map(x => x.count).join(''), await sql`drop table test`]
247+
})
248248

249249
t('Helpers in Transaction', async() => {
250250
return ['1', (await sql.begin(async sql =>
@@ -344,8 +344,8 @@ t('Point type', async() => {
344344

345345
await sql`create table test (x point)`
346346
await sql`insert into test (x) values (${ sql.types.point([10, 20]) })`
347-
return [20, (await sql`select x from test`)[0].x[1]]
348-
}, () => sql`drop table test`)
347+
return [20, (await sql`select x from test`)[0].x[1], await sql`drop table test`]
348+
})
349349

350350
t('Point type array', async() => {
351351
const sql = postgres({
@@ -362,8 +362,8 @@ t('Point type array', async() => {
362362

363363
await sql`create table test (x point[])`
364364
await sql`insert into test (x) values (${ sql.array([sql.types.point([10, 20]), sql.types.point([20, 30])]) })`
365-
return [30, (await sql`select x from test`)[0].x[1][1]]
366-
}, () => sql`drop table test`)
365+
return [30, (await sql`select x from test`)[0].x[1][1], await sql`drop table test`]
366+
})
367367

368368
t('sql file', async() =>
369369
[1, (await sql.file(path.join(__dirname, 'select.sql')))[0].x]
@@ -444,8 +444,8 @@ t('Connection destroyed with query before', async() => {
444444

445445
t('Message not supported', async() => {
446446
await sql`create table test (x int)`
447-
return ['MESSAGE_NOT_SUPPORTED', await sql`copy test to stdout`.catch(x => x.code)]
448-
}, () => sql`drop table test`)
447+
return ['MESSAGE_NOT_SUPPORTED', await sql`copy test to stdout`.catch(x => x.code), await sql`drop table test`]
448+
})
449449

450450
t('transform column', async() => {
451451
const sql = postgres({
@@ -455,8 +455,8 @@ t('transform column', async() => {
455455

456456
await sql`create table test (hello_world int)`
457457
await sql`insert into test values (1)`
458-
return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0]]
459-
}, () => sql`drop table test`)
458+
return ['dlrow_olleh', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]
459+
})
460460

461461
t('column toPascal', async() => {
462462
const sql = postgres({
@@ -466,8 +466,8 @@ t('column toPascal', async() => {
466466

467467
await sql`create table test (hello_world int)`
468468
await sql`insert into test values (1)`
469-
return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0]]
470-
}, () => sql`drop table test`)
469+
return ['HelloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]
470+
})
471471

472472
t('column toCamel', async() => {
473473
const sql = postgres({
@@ -477,8 +477,8 @@ t('column toCamel', async() => {
477477

478478
await sql`create table test (hello_world int)`
479479
await sql`insert into test values (1)`
480-
return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0]]
481-
}, () => sql`drop table test`)
480+
return ['helloWorld', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]
481+
})
482482

483483
t('column toKebab', async() => {
484484
const sql = postgres({
@@ -488,13 +488,13 @@ t('column toKebab', async() => {
488488

489489
await sql`create table test (hello_world int)`
490490
await sql`insert into test values (1)`
491-
return ['hello-world', Object.keys((await sql`select * from test`)[0])[0]]
492-
}, () => sql`drop table test`)
491+
return ['hello-world', Object.keys((await sql`select * from test`)[0])[0], await sql`drop table test`]
492+
})
493493

494494
t('unsafe', async() => {
495495
await sql`create table test (x int)`
496-
return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x]
497-
}, () => sql`drop table test`)
496+
return [1, (await sql.unsafe('insert into test values ($1) returning *', [1]))[0].x, await sql`drop table test`]
497+
})
498498

499499
t('unsafe simple', async() => {
500500
return [1, (await sql.unsafe('select 1 as x'))[0].x]
@@ -563,21 +563,21 @@ t('big query body', async() => {
563563
await sql`create table test (x int)`
564564
return [1000, (await sql`insert into test ${
565565
sql([...Array(1000).keys()].map(x => ({ x })))
566-
}`).count]
567-
}, () => sql`drop table test`)
566+
}`).count, await sql`drop table test`]
567+
})
568568

569569
t('Throws if more than 65534 parameters', async() => {
570570
await sql`create table test (x int)`
571571
return ['MAX_PARAMETERS_EXCEEDED', (await sql`insert into test ${
572572
sql([...Array(65535).keys()].map(x => ({ x })))
573-
}`.catch(e => e.code))]
574-
}, () => sql`drop table test`)
573+
}`.catch(e => e.code)), await sql`drop table test`]
574+
})
575575

576576
t('let postgres do implicit cast of unknown types', async() => {
577577
await sql`create table test (x timestamp with time zone)`
578578
const [{ x }] = await sql`insert into test values (${ new Date().toISOString() }) returning *`
579-
return [true, x instanceof Date]
580-
}, () => sql`drop table test`)
579+
return [true, x instanceof Date, await sql`drop table test`]
580+
})
581581

582582
t('only allows one statement', async() =>
583583
['42601', await sql`select 1; select 2`.catch(e => e.code)]
@@ -639,20 +639,20 @@ t('dynamic insert', async() => {
639639
await sql`create table test (a int, b text)`
640640
const x = { a: 42, b: 'the answer' }
641641

642-
return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b]
643-
}, () => sql`drop table test`)
642+
return ['the answer', (await sql`insert into test ${ sql(x) } returning *`)[0].b, await sql`drop table test`]
643+
})
644644

645645
t('dynamic insert pluck', async() => {
646646
await sql`create table test (a int, b text)`
647647
const x = { a: 42, b: 'the answer' }
648648

649-
return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b]
650-
}, () => sql`drop table test`)
649+
return [null, (await sql`insert into test ${ sql(x, 'a') } returning *`)[0].b, await sql`drop table test`]
650+
})
651651

652652
t('array insert', async() => {
653653
await sql`create table test (a int, b int)`
654-
return [2, (await sql`insert into test (a, b) values (${ [1, 2] }) returning *`)[0].b]
655-
}, () => sql`drop table test`)
654+
return [2, (await sql`insert into test (a, b) values (${ [1, 2] }) returning *`)[0].b, await sql`drop table test`]
655+
})
656656

657657
t('parameters in()', async() => {
658658
return [2, (await sql`
@@ -667,34 +667,34 @@ t('dynamic multi row insert', async() => {
667667
await sql`create table test (a int, b text)`
668668
const x = { a: 42, b: 'the answer' }
669669

670-
return ['the answer', (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b]
671-
}, () => sql`drop table test`)
670+
return ['the answer', (await sql`insert into test ${ sql([x, x]) } returning *`)[1].b, await sql`drop table test`]
671+
})
672672

673673
t('dynamic update', async() => {
674674
await sql`create table test (a int, b text)`
675675
await sql`insert into test (a, b) values (17, 'wrong')`
676676

677-
return ['the answer', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b]
678-
}, () => sql`drop table test`)
677+
return ['the answer', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }) } returning *`)[0].b, await sql`drop table test`]
678+
})
679679

680680
t('dynamic update pluck', async() => {
681681
await sql`create table test (a int, b text)`
682682
await sql`insert into test (a, b) values (17, 'wrong')`
683683

684-
return ['wrong', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b]
685-
}, () => sql`drop table test`)
684+
return ['wrong', (await sql`update test set ${ sql({ a: 42, b: 'the answer' }, 'a') } returning *`)[0].b, await sql`drop table test`]
685+
})
686686

687687
t('dynamic select array', async() => {
688688
await sql`create table test (a int, b text)`
689689
await sql`insert into test (a, b) values (42, 'yay')`
690-
return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b]
691-
}, () => sql`drop table test`)
690+
return ['yay', (await sql`select ${ sql(['a', 'b']) } from test`)[0].b, await sql`drop table test`]
691+
})
692692

693693
t('dynamic select args', async() => {
694694
await sql`create table test (a int, b text)`
695695
await sql`insert into test (a, b) values (42, 'yay')`
696-
return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b]
697-
}, () => sql`drop table test`)
696+
return ['yay', (await sql`select ${ sql('a', 'b') } from test`)[0].b, await sql`drop table test`]
697+
})
698698

699699
t('connection parameters', async() => {
700700
const sql = postgres({

tests/test.js

+1-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module.exports.not = () => ignored++
1212
module.exports.t = (...rest) => test(false, ...rest)
1313
module.exports.ot = (...rest) => (only = true, test(true, ...rest))
1414

15-
async function test(o, name, fn, after) {
15+
async function test(o, name, fn) {
1616
const line = new Error().stack.split('\n')[3].split(':')[1]
1717
await 1
1818

@@ -34,11 +34,6 @@ async function test(o, name, fn, after) {
3434
tests[line].failed = true
3535
tests[line].error = err instanceof Error ? err : new Error(util.inspect(err))
3636
})
37-
.then(() => after && after())
38-
.catch((err) => {
39-
tests[line].succeeded = false
40-
tests[line].cleanup = err
41-
})
4237
.then(() => {
4338
++done === Object.keys(tests).length && exit()
4439
})

0 commit comments

Comments
 (0)