Skip to content

Commit 8ae5160

Browse files
committed
Refactor to use node:test
1 parent bbb8a94 commit 8ae5160

File tree

2 files changed

+56
-29
lines changed

2 files changed

+56
-29
lines changed

package.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,13 @@
4343
"unified": "^10.0.0"
4444
},
4545
"devDependencies": {
46-
"@types/tape": "^5.0.0",
46+
"@types/node": "^20.0.0",
4747
"c8": "^8.0.0",
4848
"is-hidden": "^2.0.0",
4949
"prettier": "^3.0.0",
5050
"remark": "^14.0.0",
5151
"remark-cli": "^11.0.0",
5252
"remark-preset-wooorm": "^9.0.0",
53-
"tape": "^5.0.0",
5453
"type-coverage": "^2.0.0",
5554
"typescript": "^5.0.0",
5655
"xo": "^0.56.0"
@@ -83,6 +82,16 @@
8382
"strict": true
8483
},
8584
"xo": {
85+
"overrides": [
86+
{
87+
"files": [
88+
"test/**/*.js"
89+
],
90+
"rules": {
91+
"no-await-in-loop": "off"
92+
}
93+
}
94+
],
8695
"prettier": true
8796
}
8897
}

test/index.js

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,62 @@
22
* @typedef {import('../index.js').Options} Options
33
*/
44

5-
import fs from 'node:fs'
6-
import path from 'node:path'
7-
import test from 'tape'
5+
import assert from 'node:assert/strict'
6+
import fs from 'node:fs/promises'
7+
import process from 'node:process'
8+
import test from 'node:test'
89
import {remark} from 'remark'
910
import {isHidden} from 'is-hidden'
1011
import remarkToc from '../index.js'
1112

12-
test('Fixtures', (t) => {
13-
const root = path.join('test', 'fixtures')
14-
const fixtures = fs.readdirSync(root)
13+
test('remarkToc', async function (t) {
14+
const base = new URL('fixtures/', import.meta.url)
15+
const folders = await fs.readdir(base)
16+
1517
let index = -1
1618

17-
while (++index < fixtures.length) {
18-
const fixture = fixtures[index]
19+
while (++index < folders.length) {
20+
const folder = folders[index]
21+
22+
if (isHidden(folder)) continue
1923

20-
if (isHidden(fixture)) {
21-
continue
22-
}
24+
await t.test(folder, async function () {
25+
const folderUrl = new URL(folder + '/', base)
26+
const inputUrl = new URL('input.md', folderUrl)
27+
const outputUrl = new URL('output.md', folderUrl)
28+
const configUrl = new URL('config.json', folderUrl)
2329

24-
/** @type {Options} */
25-
let config = {}
30+
const input = String(await fs.readFile(inputUrl))
2631

27-
try {
28-
config = JSON.parse(
29-
String(fs.readFileSync(path.join(root, fixture, 'config.json')))
30-
)
31-
} catch {}
32+
/** @type {Options | undefined} */
33+
let config
34+
/** @type {string} */
35+
let output
3236

33-
t.equal(
34-
remark()
37+
try {
38+
config = JSON.parse(String(await fs.readFile(configUrl)))
39+
} catch {}
40+
41+
const proc = remark()
42+
// To do: use defaults.
3543
.use({settings: {bullet: '-'}})
44+
// @ts-expect-error: to do: fix type.
3645
.use(remarkToc, config)
37-
.processSync(fs.readFileSync(path.join(root, fixture, 'input.md')))
38-
.toString(),
39-
String(fs.readFileSync(path.join(root, fixture, 'output.md'))),
40-
'should work on `' + fixture + '`'
41-
)
42-
}
4346

44-
t.end()
47+
const actual = String(await proc.process(input))
48+
49+
try {
50+
if ('UPDATE' in process.env) {
51+
throw new Error('Updating…')
52+
}
53+
54+
output = String(await fs.readFile(outputUrl))
55+
} catch {
56+
output = actual
57+
await fs.writeFile(outputUrl, actual)
58+
}
59+
60+
assert.equal(actual, String(output))
61+
})
62+
}
4563
})

0 commit comments

Comments
 (0)