Skip to content

Commit d33209b

Browse files
committed
test(loaders): create unit tests
1 parent 747d650 commit d33209b

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// @ts-check
2+
3+
'use strict';
4+
5+
import assert from 'node:assert/strict';
6+
import { describe, it, mock } from 'node:test';
7+
8+
import { VFile } from 'vfile';
9+
10+
const fakeFiles = [
11+
'/fake/path/file1.js',
12+
'/fake/path/file2.js',
13+
'/fake/path/not-a-js-file.txt',
14+
];
15+
16+
const fakeContent = 'const a = 1;';
17+
18+
mock.module('node:fs', {
19+
namedExports: {
20+
globSync: () => fakeFiles,
21+
},
22+
});
23+
24+
mock.module('node:fs/promises', {
25+
namedExports: {
26+
readFile: () => Promise.resolve(fakeContent),
27+
},
28+
});
29+
30+
const createLoader = (await import('../javascript.mjs')).default;
31+
32+
describe('javascript-loader', () => {
33+
it('should load javascript files into VFiles', async () => {
34+
const loader = createLoader();
35+
const vfiles = await loader.loadFiles('*.js');
36+
37+
assert.strictEqual(vfiles.length, 2); // Ignored the .txt file
38+
39+
assert.ok(vfiles[0] instanceof VFile);
40+
assert.strictEqual(vfiles[0].path, fakeFiles[0]);
41+
assert.strictEqual(vfiles[0].value, fakeContent);
42+
assert.strictEqual(vfiles[1].path, fakeFiles[1]);
43+
assert.strictEqual(vfiles[1].value, fakeContent);
44+
});
45+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// @ts-check
2+
3+
'use strict';
4+
5+
import assert from 'node:assert/strict';
6+
import { describe, it, mock } from 'node:test';
7+
import { isDeepStrictEqual } from 'node:util';
8+
9+
import { VFile } from 'vfile';
10+
11+
const fakeContent = '# Hello';
12+
mock.module('node:fs/promises', {
13+
namedExports: {
14+
readFile: () => Promise.resolve(fakeContent),
15+
},
16+
});
17+
18+
mock.module('node:fs', {
19+
namedExports: {
20+
globSync: (/** @type {string[]} */ path) => {
21+
if (isDeepStrictEqual(path, ['ignore.md'])) {
22+
return ['/fake/path/file2.md'];
23+
}
24+
return [
25+
'/fake/path/file1.md',
26+
'/fake/path/file2.md',
27+
'/fake/path/not-a-md-file.txt',
28+
];
29+
},
30+
},
31+
});
32+
33+
const { default: createLoader } = await import('../markdown.mjs');
34+
35+
describe('markdown-loader', () => {
36+
it('should load markdown files into VFiles', async () => {
37+
const fakeFiles = [
38+
'/fake/path/file1.md',
39+
'/fake/path/file2.md',
40+
'/fake/path/not-a-md-file.txt',
41+
];
42+
43+
const loader = createLoader();
44+
const vfiles = await loader.loadFiles(['*.md']);
45+
46+
assert.strictEqual(vfiles.length, 2);
47+
assert.ok(vfiles[0] instanceof VFile);
48+
assert.strictEqual(vfiles[0].path, fakeFiles[0]);
49+
assert.strictEqual(vfiles[0].value, fakeContent);
50+
assert.strictEqual(vfiles[1].path, fakeFiles[1]);
51+
assert.strictEqual(vfiles[1].value, fakeContent);
52+
});
53+
54+
it('should ignore specified files', async () => {
55+
const loader = createLoader();
56+
const vfiles = await loader.loadFiles(['*.md'], ['ignore.md']);
57+
58+
assert.strictEqual(vfiles.length, 1);
59+
assert.strictEqual(vfiles[0].path, '/fake/path/file1.md');
60+
});
61+
});

0 commit comments

Comments
 (0)