Skip to content

Commit

Permalink
Refactor to use node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 25, 2023
1 parent 2f189a6 commit f773697
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 165 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@
"packages/rehype-cli"
],
"devDependencies": {
"@types/tape": "^5.0.0",
"@types/node": "^20.0.0",
"bail": "^2.0.0",
"c8": "^8.0.0",
"hast-util-assert": "^4.0.0",
"prettier": "^3.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"to-vfile": "^8.0.0",
"type-coverage": "^2.0.0",
"type-fest": "^4.0.0",
Expand Down
109 changes: 50 additions & 59 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
* @typedef {import('hast').Root} Root
*/

import fs from 'node:fs'
import assert from 'node:assert/strict'
import fs from 'node:fs/promises'
import path from 'node:path'
import test from 'tape'
import {readSync} from 'to-vfile'
import test from 'node:test'
import {read} from 'to-vfile'
import {removePosition} from 'unist-util-remove-position'
import {assert} from 'hast-util-assert'
import {assert as hastAssert} from 'hast-util-assert'
import {unified} from 'unified'
import rehypeParse from 'rehype-parse'
import rehypeStringify from 'rehype-stringify'
import {rehype} from 'rehype'

const fragment = {fragment: true}

test('rehype().parse(file)', (t) => {
t.deepEqual(
test('rehype().parse(file)', () => {
assert.deepEqual(
unified().use(rehypeParse).parse('Alfred'),
{
type: 'root',
Expand Down Expand Up @@ -54,7 +55,7 @@ test('rehype().parse(file)', (t) => {
'should accept a `string`'
)

t.deepEqual(
assert.deepEqual(
unified()
// @ts-expect-error: to do: type `settings`.
.data('settings', {fragment: true})
Expand All @@ -66,7 +67,7 @@ test('rehype().parse(file)', (t) => {
'should prefer options given to `rehypeParse` over `settings`'
)

t.deepEqual(
assert.deepEqual(
unified()
// @ts-expect-error: to do: type `settings`.
.data('settings', {quote: '"'})
Expand All @@ -84,7 +85,7 @@ test('rehype().parse(file)', (t) => {

removePosition(tree, {force: true})

t.deepEqual(
assert.deepEqual(
tree,
{
type: 'root',
Expand All @@ -110,7 +111,7 @@ test('rehype().parse(file)', (t) => {
const tree2 = unified().use(rehypeParse, fragment).parse('<foo><span></span>')
removePosition(tree2, {force: true})

t.deepEqual(
assert.deepEqual(
tree2,
{
type: 'root',
Expand All @@ -133,12 +134,10 @@ test('rehype().parse(file)', (t) => {
},
'should not close unknown elements by default'
)

t.end()
})

test('rehype().stringify(ast, file, options?)', (t) => {
t.throws(
test('rehype().stringify(ast, file, options?)', () => {
assert.throws(
() => {
// @ts-expect-error: incorrect value.
unified().use(rehypeStringify).stringify(false)
Expand All @@ -147,7 +146,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should throw when `ast` is not a node'
)

t.throws(
assert.throws(
() => {
// @ts-expect-error: unknown node.
unified().use(rehypeStringify).stringify({type: 'unicorn'})
Expand All @@ -156,7 +155,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should throw when `ast` is not a valid node'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify)
.stringify({
Expand All @@ -167,7 +166,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should escape entities'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify, {characterReferences: {}})
.stringify({
Expand All @@ -178,7 +177,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should encode entities (numbered by default)'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify, {characterReferences: {useNamedReferences: true}})
.stringify({
Expand All @@ -189,7 +188,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should encode entities (numbered by default)'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify)
.stringify({
Expand All @@ -202,7 +201,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should not close void elements'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify, {closeSelfClosing: true})
.stringify({
Expand All @@ -215,7 +214,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should close void elements if `closeSelfClosing` is given'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify)
.stringify({
Expand All @@ -228,7 +227,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should not close unknown elements by default'
)

t.equal(
assert.equal(
unified()
.use(rehypeStringify, {voids: ['foo']})
.stringify({
Expand All @@ -241,13 +240,13 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should close void elements if configured'
)

t.deepEqual(
assert.deepEqual(
rehype().processSync('<!doctypehtml>').messages.map(String),
[],
'should not emit parse errors by default'
)

t.deepEqual(
assert.deepEqual(
rehype()
// @ts-expect-error: to do: type `settings`.
.data('settings', {emitParseErrors: true})
Expand All @@ -257,7 +256,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should emit parse errors when `emitParseErrors: true`'
)

t.deepEqual(
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
Expand All @@ -270,7 +269,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should ignore parse errors when the specific rule is turned off'
)

t.deepEqual(
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
Expand All @@ -283,7 +282,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should emit parse errors when the specific rule is turned on'
)

t.deepEqual(
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
Expand All @@ -295,7 +294,7 @@ test('rehype().stringify(ast, file, options?)', (t) => {
'should emit fatal parse errors when the specific rule is `2`'
)

t.deepEqual(
assert.deepEqual(
rehype()
.data('settings', {
// @ts-expect-error: to do: type `settings`.
Expand All @@ -306,35 +305,25 @@ test('rehype().stringify(ast, file, options?)', (t) => {
false,
'should emit fatal parse errors when the specific rule is `1`'
)

t.end()
})

test('fixtures', (t) => {
test('fixtures', async (t) => {
let index = -1
const root = path.join('test', 'fixtures')
const fixtures = fs.readdirSync(root)
const fixtures = await fs.readdir(root)

/* Check the next fixture. */
function next() {
const fixture = fixtures[++index]

if (!fixture) {
t.end()
return
}
while (++index < fixtures.length) {
const fixture = fixtures[index]

if (fixture.charAt(0) === '.') {
setImmediate(next)
return
continue
}

const fp = path.join(root, fixture)

setImmediate(next) // Queue next.

t.test(fixture, (st) => {
const file = readSync(path.join(fp, 'index.html'))
// eslint-disable-next-line no-await-in-loop
await t.test(fixture, async () => {
const file = await read(path.join(fp, 'index.html'))
/** @type {{fragment?: boolean, reprocess?: boolean}} */
let config = {}
/** @type {Root|undefined} */
Expand All @@ -346,38 +335,44 @@ test('fixtures', (t) => {

try {
config = JSON.parse(
String(fs.readFileSync(path.join(fp, 'config.json')))
String(await fs.readFile(path.join(fp, 'config.json')))
)
} catch {}

try {
result = fs.readFileSync(path.join(fp, 'result.html'), 'utf8')
result = await fs.readFile(path.join(fp, 'result.html'), 'utf8')
} catch {}

// @ts-expect-error: to do: type `settings`.
const node = rehype().data('settings', config).parse(file)

try {
tree = JSON.parse(String(fs.readFileSync(path.join(fp, 'index.json'))))
tree = JSON.parse(
String(await fs.readFile(path.join(fp, 'index.json')))
)
} catch {
fs.writeFileSync(
await fs.writeFile(
path.join(fp, 'index.json'),
JSON.stringify(node, null, 2) + '\n'
)
return
}

assert(node)
hastAssert(node)

st.deepEqual(tree, node, 'should parse `' + fixture + '`')
assert.deepEqual(tree, node, 'should parse `' + fixture + '`')

// @ts-expect-error: to do: type `settings`.
const out = rehype().data('settings', config).stringify(node)

if (result) {
st.equal(out, result, 'should stringify `' + fixture + '`')
assert.equal(out, result, 'should stringify `' + fixture + '`')
} else {
st.equal(out, String(file), 'should stringify `' + fixture + '` exact')
assert.equal(
out,
String(file),
'should stringify `' + fixture + '` exact'
)
}

removePosition(node)
Expand All @@ -387,12 +382,8 @@ test('fixtures', (t) => {
removePosition(expected)

if (config.reprocess !== false) {
st.deepEqual(node, expected, 'should re-parse `' + fixture + '`')
assert.deepEqual(node, expected, 'should re-parse `' + fixture + '`')
}

st.end()
})
}

next()
})
Loading

0 comments on commit f773697

Please sign in to comment.