|
| 1 | +/** |
| 2 | + * @file Unit Tests - resolveAlias |
| 3 | + * @module mlly/lib/tests/resolveAlias/unit |
| 4 | + */ |
| 5 | + |
| 6 | +import type { AliasResolverOptions as Options } from '#src/interfaces' |
| 7 | +import path from 'node:path' |
| 8 | +import testSubject from '../resolve-alias' |
| 9 | + |
| 10 | +describe('unit:lib/resolveAlias', () => { |
| 11 | + const paths: NonNullable<Options['paths']> = { |
| 12 | + '#fixtures/*': ['__fixtures__/*'], |
| 13 | + '#src/*': ['src/*'], |
| 14 | + '@flex-development/tutils/*': [ |
| 15 | + 'node_modules/@flex-development/tutils/dist/*' |
| 16 | + ] |
| 17 | + } |
| 18 | + |
| 19 | + it('should convert alias from unknown source to absolute specifier', () => { |
| 20 | + // Act |
| 21 | + const result = testSubject('#src/lib/detect-syntax', { paths }) |
| 22 | + |
| 23 | + // Expect |
| 24 | + expect(result).to.equal(path.join(process.cwd(), 'src/lib/detect-syntax')) |
| 25 | + }) |
| 26 | + |
| 27 | + it('should convert current directory alias to relative specifier', () => { |
| 28 | + // Arrange |
| 29 | + const specifier = '#src/constants' |
| 30 | + const url = path.resolve('src/index.ts') |
| 31 | + |
| 32 | + // Act + Expect |
| 33 | + expect(testSubject(specifier, { paths, url })).to.equal('./constants') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should convert module alias to relative specifier', () => { |
| 37 | + // Arrange |
| 38 | + const expected = '../../../__fixtures__/aggregate-error-ponyfill.cjs' |
| 39 | + const specifier = '#fixtures/aggregate-error-ponyfill.cjs' |
| 40 | + const url = path.resolve('src/lib/__tests__/detect-syntax.spec.ts') |
| 41 | + |
| 42 | + // Act + Expect |
| 43 | + expect(testSubject(specifier, { paths, url })).to.equal(expected) |
| 44 | + }) |
| 45 | + |
| 46 | + it('should convert node_modules alias to bare specifier', () => { |
| 47 | + // Arrange |
| 48 | + const expected = '@flex-development/tutils/dist/guards/is-node-env' |
| 49 | + const specifier = '@flex-development/tutils/guards/is-node-env' |
| 50 | + |
| 51 | + // Act + Expect |
| 52 | + expect(testSubject(specifier, { paths })).to.equal(expected) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should convert parent directory alias to relative specifier', () => { |
| 56 | + // Arrange |
| 57 | + const url = path.resolve('src/interfaces/statement.ts') |
| 58 | + |
| 59 | + // Act + Expect |
| 60 | + expect(testSubject('#src/types', { paths, url })).to.equal('../types') |
| 61 | + }) |
| 62 | + |
| 63 | + it('should return specifier if path match was not found', () => { |
| 64 | + // Arrange |
| 65 | + const specifier: string = '#src/lib/resolve-alias' |
| 66 | + |
| 67 | + // Act |
| 68 | + const results = testSubject(specifier, { paths: {} }) |
| 69 | + |
| 70 | + // Expect |
| 71 | + expect(results).to.equal(specifier) |
| 72 | + }) |
| 73 | +}) |
0 commit comments