From 6e6c0ed89f4afa2744153943a519094ee509f64f Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 15 Sep 2023 19:27:30 +0200 Subject: [PATCH] Refactor to use `node:test` --- package.json | 13 +++++-- test/index.js | 99 ++++++++++++++++++++++++++++----------------------- 2 files changed, 66 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index 04c5ae6..28d9f49 100644 --- a/package.json +++ b/package.json @@ -39,14 +39,13 @@ "unified": "^10.0.0" }, "devDependencies": { - "@types/tape": "^5.0.0", + "@types/node": "^20.0.0", "c8": "^8.0.0", "is-hidden": "^2.0.0", "prettier": "^3.0.0", "remark": "^14.0.0", "remark-cli": "^11.0.0", "remark-preset-wooorm": "^9.0.0", - "tape": "^5.0.0", "to-vfile": "^7.0.0", "type-coverage": "^2.0.0", "typescript": "^5.0.0", @@ -80,6 +79,16 @@ "strict": true }, "xo": { + "overrides": [ + { + "files": [ + "test/**/*.js" + ], + "rules": { + "no-await-in-loop": "off" + } + } + ], "prettier": true } } diff --git a/test/index.js b/test/index.js index de8fa57..6582d8e 100644 --- a/test/index.js +++ b/test/index.js @@ -2,66 +2,77 @@ * @typedef {import('mdast').Root} Root */ -import fs from 'node:fs' -import path from 'node:path' -import test from 'tape' -import {readSync} from 'to-vfile' -import {unified} from 'unified' -import {remark} from 'remark' +import assert from 'node:assert/strict' +import fs from 'node:fs/promises' +import process from 'node:process' +import test from 'node:test' import {isHidden} from 'is-hidden' -import directive from '../index.js' +import {remark} from 'remark' +import remarkDirective from '../index.js' -test('directive()', (t) => { - t.doesNotThrow(() => { - remark().use(directive).freeze() - }, 'should not throw if not passed options') +test('remarkDirective', async function (t) { + await t.test('should expose the public api', async function () { + assert.deepEqual(Object.keys(await import('../index.js')).sort(), [ + 'default' + ]) + }) - t.doesNotThrow(() => { - unified().use(directive).freeze() - }, 'should not throw if without parser or compiler') - - t.end() + await t.test('should not throw if not passed options', async function () { + assert.doesNotThrow(function () { + remark().use(remarkDirective).freeze() + }) + }) }) -test('fixtures', (t) => { - const base = path.join('test', 'fixtures') - const entries = fs.readdirSync(base).filter((d) => !isHidden(d)) - - t.plan(entries.length) +test('fixtures', async function (t) { + const base = new URL('fixtures/', import.meta.url) + const folders = await fs.readdir(base) let index = -1 - while (++index < entries.length) { - const fixture = entries[index] - t.test(fixture, (st) => { - const file = readSync(path.join(base, fixture, 'input.md')) - const input = String(file) - const outputPath = path.join(base, fixture, 'output.md') - const treePath = path.join(base, fixture, 'tree.json') - const proc = remark().use(directive).freeze() - const actual = proc.parse(file) - /** @type {string} */ - let output + + while (++index < folders.length) { + const folder = folders[index] + + if (isHidden(folder)) continue + + await t.test(folder, async function () { + const folderUrl = new URL(folder + '/', base) + const inputUrl = new URL('input.md', folderUrl) + const outputUrl = new URL('output.md', folderUrl) + const treeUrl = new URL('tree.json', folderUrl) + + const input = String(await fs.readFile(inputUrl)) + /** @type {Root} */ let expected + /** @type {string} */ + let output + + const proc = remark().use(remarkDirective) + const actual = proc.parse(input) try { - expected = JSON.parse(String(fs.readFileSync(treePath))) - } catch { - // New fixture. - fs.writeFileSync(treePath, JSON.stringify(actual, null, 2) + '\n') - expected = actual - } - - try { - output = fs.readFileSync(outputPath, 'utf8') + output = String(await fs.readFile(outputUrl)) } catch { output = input } - st.deepEqual(actual, expected, 'tree') - st.equal(String(proc.processSync(file)), output, 'process') + try { + 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)) }) } })