Skip to content

Commit 3906ce7

Browse files
committed
Refactor test/main
1 parent 6a6d88a commit 3906ce7

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

test/main.ts

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,68 @@
1+
#!/usr/bin/env node
2+
13
import * as fs from 'fs'
24
import {promisify} from 'util'
35

6+
let passed = 0, total = 0, asyncErrors = 0
7+
function success() {
8+
passed++
9+
}
10+
process
11+
.on('uncaughtException', err => {
12+
console.error('Error occurred in async test:')
13+
console.error(err)
14+
asyncErrors++
15+
})
16+
.on('exit', _ => {
17+
console.log(
18+
String(passed) +
19+
' tests out of ' +
20+
String(total) +
21+
' passed (' +
22+
Math.round(passed / total * 100) +
23+
'%)'
24+
)
25+
process.exitCode = (total - passed) + asyncErrors
26+
})
427
promisify(fs.readdir)(__dirname)
528
.then(testSuites => {
6-
let passed = 0
7-
let total = 0
8-
let asyncErrors = 0
9-
function success() {
10-
passed++
11-
}
12-
function testFile(dir: string, test: string) {
29+
function testFile(dir: string, test: string): Promise<void> {
1330
const file = dir + '/' + test
1431
total++
1532
function error(err: Error) {
16-
console.error('Error in test file ' + file)
33+
console.error('Error in test file', file)
1734
console.error(err)
1835
}
1936
let runTest: Promise<void> | (() => void)
2037
try { runTest = require(file) }
2138
catch (e) {
2239
error(e)
23-
return
40+
return Promise.resolve()
2441
}
2542
if (runTest instanceof Promise) {
26-
runTest
27-
.then(success)
28-
.catch(error)
43+
return runTest.then(success, error)
2944
}
3045
else {
3146
try {
3247
runTest()
3348
success()
3449
}
3550
catch (e) { error(e) }
51+
return Promise.resolve()
3652
}
3753
}
38-
Promise.all(
54+
return Promise.all(
3955
testSuites
4056
.filter(suite => !suite.includes('.'))
4157
.map(testSuite => {
4258
const dir = __dirname + '/' + testSuite
4359
return promisify(fs.readdir)(dir)
44-
.then(tests => {
45-
for (const test of tests.filter(test => test.endsWith('.ts'))) {
46-
testFile(dir, test.replace('.ts', '.js'))
47-
}
48-
})
49-
.catch(err => {})
60+
.then(tests => Promise.all(tests
61+
.filter(test => test.endsWith('.ts'))
62+
.map(test => testFile(dir, test.replace('.ts', '.js')))
63+
))
64+
.catch(err => { throw err })
5065
})
5166
)
52-
process
53-
.on('exit', () => {
54-
console.log(
55-
String(passed) +
56-
' tests out of ' +
57-
String(total) +
58-
' passed (' +
59-
Math.round(passed / total * 100) +
60-
'%)'
61-
)
62-
process.exitCode = (total - passed) + asyncErrors
63-
})
64-
.on('uncaughtException', err => {
65-
console.error('Error occurred in async test:')
66-
console.log(err)
67-
asyncErrors++
68-
})
6967
})
70-
.catch(err => {
71-
throw err
72-
})
68+
.catch(err => { throw err })

0 commit comments

Comments
 (0)