Skip to content

Commit

Permalink
test: replace tap with built-in test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
aldis-ameriks committed Sep 7, 2024
1 parent 4d30bfd commit f1a34ba
Show file tree
Hide file tree
Showing 28 changed files with 11,019 additions and 23,556 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.idea
.tap
node_modules
package-lock.json
test/entities.ts
Expand Down
112 changes: 38 additions & 74 deletions cjs/test/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const childProcess = require('node:child_process')
const fs = require('node:fs')
const path = require('node:path')
const t = require('tap')
const { getTestPostgresConnectionString } = require('./helpers/setup-postgres.js')
const test = require('node:test')
const assert = require('node:assert/strict')

const connection = getTestPostgresConnectionString()
const ssl = process.env.DATABASE_SSL_ENABLED === 'true'

t.test('help', t => {
t.plan(1)

test('help', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), '--help'], {
cwd: __dirname,
env: process.env,
Expand All @@ -23,15 +22,11 @@ t.test('help', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result)
})
})

t.test('version', t => {
t.plan(1)

t.cleanSnapshot = s => s.replace(/v[0-9.]+/g, 'v{v}')

test('version', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), '--version'], {
cwd: __dirname,
env: process.env,
Expand All @@ -45,13 +40,11 @@ t.test('version', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
assert.ok(/v[0-9.]+/.test(result.data))
})
})

t.test('missing connection string', t => {
t.plan(1)

test('missing connection string', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js')], {
cwd: __dirname,
env: process.env,
Expand All @@ -65,13 +58,11 @@ t.test('missing connection string', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result)
})
})

t.test('generates types stdout', t => {
t.plan(1)

test('generates types stdout', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : ''], {
cwd: __dirname,
env: process.env,
Expand All @@ -85,13 +76,11 @@ t.test('generates types stdout', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result)
})
})

t.test('generates types to file', t => {
t.plan(1)

test('generates types to file', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), '-o', './entities.ts', connection, ssl ? '--ssl' : ''], {
cwd: __dirname,
env: process.env,
Expand All @@ -102,14 +91,12 @@ t.test('generates types to file', t => {
child.on('close', () => {
const outputPath = path.join(__dirname, './entities.ts')
const result = fs.readFileSync(outputPath, 'utf8')
t.matchSnapshot(result)
t.assert.snapshot(result)
fs.unlinkSync(outputPath)
})
})

t.test('reports success to stdout', t => {
t.plan(1)

test('reports success to stdout', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), '-o', './entities.ts', connection, ssl ? '--ssl' : ''], {
cwd: __dirname,
env: process.env,
Expand All @@ -123,13 +110,11 @@ t.test('reports success to stdout', t => {
})

child.on('close', () => {
t.equal(result.data, '✔ Generated types from 10 tables and 4 enums\n')
assert.equal(result.data, '✔ Generated types from 10 tables and 4 enums\n')
})
})

t.test('generates types with exclusion', t => {
t.plan(1)

test('generates types with exclusion', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '-e', 'types,snake_test'], {
cwd: __dirname,
env: process.env,
Expand All @@ -143,13 +128,11 @@ t.test('generates types with exclusion', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with header', t => {
t.plan(1)

test('generates types with header', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '-h', '/* eslint-disable */'], {
cwd: __dirname,
env: process.env,
Expand All @@ -163,13 +146,11 @@ t.test('generates types with header', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with pascal case enums', t => {
t.plan(1)

test('generates types with pascal case enums', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--pascal-enums'], {
cwd: __dirname,
env: process.env,
Expand All @@ -183,13 +164,11 @@ t.test('generates types with pascal case enums', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with noSemi option', t => {
t.plan(1)

test('generates types with noSemi option', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--noSemi'], {
cwd: __dirname,
env: process.env,
Expand All @@ -203,13 +182,11 @@ t.test('generates types with noSemi option', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with no-semicolons option', t => {
t.plan(1)

test('generates types with no-semicolons option', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--no-semicolons'], {
cwd: __dirname,
env: process.env,
Expand All @@ -223,13 +200,11 @@ t.test('generates types with no-semicolons option', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with semicolons option', t => {
t.plan(1)

test('generates types with semicolons option', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--semicolons'], {
cwd: __dirname,
env: process.env,
Expand All @@ -243,13 +218,11 @@ t.test('generates types with semicolons option', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with bigint option', t => {
t.plan(1)

test('generates types with bigint option', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--bigint'], {
cwd: __dirname,
env: process.env,
Expand All @@ -263,13 +236,11 @@ t.test('generates types with bigint option', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with types option', t => {
t.plan(1)

test('generates types with types option', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--type'], {
cwd: __dirname,
env: process.env,
Expand All @@ -283,13 +254,11 @@ t.test('generates types with types option', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates types with insert types', t => {
t.plan(1)

test('generates types with insert types', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--insert-types'], {
cwd: __dirname,
env: process.env,
Expand All @@ -303,13 +272,11 @@ t.test('generates types with insert types', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates table names', t => {
t.plan(1)

test('generates table names', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--table-names'], {
cwd: __dirname,
env: process.env,
Expand All @@ -323,13 +290,11 @@ t.test('generates table names', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('generates view names', t => {
t.plan(1)

test('generates view names', t => {
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), connection, ssl ? '--ssl' : '', '--view-names'], {
cwd: __dirname,
env: process.env,
Expand All @@ -343,12 +308,11 @@ t.test('generates view names', t => {
})

child.on('close', () => {
t.matchSnapshot(result.data)
t.assert.snapshot(result.data)
})
})

t.test('sends error to stderr', t => {
t.plan(1)
test('sends error to stderr', t => {
const invalidConnection = 'postgres://postgres:postgres@0.0.0.0:1/test_db'
const child = childProcess.spawn(process.execPath, [path.join(__dirname, '..', 'src', 'index.js'), '-o', './entities.ts', invalidConnection, ssl ? '--ssl' : ''], {
cwd: __dirname,
Expand All @@ -363,6 +327,6 @@ t.test('sends error to stderr', t => {
})

child.on('close', () => {
t.ok(result.data.includes('Error'), `${result.data} is not the expected error`)
assert.ok(result.data.includes('Error'), `${result.data} is not the expected error`)
})
})
Loading

0 comments on commit f1a34ba

Please sign in to comment.