-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
33 lines (27 loc) · 1.19 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import path from 'node:path'
import {readFileSync} from 'node:fs'
import {fileURLToPath} from 'node:url'
import {test, expect} from 'vitest'
import posthtml from 'posthtml'
import plugin from '../lib/index.js'
import asyncPlugin from '../lib/index-async.js'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const fixture = file => readFileSync(path.join(__dirname, 'fixtures', `${file}.html`), 'utf8').trim()
const expected = file => readFileSync(path.join(__dirname, 'expected', `${file}.html`), 'utf8').trim()
// eslint-disable-next-line
const error = (name, options, cb) => posthtml([plugin(options)]).process(fixture(name)).catch(cb)
const clean = html => html.replaceAll(/[^\S\r\n]+$/gm, '').trim()
const process = (name, options, log = false) => {
return posthtml([plugin(options)])
.process(fixture(name))
.then(result => log ? console.log(result.html) : clean(result.html)) // eslint-disable-line
.then(html => expect(html).toEqual(expected(name)))
}
test('Basic', () => {
return process('basic')
})
test('Async plugin', () => {
return posthtml([asyncPlugin()])
.process(fixture('basic'))
.then(result => expect(clean(result.html)).toEqual(expected('basic')))
})