Skip to content

Commit 23e9b15

Browse files
committed
Fix #10, circular reference, release as 4.2.0
1 parent d9a49d1 commit 23e9b15

6 files changed

Lines changed: 607 additions & 589 deletions

File tree

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- 13
3+
- 16
4+
- 14
45
- 12
5-
- 10

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44
The format is (loosely) based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [4.1.1](https://github.com/doesdev/mvt/compare/4.1.1...4.2.0)
8+
#### 2021-07-10
9+
10+
### Changed
11+
- Fix #10, caused by circular require
12+
- Update Travis to use node 16, 14, 12
13+
- Update ava
14+
715
## [4.1.1](https://github.com/doesdev/mvt/compare/4.1.0...4.1.1)
816
#### 2020-07-16
917

lib/runners.js

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,86 +3,89 @@
33
const { state } = require('./state')
44
const { assert } = require('./assertions')
55
const { handleError } = require('./errors')
6-
const { failing } = require('./tests')
76
const { reporter, info } = require('./reporters')
87
const { char, color, fmtMs } = require('./utility')
98

10-
const runner = async (t, noExit) => {
11-
const { msg, fn, failing, benchOpts, fileName: currFile } = t
9+
const getRunner = (tests) => {
10+
const runner = async (t, noExit) => {
11+
const { msg, fn, failing, benchOpts, fileName: currFile } = t
1212

13-
if (currFile) {
14-
if (currFile !== state.lastFileName) {
15-
state.fileTestCount = 0
16-
info(`\nRunning tests for ${currFile}\n\n`)
17-
}
13+
if (currFile) {
14+
if (currFile !== state.lastFileName) {
15+
state.fileTestCount = 0
16+
info(`\nRunning tests for ${currFile}\n\n`)
17+
}
1818

19-
state.lastFileName = currFile
20-
}
19+
state.lastFileName = currFile
20+
}
2121

22-
if (benchOpts) return benchRunner(t)
22+
if (benchOpts) return benchRunner(t)
2323

24-
const start = Date.now()
24+
const start = Date.now()
2525

26-
try {
27-
await fn(assert(msg, failing))
28-
} catch (error) {
29-
if (!failing) return handleError(msg, error, noExit)
26+
try {
27+
await fn(assert(msg, failing))
28+
} catch (error) {
29+
if (!failing) return handleError(msg, error, noExit)
3030

31-
const ms = ` (${fmtMs(Date.now() - start)})`
32-
const out = `${char('okFail')} ${color.red}${msg}${ms}`
33-
return reporter({ msg, out, error, pass: false, mod: 'failing' })
34-
}
31+
const ms = ` (${fmtMs(Date.now() - start)})`
32+
const out = `${char('okFail')} ${color.red}${msg}${ms}`
33+
return reporter({ msg, out, error, pass: false, mod: 'failing' })
34+
}
3535

36-
const ms = Date.now() - start
36+
const ms = Date.now() - start
3737

38-
if (failing) {
39-
return handleError(msg, new Error('Passed test called with test.failing'))
40-
}
38+
if (failing) {
39+
return handleError(msg, new Error('Passed test called with test.failing'))
40+
}
4141

42-
if (!msg) return
42+
if (!msg) return
4343

44-
const out = `${char('good')} ${msg} (${fmtMs(ms)})`
45-
return reporter({ msg, out, pass: true })
46-
}
44+
const out = `${char('good')} ${msg} (${fmtMs(ms)})`
45+
return reporter({ msg, out, pass: true })
46+
}
4747

48-
const benchRunner = async ({ msg, fn, benchOpts }) => {
49-
const { samples, max } = benchOpts
50-
const start = process.hrtime()
51-
let msAvg
52-
53-
try {
54-
if (benchOpts.parallel) {
55-
const wrapped = async () => {
56-
const iStart = process.hrtime()
57-
await fn(assert(msg, failing))
58-
return process.hrtime(iStart)
48+
const benchRunner = async ({ msg, fn, benchOpts }) => {
49+
const { samples, max } = benchOpts
50+
const start = process.hrtime()
51+
let msAvg
52+
53+
try {
54+
if (benchOpts.parallel) {
55+
const wrapped = async () => {
56+
const iStart = process.hrtime()
57+
await fn(assert(msg, tests.failing))
58+
return process.hrtime(iStart)
59+
}
60+
const times = await Promise.all([...Array(samples)].map(wrapped))
61+
msAvg = parseInt(times.reduce((accum, curval) => {
62+
return accum + (curval[0] * 1e3 + curval[1] / 1e6)
63+
}, 0) / samples, 10)
64+
} else {
65+
for (let i = 0; i < samples; i++) await fn(assert(msg, tests.failing))
5966
}
60-
const times = await Promise.all([...Array(samples)].map(wrapped))
61-
msAvg = parseInt(times.reduce((accum, curval) => {
62-
return accum + (curval[0] * 1e3 + curval[1] / 1e6)
63-
}, 0) / samples, 10)
64-
} else {
65-
for (let i = 0; i < samples; i++) await fn(assert(msg, failing))
67+
} catch (ex) {
68+
return handleError(msg, ex)
6669
}
67-
} catch (ex) {
68-
return handleError(msg, ex)
69-
}
7070

71-
const ranFor = process.hrtime(start)
72-
const msTotal = ranFor[0] * 1e3 + ranFor[1] / 1e6
73-
msAvg = msAvg || parseInt(msTotal / samples, 10)
71+
const ranFor = process.hrtime(start)
72+
const msTotal = ranFor[0] * 1e3 + ranFor[1] / 1e6
73+
msAvg = msAvg || parseInt(msTotal / samples, 10)
7474

75-
if (typeof benchOpts.cb === 'function') {
76-
benchOpts.cb({ msTotal, msAvg })
77-
}
75+
if (typeof benchOpts.cb === 'function') {
76+
benchOpts.cb({ msTotal, msAvg })
77+
}
78+
79+
if (msAvg > max) {
80+
const maxErr = new Error(`Bench failed: (${fmtMs(msAvg)} > ${fmtMs(max)})`)
81+
return handleError(msg, maxErr)
82+
}
7883

79-
if (msAvg > max) {
80-
const maxErr = new Error(`Bench failed: (${fmtMs(msAvg)} > ${fmtMs(max)})`)
81-
return handleError(msg, maxErr)
84+
const out = `${char('good')} ${msg} (${fmtMs(msAvg)} avg)`
85+
return reporter({ msg, out, pass: true })
8286
}
8387

84-
const out = `${char('good')} ${msg} (${fmtMs(msAvg)} avg)`
85-
return reporter({ msg, out, pass: true })
88+
return { runner, benchRunner }
8689
}
8790

88-
module.exports = { runner, benchRunner }
91+
module.exports = { getRunner }

lib/tests.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
const { state, setup } = require('./state')
44
const { finalizeError } = require('./errors')
5-
const { runner } = require('./runners')
5+
const { getRunner } = require('./runners')
66
const { assert } = require('./assertions')
77
const { reporter, summary } = require('./reporters')
88
const { color, parseMs, char } = require('./utility')
99

1010
const test = async (msg, fn) => {
1111
if (msg && fn) state.queue.push({ msg, fn, fileName: state.fileName })
1212

13+
const { runner } = getRunner(test)
1314
const curLen = state.queue.length
1415
process.nextTick(async () => {
1516
if (curLen !== state.queue.length) return

0 commit comments

Comments
 (0)