Refactor to use node:test

This commit is contained in:
Titus Wormer 2023-09-15 19:27:30 +02:00
parent 055840baf2
commit 6e6c0ed89f
No known key found for this signature in database
GPG key ID: E6E581152ED04E2E
2 changed files with 66 additions and 46 deletions

View file

@ -39,14 +39,13 @@
"unified": "^10.0.0" "unified": "^10.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/tape": "^5.0.0", "@types/node": "^20.0.0",
"c8": "^8.0.0", "c8": "^8.0.0",
"is-hidden": "^2.0.0", "is-hidden": "^2.0.0",
"prettier": "^3.0.0", "prettier": "^3.0.0",
"remark": "^14.0.0", "remark": "^14.0.0",
"remark-cli": "^11.0.0", "remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0", "remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"to-vfile": "^7.0.0", "to-vfile": "^7.0.0",
"type-coverage": "^2.0.0", "type-coverage": "^2.0.0",
"typescript": "^5.0.0", "typescript": "^5.0.0",
@ -80,6 +79,16 @@
"strict": true "strict": true
}, },
"xo": { "xo": {
"overrides": [
{
"files": [
"test/**/*.js"
],
"rules": {
"no-await-in-loop": "off"
}
}
],
"prettier": true "prettier": true
} }
} }

View file

@ -2,66 +2,77 @@
* @typedef {import('mdast').Root} Root * @typedef {import('mdast').Root} Root
*/ */
import fs from 'node:fs' import assert from 'node:assert/strict'
import path from 'node:path' import fs from 'node:fs/promises'
import test from 'tape' import process from 'node:process'
import {readSync} from 'to-vfile' import test from 'node:test'
import {unified} from 'unified'
import {remark} from 'remark'
import {isHidden} from 'is-hidden' import {isHidden} from 'is-hidden'
import directive from '../index.js' import {remark} from 'remark'
import remarkDirective from '../index.js'
test('directive()', (t) => { test('remarkDirective', async function (t) {
t.doesNotThrow(() => { await t.test('should expose the public api', async function () {
remark().use(directive).freeze() assert.deepEqual(Object.keys(await import('../index.js')).sort(), [
}, 'should not throw if not passed options') 'default'
])
})
t.doesNotThrow(() => { await t.test('should not throw if not passed options', async function () {
unified().use(directive).freeze() assert.doesNotThrow(function () {
}, 'should not throw if without parser or compiler') remark().use(remarkDirective).freeze()
})
t.end() })
}) })
test('fixtures', (t) => { test('fixtures', async function (t) {
const base = path.join('test', 'fixtures') const base = new URL('fixtures/', import.meta.url)
const entries = fs.readdirSync(base).filter((d) => !isHidden(d)) const folders = await fs.readdir(base)
t.plan(entries.length)
let index = -1 let index = -1
while (++index < entries.length) {
const fixture = entries[index] while (++index < folders.length) {
t.test(fixture, (st) => { const folder = folders[index]
const file = readSync(path.join(base, fixture, 'input.md'))
const input = String(file) if (isHidden(folder)) continue
const outputPath = path.join(base, fixture, 'output.md')
const treePath = path.join(base, fixture, 'tree.json') await t.test(folder, async function () {
const proc = remark().use(directive).freeze() const folderUrl = new URL(folder + '/', base)
const actual = proc.parse(file) const inputUrl = new URL('input.md', folderUrl)
/** @type {string} */ const outputUrl = new URL('output.md', folderUrl)
let output const treeUrl = new URL('tree.json', folderUrl)
const input = String(await fs.readFile(inputUrl))
/** @type {Root} */ /** @type {Root} */
let expected let expected
/** @type {string} */
let output
const proc = remark().use(remarkDirective)
const actual = proc.parse(input)
try { try {
expected = JSON.parse(String(fs.readFileSync(treePath))) output = String(await fs.readFile(outputUrl))
} catch {
// New fixture.
fs.writeFileSync(treePath, JSON.stringify(actual, null, 2) + '\n')
expected = actual
}
try {
output = fs.readFileSync(outputPath, 'utf8')
} catch { } catch {
output = input output = input
} }
st.deepEqual(actual, expected, 'tree') try {
st.equal(String(proc.processSync(file)), output, 'process') if ('UPDATE' in process.env) {
throw new Error('Updating…')
}
st.end() expected = JSON.parse(String(await fs.readFile(treeUrl)))
} catch {
expected = actual
// New fixture.
await fs.writeFile(treeUrl, JSON.stringify(actual, undefined, 2) + '\n')
}
assert.deepEqual(actual, expected)
assert.equal(String(await proc.process(input)), String(output))
}) })
} }
}) })