|
| 1 | +/** |
| 2 | + * @file Unit Tests - createModuleResolutionHost |
| 3 | + * @module tsconfig-utils/lib/tests/unit/createModuleResolutionHost |
| 4 | + */ |
| 5 | + |
| 6 | +import testSubject from '#lib/create-module-resolution-host' |
| 7 | +import * as mlly from '@flex-development/mlly' |
| 8 | +import pathe from '@flex-development/pathe' |
| 9 | +import type { ModuleResolutionHost } from '@flex-development/tsconfig-utils' |
| 10 | +import ts from 'typescript' |
| 11 | + |
| 12 | +describe('unit:lib/createModuleResolutionHost', () => { |
| 13 | + let subject: ModuleResolutionHost |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + subject = testSubject() |
| 17 | + }) |
| 18 | + |
| 19 | + describe('host', () => { |
| 20 | + describe('directoryExists', () => { |
| 21 | + it.each<Parameters<ModuleResolutionHost['directoryExists']>>([ |
| 22 | + [import.meta.url], |
| 23 | + [new URL('node:fs')] |
| 24 | + ])('should return `false` if `id` is not a directory (%j)', id => { |
| 25 | + expect(subject.directoryExists(id)).to.be.false |
| 26 | + }) |
| 27 | + |
| 28 | + it.each<Parameters<ModuleResolutionHost['directoryExists']>>([ |
| 29 | + [String(pathe.cwd())], |
| 30 | + [new URL(mlly.cwd())] |
| 31 | + ])('should return `true` if `id` is a directory (%j)', id => { |
| 32 | + expect(subject.directoryExists(id)).to.be.true |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + describe('fileExists', () => { |
| 37 | + it.each<Parameters<ModuleResolutionHost['fileExists']>>([ |
| 38 | + ['package-lock.json'], |
| 39 | + [String(pathe.cwd())], |
| 40 | + [new URL(mlly.cwd())] |
| 41 | + ])('should return `false` if `id` is not a file (%j)', id => { |
| 42 | + expect(subject.fileExists(id)).to.be.false |
| 43 | + }) |
| 44 | + |
| 45 | + it.each<Parameters<ModuleResolutionHost['fileExists']>>([ |
| 46 | + ['package.json'], |
| 47 | + [String(new URL('vitest.config.mts', mlly.cwd()))], |
| 48 | + [import.meta.url] |
| 49 | + ])('should return `true` if `id` is a file (%j)', id => { |
| 50 | + expect(subject.fileExists(id)).to.be.true |
| 51 | + }) |
| 52 | + }) |
| 53 | + |
| 54 | + describe('getCurrentDirectory', () => { |
| 55 | + it('should return path to current working directory', () => { |
| 56 | + expect(subject.getCurrentDirectory()).to.eq(pathe.cwd() + pathe.sep) |
| 57 | + }) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('getDirectories', () => { |
| 61 | + it('should return list of subdirectory names', () => { |
| 62 | + // Arrange |
| 63 | + const id: string = pathe.dirname(pathe.dirname(import.meta.url)) |
| 64 | + const path: string = pathe.fileURLToPath(id) |
| 65 | + |
| 66 | + // Act + Expect |
| 67 | + expect(subject.getDirectories(id)).to.eql(ts.sys.getDirectories(path)) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('readFile', () => { |
| 72 | + it('should return `undefined` if file does not exist at `id`', () => { |
| 73 | + // Arrange |
| 74 | + const id: string = 'src/index.cts' |
| 75 | + |
| 76 | + // Act + Expect |
| 77 | + expect(subject.readFile(id)).to.eq(ts.sys.readFile(id)).and.be.undefined |
| 78 | + }) |
| 79 | + |
| 80 | + it.each<Parameters<ModuleResolutionHost['readFile']>>([ |
| 81 | + ['src/index.mts'], |
| 82 | + [import.meta.url] |
| 83 | + ])('should return contents of file at `id` (%j)', id => { |
| 84 | + // Arrange |
| 85 | + const path: string = pathe.toPath(id) |
| 86 | + |
| 87 | + // Act |
| 88 | + const result = subject.readFile(id) |
| 89 | + |
| 90 | + // Expect |
| 91 | + expect(result).to.eq(ts.sys.readFile(path)).and.not.be.undefined |
| 92 | + }) |
| 93 | + }) |
| 94 | + |
| 95 | + describe('realpath', () => { |
| 96 | + it.each<Parameters<ModuleResolutionHost['realpath']>>([ |
| 97 | + ['src/lib/..'], |
| 98 | + [pathe.dirname(import.meta.url) + pathe.sep + pathe.dot.repeat(2)] |
| 99 | + ])('should return canonical pathname of `id` (%j)', id => { |
| 100 | + // Arrange |
| 101 | + const path: string = pathe.toPath(id) |
| 102 | + |
| 103 | + // Act |
| 104 | + const result = subject.realpath(id) |
| 105 | + |
| 106 | + // Expect |
| 107 | + expect(result).to.eq(ts.sys.realpath!(path)).and.not.be.undefined |
| 108 | + }) |
| 109 | + }) |
| 110 | + }) |
| 111 | +}) |
0 commit comments