Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
75lb committed Aug 29, 2024
1 parent 3d758cf commit a7c1369
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 29 deletions.
47 changes: 23 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ function indent (input, indentWith) {
return lines.join(os.EOL)
}

function createTests (arr, map, file) {
for (const [name, testFn] of map) {
const test = new Test(name, testFn)
test.metadata.file = file
arr.push(test)
}
}

process.on('uncaughtException', (err, origin) => {
console.error(`\nAn ${origin} was thrown, possibly in a separate tick.\n`)
console.error(err)
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('\nAn unhandledRejection was thrown. Please ensure the rejecting promise is returned from the test function.\n')
console.error(reason)
process.exit(1)
})

class TestRunner {
tests

Expand All @@ -22,14 +41,13 @@ class TestRunner {

async * run () {
for (const test of this.tests) {
console.log(`${ansi.format(test.metadata.file, ['magenta'])} ${test.name}`)
console.log(`${ansi.format(test.metadata?.file || '', ['magenta'])} ${test.name}`)
try {
await test.run()
} catch (err) {
console.error(err)
console.log(`${ansi.format(test.metadata.file, ['magenta'])} ${test.name} - ${ansi.format('Failed', ['red'])}`)
} finally {
yield test
console.log(`${ansi.format(test.metadata?.file || '', ['magenta'])} ${test.name} - ${ansi.format('Failed', ['red'])}`)
/* Crash the process */
throw err
}
}
}
Expand All @@ -46,25 +64,6 @@ class TestRunner {
const tests = []
const only = []

process.on('uncaughtException', (err, origin) => {
console.error(`\nAn ${origin} was thrown, possibly in a separate tick.\n`)
console.error(err)
process.exit(1)
})
process.on('unhandledRejection', (reason, promise) => {
console.error('\nAn unhandledRejection was thrown. Please ensure the rejecting promise is returned from the test function.\n')
console.error(reason)
process.exit(1)
})

function createTests (arr, map, file) {
for (const [name, testFn] of map) {
const test = new Test(name, testFn)
test.metadata.file = file
arr.push(test)
}
}

for (const file of files) {
const importPath = pathToFileURL(file).href
const testModule = await import(importPath)
Expand Down
6 changes: 5 additions & 1 deletion test/fixture/one.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const test = new Map()

test.set('one', function one () {
test.set('one sync', function one () {
return 'one'
})

test.set('one async', async function onea () {
return 'one'
})

Expand Down
6 changes: 5 additions & 1 deletion test/fixture/two.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const test = new Map()

test.set('two', function two () {
test.set('two sync', function two () {
throw new Error('broken')
})

test.set('two async', function twoa () {
throw new Error('broken')
})

Expand Down
20 changes: 17 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@ async function one () {
}
one()

/* Async test passes, storing the result */
async function onea () {
const actuals = []
const test1 = new Test('onea', async function onea () {
actuals.push('one')
return 'one'
})
const runner = new TestRunner([test1])
await runner.runAll()
a.equal(test1.result, 'one')
a.deepEqual(actuals, ['one'])
}
onea()

/* Sync test fails, crashing the process */
async function syncFail () {
const actuals = []
const test1 = new Test('one', function one () {
actuals.push('one')
const test1 = new Test('syncFail', function syncFail () {
actuals.push('syncFail')
throw new Error('broken')
})
const runner = new TestRunner([test1])
Expand All @@ -32,7 +46,7 @@ async function syncFail () {
} catch (err) {
a.equal(err.message, 'broken')
a.equal(test1.result, undefined)
a.deepEqual(actuals, ['one'])
a.deepEqual(actuals, ['syncFail'])
}
}
syncFail()
Expand Down

0 comments on commit a7c1369

Please sign in to comment.