Skip to content

Commit c54b7d6

Browse files
committed
Drop CommonJS suppport, support ES Modules, 5-beta.1
1 parent 8c5839e commit c54b7d6

26 files changed

Lines changed: 1181 additions & 3313 deletions

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ 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+
## [5.0.0](https://github.com/doesdev/mvt/compare/4.2.1...5.0.0)
8+
#### 2022-03-03
9+
10+
### BREAKING
11+
- ES Module format supported, CommonJS no longer supported
12+
713
## [4.2.1](https://github.com/doesdev/mvt/compare/4.2.0...4.2.1)
814
#### 2021-07-10
915

README.md

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

33
> Minimum Viable Testing framework
44
5+
## 5.0.0
6+
Version 5.0.0+ deprecates support for CommonJS, opting to only support ES Modules.
7+
8+
Please use `mvt@4` for CommonJS usage.
9+
510
## A Minimalist Take on AVA's Approach to Testing
611
Because [AVA](https://github.com/avajs/ava) is awesome. Security alerts on dev
712
dependencies are not awesome. Especially when you use the same test library
@@ -68,7 +73,7 @@ node test.js --verbose
6873
```
6974

7075
```js
71-
const test = require('mvt')
76+
import test from 'mvt'
7277

7378
test.setup({ verbose: true })
7479

cli.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#! /usr/bin/env node
2-
'use strict'
2+
import fs from 'fs'
3+
import path from 'path'
4+
import { pathToFileURL } from 'url'
35

4-
const fs = require('fs')
5-
const path = require('path')
6-
const { pathToFileURL } = require('url')
76
const cwd = process.cwd()
87
const exts = ['.js', '.mjs']
98
const args = process.argv.slice(2)
9+
const setup = {}
1010
const flags = {
1111
'-v': 'verbose',
1212
'--verbose': 'verbose',
@@ -17,7 +17,6 @@ const flags = {
1717
'-p': 'persist',
1818
'--persist': 'persist'
1919
}
20-
const setup = {}
2120

2221
const deflagged = args.filter((a) => !(flags[a] && (setup[flags[a]] = true)))
2322

@@ -77,13 +76,14 @@ const main = async () => {
7776

7877
let test
7978
try {
80-
test = require(path.resolve(cwd, 'node_modules', 'mvt'))
79+
test = (await import(path.resolve(cwd, 'node_modules', 'mvt'))).default
8180
} catch (ex) {
82-
test = require('./index')
81+
test = (await import('./index.js')).default
8382
}
8483

8584
const seen = {}
86-
files.forEach((f) => {
85+
86+
for (const f of files) {
8787
if (seen[f]) return
8888
seen[f] = true
8989

@@ -96,21 +96,11 @@ const main = async () => {
9696
try {
9797
test.setup(Object.assign({}, setup, { fileName: path.basename(f) }))
9898

99-
try {
100-
require(f)
101-
} catch (ex) {
102-
try {
103-
/* eslint-disable no-unused-expressions */
104-
import(pathToFileURL(f))
105-
/* eslint-enable no-unused-expressions */
106-
} catch (ex) {
107-
logFailedImport(ex)
108-
}
109-
}
99+
await import(pathToFileURL(f))
110100
} catch (ex) {
111101
logFailedImport(ex)
112102
}
113-
})
103+
}
114104
}
115105

116106
main().catch(console.error)

index.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
'use strict'
2-
1+
import { test } from './lib/tests.js'
2+
import { _setup } from './lib/state.js'
33
process.env.NODE_ENV = process.env.NODE_ENV || 'test'
4-
const { test } = require('./lib/tests')
5-
const { _setup } = require('./lib/state')
64

75
test.before(_setup)
86

9-
module.exports = test
7+
export default test

lib/assertions.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
'use strict'
1+
import { strict } from 'assert'
2+
import { handleError, diffError } from './errors.js'
3+
import { toPrint } from './utility.js'
24

3-
const { deepStrictEqual, notDeepStrictEqual } = require('assert').strict
4-
const { handleError, diffError } = require('./errors')
5-
const { toPrint } = require('./utility')
5+
const { deepStrictEqual, notDeepStrictEqual } = strict
66

7-
const assert = (msg, f) => ({
7+
export const assert = (msg, f) => ({
88
is: is(msg, f),
99
not: not(msg, f),
1010
pass: pass(msg, f),
@@ -25,8 +25,6 @@ const assert = (msg, f) => ({
2525
notThrowsAsync: notThrowsAsync(msg, f)
2626
})
2727

28-
module.exports = { assert }
29-
3028
const wrap = (msg, passFn, err, failing) => {
3129
let passed = false
3230
try {

lib/cli-char-supported.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
'use strict'
1+
import readline from 'readline'
22

3-
const readline = require('readline')
43
const scanner = '\x1b[6n'
54
const appleTerm = process.env.TERM_PROGRAM === 'Apple_Terminal'
65
const xterm = process.env.TERM === 'xterm-256color'
7-
const charOffset = appleTerm || xterm ? -1 : 0
6+
const baseCharOffset = appleTerm || xterm ? -1 : 0
87

9-
const checkChar = (char, expected) => new Promise((resolve, reject) => {
8+
export const charOffset = Math.abs(baseCharOffset)
9+
10+
export const checkChar = (char, expected) => new Promise((resolve, reject) => {
1011
if (!process.stdin.isTTY) return resolve(false)
1112
let clean = false
1213

@@ -34,7 +35,7 @@ const checkChar = (char, expected) => new Promise((resolve, reject) => {
3435

3536
const checker = (d) => {
3637
const val = +((`${d}` || '').match(/;(\d+)R/) || [])[1]
37-
return cleanup(val === expect || ((val + charOffset) === expect))
38+
return cleanup(val === expect || ((val + baseCharOffset) === expect))
3839
}
3940

4041
const timeout = setTimeout(() => cleanup(false, true), 500)
@@ -47,5 +48,3 @@ const checkChar = (char, expected) => new Promise((resolve, reject) => {
4748
process.stdout.write(char)
4849
process.stdout.write(scanner)
4950
})
50-
51-
module.exports = { checkChar, charOffset: Math.abs(charOffset) }

lib/errors.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
'use strict'
1+
import { strict } from 'assert'
2+
import { char, color, toPrint } from './utility.js'
23

3-
const { deepStrictEqual } = require('assert').strict
4-
const { char, color, toPrint } = require('./utility')
4+
const { deepStrictEqual } = strict
55

6-
const handleError = (msg, err, noExit) => {
6+
let differ
7+
import('diff').then((mod) => { differ = mod }).catch(() => {})
8+
9+
export const handleError = (msg, err, noExit) => {
710
process.stderr.write(`${char('fail')} ${msg}\n`)
811
err = err || `Failed: ${msg}`
912
err = err instanceof Error ? err : new Error(err)
@@ -18,7 +21,7 @@ const handleError = (msg, err, noExit) => {
1821
throw err
1922
}
2023

21-
const finalizeError = (error) => {
24+
export const finalizeError = (error) => {
2225
const { noExit } = error.metaMvt || {}
2326
delete error.metaMvt
2427

@@ -27,12 +30,10 @@ const finalizeError = (error) => {
2730
if (!noExit) process.exit(1)
2831
}
2932

30-
const diffError = (a, b, errName = 'Values should be identical') => {
33+
export const diffError = (a, b, errName = 'Values should be identical') => {
3134
let diff
3235

3336
try {
34-
const differ = require('diff')
35-
3637
diff = ''
3738
differ.diffLines(a, b).forEach(({ added, removed, value }) => {
3839
const colored = added ? color.green : (removed ? color.red : color.grey)
@@ -53,5 +54,3 @@ const diffError = (a, b, errName = 'Values should be identical') => {
5354

5455
return new Error(`${errName}:\n${diff.trim()}`)
5556
}
56-
57-
module.exports = { handleError, diffError, finalizeError }

lib/reporters.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
'use strict'
1+
import readline from 'readline'
2+
import { state } from './state.js'
3+
import { char, color, fmtMs, plural } from './utility.js'
24

3-
const readline = require('readline')
4-
const { state } = require('./state')
5-
const { char, color, fmtMs, plural } = require('./utility')
6-
7-
const writeNonVerboseCount = () => {
5+
export const writeNonVerboseCount = () => {
86
const count = ++state.fileTestCount
97
readline.cursorTo(process.stdout, 0)
108
readline.moveCursor(process.stdout, 0, -2)
119
process.stdout.write(`\n${char('good')} ${count} tests passed\n`)
1210
}
1311

14-
const info = (msg) => process.stdout.write(msg)
12+
export const info = (msg) => process.stdout.write(msg)
1513

16-
const reporter = ({ msg, pass, out, error, mod }) => {
14+
export const reporter = ({ msg, pass, out, error, mod }) => {
1715
const skipNonVerbose = { skip: true, todo: true }
1816
if (out) {
1917
if (state.verbose) {
@@ -24,7 +22,7 @@ const reporter = ({ msg, pass, out, error, mod }) => {
2422
}
2523
}
2624

27-
const summary = (ms) => {
25+
export const summary = (ms) => {
2826
const counts = state.counts
2927
const result = `${color.green}All tests passed in ${fmtMs(ms)}${color.reset}`
3028
process.stdout.write(`\n${char('good')} ${result}\n`)
@@ -52,5 +50,3 @@ const summary = (ms) => {
5250
}
5351
}
5452
}
55-
56-
module.exports = { reporter, writeNonVerboseCount, info, summary }

lib/runners.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
'use strict'
1+
import { state } from './state.js'
2+
import { assert } from './assertions.js'
3+
import { handleError } from './errors.js'
4+
import { reporter, info } from './reporters.js'
5+
import { char, color, fmtMs } from './utility.js'
26

3-
const { state } = require('./state')
4-
const { assert } = require('./assertions')
5-
const { handleError } = require('./errors')
6-
const { reporter, info } = require('./reporters')
7-
const { char, color, fmtMs } = require('./utility')
8-
9-
const getRunner = (tests) => {
7+
export const getRunner = (tests) => {
108
const runner = async (t, noExit) => {
119
const { msg, fn, failing, benchOpts, fileName: currFile } = t
1210

@@ -87,5 +85,3 @@ const getRunner = (tests) => {
8785

8886
return { runner, benchRunner }
8987
}
90-
91-
module.exports = { getRunner }

lib/state.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
'use strict'
1+
import { initCharCheck } from './utility.js'
22

3-
const { initCharCheck } = require('./utility')
43
const argv = process.argv
54

65
const initTap = argv.some((a) => a === '--tap' || a === '-t')
76
const initPersist = initTap || argv.some((a) => a === '--persist' || a === '-p')
87
const initVerbose = !initTap && argv.some((a) => a === '--verbose' || a === '-v')
98

10-
const state = {
9+
export const state = {
1110
queue: [],
1211
counts: {},
1312
fileTestCount: 0,
@@ -16,7 +15,7 @@ const state = {
1615
verbose: initVerbose
1716
}
1817

19-
const _setup = async (opts = {}) => {
18+
export const _setup = async (opts = {}) => {
2019
const setForKey = (k) => opts[k] !== undefined ? !!opts[k] : state[k]
2120
state.tap = setForKey('tap')
2221
state.persist = state.tap || setForKey('persist')
@@ -26,9 +25,7 @@ const _setup = async (opts = {}) => {
2625
await initCharCheck()
2726
}
2827

29-
const setup = (opts = {}) => state.queue.push({
28+
export const setup = (opts = {}) => state.queue.push({
3029
fn: () => _setup(opts),
3130
fileName: opts.fileName
3231
})
33-
34-
module.exports = { state, setup, _setup }

0 commit comments

Comments
 (0)