forked from dcastil/tailwind-merge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docs-examples.test.ts
36 lines (29 loc) · 1.05 KB
/
docs-examples.test.ts
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
34
35
36
import fs from 'fs'
import globby from 'globby'
import { expect, test } from 'vitest'
import { twMerge } from '../src'
const twMergeExampleRegex =
/twMerge\((?<arguments>[\w\s\-:[\]#(),!&\n'"]+?)\)(?!.*(?<!\/\/.*)')\s*\n?\s*\/\/\s*→\s*['"](?<result>.+)['"]/g
test('docs examples', () => {
expect.assertions(29)
return forEachFile(['README.md', 'docs/**/*.md'], (fileContent) => {
Array.from(fileContent.matchAll(twMergeExampleRegex)).forEach((match) => {
// eslint-disable-next-line no-eval
const args = eval(`[${match.groups!.arguments}]`)
expect(twMerge(...args)).toBe(match.groups!.result)
})
})
})
async function forEachFile(patterns: string | string[], callback: (fileContent: string) => void) {
const paths = await globby(patterns, {
dot: true,
absolute: true,
onlyFiles: true,
unique: true,
})
await Promise.all(
paths.map((filePath) =>
fs.promises.readFile(filePath, { encoding: 'utf-8' }).then(callback),
),
)
}