Skip to content

Commit

Permalink
Use Node test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Jan 24, 2023
1 parent 7b3adaf commit 169f938
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
strategy:
matrix:
node:
- lts/fermium
- lts/gallium
- node
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,14 @@
"unist-util-visit-parents": "^5.1.1"
},
"devDependencies": {
"@types/tape": "^4.0.0",
"@types/node": "^18.0.0",
"c8": "^7.0.0",
"mdast-util-from-markdown": "^1.0.0",
"mdast-util-gfm": "^2.0.0",
"micromark-extension-gfm": "^2.0.0",
"prettier": "^2.0.0",
"remark-cli": "^11.0.0",
"remark-preset-wooorm": "^9.0.0",
"tape": "^5.0.0",
"tsd": "^0.25.0",
"type-coverage": "^2.0.0",
"typescript": "^4.7.0",
Expand Down
170 changes: 71 additions & 99 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* @typedef {import('unist').Literal<string>} Literal
* @typedef {import('unist').Node} Node
* @typedef {import('unist').Parent} Parent
*/

import assert from 'node:assert'
import test from 'tape'
import assert from 'node:assert/strict'
import test from 'node:test'
import {fromMarkdown} from 'mdast-util-from-markdown'
import {gfmFromMarkdown} from 'mdast-util-gfm'
import {gfm} from 'micromark-extension-gfm'
Expand Down Expand Up @@ -48,8 +47,8 @@ const reverseTypes = [
'text'
]

test('unist-util-visit', (t) => {
t.throws(
test('visit', async (t) => {
assert.throws(
() => {
// @ts-expect-error runtime.
visit()
Expand All @@ -58,7 +57,7 @@ test('unist-util-visit', (t) => {
'should fail without tree'
)

t.throws(
assert.throws(
() => {
// @ts-expect-error runtime.
visit(tree)
Expand All @@ -67,14 +66,12 @@ test('unist-util-visit', (t) => {
'should fail without visitor'
)

t.test('should iterate over all nodes', (t) => {
await t.test('should iterate over all nodes', () => {
let n = 0

visit(tree, visitor)

t.equal(n, types.length, 'should visit all nodes')

t.end()
assert.equal(n, types.length, 'should visit all nodes')

/**
* @param {Node} node
Expand All @@ -85,14 +82,12 @@ test('unist-util-visit', (t) => {
}
})

t.test('should iterate over all nodes, backwards', (t) => {
await t.test('should iterate over all nodes, backwards', () => {
let n = 0

visit(tree, visitor, true)

t.equal(n, reverseTypes.length, 'should visit all nodes in reverse')

t.end()
assert.equal(n, reverseTypes.length, 'should visit all nodes in reverse')

/**
* @param {Node} node
Expand All @@ -107,14 +102,12 @@ test('unist-util-visit', (t) => {
}
})

t.test('should only visit a given `type`', (t) => {
await t.test('should only visit a given `type`', () => {
let n = 0

visit(tree, 'text', visitor)

t.equal(n, texts, 'should visit all matching nodes')

t.end()
assert.equal(n, texts, 'should visit all matching nodes')

/**
* @param {Node} node
Expand All @@ -125,15 +118,13 @@ test('unist-util-visit', (t) => {
}
})

t.test('should only visit given `type`s', (t) => {
await t.test('should only visit given `type`s', () => {
const types = ['text', 'inlineCode']
let n = 0

visit(tree, types, visitor)

t.equal(n, texts + codes, 'should visit all matching nodes')

t.end()
assert.equal(n, texts + codes, 'should visit all matching nodes')

/**
* @param {Node} node
Expand All @@ -144,7 +135,7 @@ test('unist-util-visit', (t) => {
}
})

t.test('should accept any `is`-compatible test function', (t) => {
await t.test('should accept any `is`-compatible test function', () => {
let n = 0

visit(tree, test, (node, index, parent) => {
Expand All @@ -153,9 +144,7 @@ test('unist-util-visit', (t) => {
n++
})

t.equal(n, 3, 'should visit all passing nodes')

t.end()
assert.equal(n, 3, 'should visit all passing nodes')

/**
* @param {Node} _
Expand All @@ -166,7 +155,7 @@ test('unist-util-visit', (t) => {
}
})

t.test('should accept an array of `is`-compatible tests', (t) => {
await t.test('should accept an array of `is`-compatible tests', () => {
const expected = new Set(['root', 'paragraph', 'emphasis', 'strong'])
const tests = [
/** @param {Node} node */
Expand All @@ -185,19 +174,15 @@ test('unist-util-visit', (t) => {
n++
})

t.equal(n, 5, 'should visit all passing nodes')

t.end()
assert.equal(n, 5, 'should visit all passing nodes')
})

t.test('should stop if `visitor` stops', (t) => {
await t.test('should stop if `visitor` stops', () => {
let n = 0

visit(tree, visitor)

t.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

t.end()
assert.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

/**
* @param {Node} node
Expand All @@ -208,14 +193,12 @@ test('unist-util-visit', (t) => {
}
})

t.test('should stop if `visitor` stops, backwards', (t) => {
await t.test('should stop if `visitor` stops, backwards', () => {
let n = 0

visit(tree, visitor, true)

t.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

t.end()
assert.equal(n, stopIndex, 'should visit nodes until `EXIT` is given')

/**
* @param {Node} node
Expand All @@ -230,20 +213,18 @@ test('unist-util-visit', (t) => {
}
})

t.test('should skip if `visitor` skips', (t) => {
await t.test('should skip if `visitor` skips', () => {
let n = 0
let count = 0

visit(tree, visitor)

t.equal(
assert.equal(
count,
types.length - 1,
'should visit nodes except when `SKIP` is given'
)

t.end()

/**
* @param {Node} node
*/
Expand All @@ -258,20 +239,18 @@ test('unist-util-visit', (t) => {
}
})

t.test('should skip if `visitor` skips, backwards', (t) => {
await t.test('should skip if `visitor` skips, backwards', () => {
let n = 0
let count = 0

visit(tree, visitor, true)

t.equal(
assert.equal(
count,
reverseTypes.length - 1,
'should visit nodes except when `SKIP` is given'
)

t.end()

/**
* @param {Node} node
*/
Expand All @@ -290,9 +269,9 @@ test('unist-util-visit', (t) => {
}
})

t.test(
await t.test(
'should support a given `index` to iterate over next (`0` to reiterate)',
(t) => {
() => {
let n = 0
let again = false
const expected = [
Expand All @@ -317,9 +296,7 @@ test('unist-util-visit', (t) => {

visit(tree, visitor)

t.equal(n, expected.length, 'should visit nodes again')

t.end()
assert.equal(n, expected.length, 'should visit nodes again')

/**
* @param {Node} node
Expand All @@ -339,9 +316,9 @@ test('unist-util-visit', (t) => {
}
)

t.test(
await t.test(
'should support a given `index` to iterate over next (`children.length` to skip further children)',
(t) => {
() => {
let n = 0
let again = false
const expected = [
Expand All @@ -357,9 +334,7 @@ test('unist-util-visit', (t) => {

visit(tree, visitor)

t.equal(n, expected.length, 'should skip nodes')

t.end()
assert.equal(n, expected.length, 'should skip nodes')

/**
* @param {Node} node
Expand All @@ -381,51 +356,52 @@ test('unist-util-visit', (t) => {
}
)

t.test('should support any other given `index` to iterate over next', (t) => {
let n = 0
let again = false
const expected = [
'root',
'paragraph',
'text',
'emphasis',
'text',
'text',
'strong',
'text',
'inlineCode', // Skip to here.
'text'
]

visit(tree, visitor)
await t.test(
'should support any other given `index` to iterate over next',
() => {
let n = 0
let again = false
const expected = [
'root',
'paragraph',
'text',
'emphasis',
'text',
'text',
'strong',
'text',
'inlineCode', // Skip to here.
'text'
]

t.equal(n, expected.length, 'should skip nodes')
visit(tree, visitor)

t.end()
assert.equal(n, expected.length, 'should skip nodes')

/**
* @param {Node} node
* @param {number|null} index
*/
function visitor(node, index) {
assert.strictEqual(
node.type,
expected[n++],
'should be the expected type'
)
/**
* @param {Node} node
* @param {number|null} index
*/
function visitor(node, index) {
assert.strictEqual(
node.type,
expected[n++],
'should be the expected type'
)

if (
typeof index === 'number' &&
again === false &&
node.type === 'strong'
) {
again = true
return index + 2 // Skip to `inlineCode`.
if (
typeof index === 'number' &&
again === false &&
node.type === 'strong'
) {
again = true
return index + 2 // Skip to `inlineCode`.
}
}
}
})
)

t.test('should visit added nodes', (t) => {
await t.test('should visit added nodes', () => {
const tree = fromMarkdown('Some _emphasis_, **importance**, and `code`.')
const other = /** @type {Parent} */ (
fromMarkdown('Another ~~sentence~~.', {
Expand All @@ -439,9 +415,7 @@ test('unist-util-visit', (t) => {

visit(tree, visitor)

t.equal(n, l, 'should walk over all nodes')

t.end()
assert.equal(n, l, 'should walk over all nodes')

/**
* @param {Node} _1
Expand All @@ -456,6 +430,4 @@ test('unist-util-visit', (t) => {
}
}
})

t.end()
})

0 comments on commit 169f938

Please sign in to comment.