Skip to content

Commit ff5d9df

Browse files
committed
Refactor tests
1 parent 5c3b277 commit ff5d9df

File tree

2 files changed

+77
-52
lines changed

2 files changed

+77
-52
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
"@types/node": "^22.0.0",
1717
"c8": "^10.0.0",
1818
"prettier": "^3.0.0",
19-
"rehype": "^13.0.0",
19+
"rehype-parse": "^9.0.0",
20+
"rehype-stringify": "^10.0.0",
2021
"remark-cli": "^12.0.0",
2122
"remark-preset-wooorm": "^11.0.0",
2223
"type-coverage": "^2.0.0",
2324
"typescript": "^5.0.0",
25+
"unified": "^11.0.0",
2426
"xo": "^0.60.0"
2527
},
2628
"exports": "./index.js",

test.js

Lines changed: 74 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import assert from 'node:assert/strict'
66
import test from 'node:test'
77
import {common} from 'lowlight'
8-
import {rehype} from 'rehype'
98
import rehypeHighlight from 'rehype-highlight'
9+
import rehypeParse from 'rehype-parse'
10+
import rehypeStringify from 'rehype-stringify'
11+
import {unified} from 'unified'
1012

1113
test('rehypeHighlight', async function (t) {
1214
await t.test('should expose the public api', async function () {
@@ -16,9 +18,10 @@ test('rehypeHighlight', async function (t) {
1618
})
1719

1820
await t.test('should work on empty code', async function () {
19-
const file = await rehype()
20-
.data('settings', {fragment: true})
21+
const file = await unified()
22+
.use(rehypeParse, {fragment: true})
2123
.use(rehypeHighlight, {detect: true})
24+
.use(rehypeStringify)
2225
.process(
2326
['<h1>Hello World!</h1>', '', '<pre><code></code></pre>'].join('\n')
2427
)
@@ -34,9 +37,10 @@ test('rehypeHighlight', async function (t) {
3437
})
3538

3639
await t.test('should not highlight (no class)', async function () {
37-
const file = await rehype()
38-
.data('settings', {fragment: true})
40+
const file = await unified()
41+
.use(rehypeParse, {fragment: true})
3942
.use(rehypeHighlight)
43+
.use(rehypeStringify)
4044
.process(
4145
[
4246
'<h1>Hello World!</h1>',
@@ -56,9 +60,10 @@ test('rehypeHighlight', async function (t) {
5660
})
5761

5862
await t.test('should highlight (`detect`, no class)', async function () {
59-
const file = await rehype()
60-
.data('settings', {fragment: true})
63+
const file = await unified()
64+
.use(rehypeParse, {fragment: true})
6165
.use(rehypeHighlight, {detect: true})
66+
.use(rehypeStringify)
6267
.process(
6368
[
6469
'<h1>Hello World!</h1>',
@@ -80,9 +85,10 @@ test('rehypeHighlight', async function (t) {
8085
await t.test(
8186
'should highlight (detect, no class, subset)',
8287
async function () {
83-
const file = await rehype()
84-
.data('settings', {fragment: true})
88+
const file = await unified()
89+
.use(rehypeParse, {fragment: true})
8590
.use(rehypeHighlight, {detect: true, subset: ['arduino']})
91+
.use(rehypeStringify)
8692
.process(
8793
[
8894
'<h1>Hello World!</h1>',
@@ -105,9 +111,10 @@ test('rehypeHighlight', async function (t) {
105111
await t.test(
106112
'should not highlight (`detect: false`, no class)',
107113
async function () {
108-
const file = await rehype()
109-
.data('settings', {fragment: true})
114+
const file = await unified()
115+
.use(rehypeParse, {fragment: true})
110116
.use(rehypeHighlight, {detect: false})
117+
.use(rehypeStringify)
111118
.process(
112119
[
113120
'<h1>Hello World!</h1>',
@@ -128,9 +135,10 @@ test('rehypeHighlight', async function (t) {
128135
)
129136

130137
await t.test('should highlight (prefix without dash)', async function () {
131-
const file = await rehype()
132-
.data('settings', {fragment: true})
138+
const file = await unified()
139+
.use(rehypeParse, {fragment: true})
133140
.use(rehypeHighlight, {detect: true, prefix: 'foo'})
141+
.use(rehypeStringify)
134142
.process(
135143
[
136144
'<h1>Hello World!</h1>',
@@ -150,9 +158,10 @@ test('rehypeHighlight', async function (t) {
150158
})
151159

152160
await t.test('should highlight (prefix with dash)', async function () {
153-
const file = await rehype()
154-
.data('settings', {fragment: true})
161+
const file = await unified()
162+
.use(rehypeParse, {fragment: true})
155163
.use(rehypeHighlight, {detect: true, prefix: 'foo-'})
164+
.use(rehypeStringify)
156165
.process(
157166
[
158167
'<h1>Hello World!</h1>',
@@ -172,9 +181,10 @@ test('rehypeHighlight', async function (t) {
172181
})
173182

174183
await t.test('should highlight (lang class)', async function () {
175-
const file = await rehype()
176-
.data('settings', {fragment: true})
184+
const file = await unified()
185+
.use(rehypeParse, {fragment: true})
177186
.use(rehypeHighlight)
187+
.use(rehypeStringify)
178188
.process(
179189
[
180190
'<h1>Hello World!</h1>',
@@ -196,9 +206,10 @@ test('rehypeHighlight', async function (t) {
196206
})
197207

198208
await t.test('should highlight (language class)', async function () {
199-
const file = await rehype()
200-
.data('settings', {fragment: true})
209+
const file = await unified()
210+
.use(rehypeParse, {fragment: true})
201211
.use(rehypeHighlight)
212+
.use(rehypeStringify)
202213
.process(
203214
[
204215
'<h1>Hello World!</h1>',
@@ -220,9 +231,10 @@ test('rehypeHighlight', async function (t) {
220231
})
221232

222233
await t.test('should highlight (long name)', async function () {
223-
const file = await rehype()
224-
.data('settings', {fragment: true})
234+
const file = await unified()
235+
.use(rehypeParse, {fragment: true})
225236
.use(rehypeHighlight)
237+
.use(rehypeStringify)
226238
.process(
227239
[
228240
'<h1>Hello World!</h1>',
@@ -244,9 +256,10 @@ test('rehypeHighlight', async function (t) {
244256
})
245257

246258
await t.test('should not highlight (`no-highlight`)', async function () {
247-
const file = await rehype()
248-
.data('settings', {fragment: true})
259+
const file = await unified()
260+
.use(rehypeParse, {fragment: true})
249261
.use(rehypeHighlight)
262+
.use(rehypeStringify)
250263
.process(
251264
[
252265
'<h1>Hello World!</h1>',
@@ -270,9 +283,10 @@ test('rehypeHighlight', async function (t) {
270283
await t.test(
271284
'should prefer `no-highlight` over a `language-*` class',
272285
async function () {
273-
const file = await rehype()
274-
.data('settings', {fragment: true})
286+
const file = await unified()
287+
.use(rehypeParse, {fragment: true})
275288
.use(rehypeHighlight)
289+
.use(rehypeStringify)
276290
.process(
277291
'<h1>Hello World!</h1>\n<pre><code class="lang-js no-highlight">alert(1)</code></pre>'
278292
)
@@ -285,9 +299,10 @@ test('rehypeHighlight', async function (t) {
285299
)
286300

287301
await t.test('should not highlight (`nohighlight`)', async function () {
288-
const file = await rehype()
289-
.data('settings', {fragment: true})
302+
const file = await unified()
303+
.use(rehypeParse, {fragment: true})
290304
.use(rehypeHighlight)
305+
.use(rehypeStringify)
291306
.process(
292307
[
293308
'<h1>Hello World!</h1>',
@@ -309,9 +324,10 @@ test('rehypeHighlight', async function (t) {
309324
})
310325

311326
await t.test('should warn on missing languages', async function () {
312-
const file = await rehype()
313-
.data('settings', {fragment: true})
327+
const file = await unified()
328+
.use(rehypeParse, {fragment: true})
314329
.use(rehypeHighlight)
330+
.use(rehypeStringify)
315331
.process(
316332
[
317333
'<h1>Hello World!</h1>',
@@ -329,9 +345,10 @@ test('rehypeHighlight', async function (t) {
329345
await t.test(
330346
'should not highlight plainText-ed languages',
331347
async function () {
332-
const file = await rehype()
333-
.data('settings', {fragment: true})
348+
const file = await unified()
349+
.use(rehypeParse, {fragment: true})
334350
.use(rehypeHighlight, {plainText: ['js']})
351+
.use(rehypeStringify)
335352
.process(
336353
[
337354
'<h1>Hello World!</h1>',
@@ -355,9 +372,10 @@ test('rehypeHighlight', async function (t) {
355372

356373
await t.test('should not remove contents', async function () {
357374
// For some reason this isn’t detected as c++.
358-
const file = await rehype()
359-
.data('settings', {fragment: true})
375+
const file = await unified()
376+
.use(rehypeParse, {fragment: true})
360377
.use(rehypeHighlight, {detect: true, subset: ['cpp']})
378+
.use(rehypeStringify)
361379
.process(`<pre><code>def add(a, b):\n return a + b</code></pre>`)
362380

363381
assert.equal(
@@ -367,9 +385,10 @@ test('rehypeHighlight', async function (t) {
367385
})
368386

369387
await t.test('should support multiple `code`s in a `pre`', async function () {
370-
const file = await rehype()
371-
.data('settings', {fragment: true})
372-
.use(rehypeHighlight).process(`<pre>
388+
const file = await unified()
389+
.use(rehypeParse, {fragment: true})
390+
.use(rehypeHighlight)
391+
.use(rehypeStringify).process(`<pre>
373392
<code class="language-javascript">const a = 1;</code>
374393
<code class="language-python">printf("x")</code>
375394
</pre>`)
@@ -381,9 +400,10 @@ test('rehypeHighlight', async function (t) {
381400
})
382401

383402
await t.test('should reprocess exact', async function () {
384-
const file = await rehype()
385-
.data('settings', {fragment: true})
403+
const file = await unified()
404+
.use(rehypeParse, {fragment: true})
386405
.use(rehypeHighlight)
406+
.use(rehypeStringify)
387407
.process(
388408
[
389409
'<h1>Hello World!</h1>',
@@ -405,11 +425,10 @@ test('rehypeHighlight', async function (t) {
405425
})
406426

407427
await t.test('should parse custom language', async function () {
408-
const file = await rehype()
409-
.data('settings', {fragment: true})
410-
.use(rehypeHighlight, {
411-
aliases: {javascript: ['funkyscript']}
412-
})
428+
const file = await unified()
429+
.use(rehypeParse, {fragment: true})
430+
.use(rehypeHighlight, {aliases: {javascript: ['funkyscript']}})
431+
.use(rehypeStringify)
413432
.process(
414433
'<pre><code class="lang-funkyscript">console.log(1)</code></pre>'
415434
)
@@ -421,9 +440,10 @@ test('rehypeHighlight', async function (t) {
421440
})
422441

423442
await t.test('should reprocess exact', async function () {
424-
const file = await rehype()
425-
.data('settings', {fragment: true})
443+
const file = await unified()
444+
.use(rehypeParse, {fragment: true})
426445
.use(rehypeHighlight)
446+
.use(rehypeStringify)
427447
.process(
428448
[
429449
'<h1>Hello World!</h1>',
@@ -445,9 +465,10 @@ test('rehypeHighlight', async function (t) {
445465
})
446466

447467
await t.test('should ignore comments', async function () {
448-
const file = await rehype()
449-
.data('settings', {fragment: true})
468+
const file = await unified()
469+
.use(rehypeParse, {fragment: true})
450470
.use(rehypeHighlight, {detect: true})
471+
.use(rehypeStringify)
451472
.process(
452473
[
453474
'<h1>Hello World!</h1>',
@@ -467,9 +488,10 @@ test('rehypeHighlight', async function (t) {
467488
})
468489

469490
await t.test('should support `<br>` elements', async function () {
470-
const file = await rehype()
471-
.data('settings', {fragment: true})
491+
const file = await unified()
492+
.use(rehypeParse, {fragment: true})
472493
.use(rehypeHighlight)
494+
.use(rehypeStringify)
473495
.process(
474496
[
475497
'<h1>Hello World!</h1>',
@@ -490,9 +512,10 @@ test('rehypeHighlight', async function (t) {
490512
})
491513

492514
await t.test('should register languages', async function () {
493-
const file = await rehype()
494-
.data('settings', {fragment: true})
515+
const file = await unified()
516+
.use(rehypeParse, {fragment: true})
495517
.use(rehypeHighlight, {languages: {...common, test: testLang}})
518+
.use(rehypeStringify)
496519
.process(
497520
[
498521
'<h1>Hello World!</h1>',

0 commit comments

Comments
 (0)