|
| 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