|
| 1 | +import {pathsToModuleNameMapper} from '../paths-to-module-name-mapper' |
| 2 | + |
| 3 | +const tsconfigMap = { |
| 4 | + log: ['src/utils/log'], |
| 5 | + server: ['src/server'], |
| 6 | + client: ['src/client', 'src/client/index'], |
| 7 | + 'util/*': ['src/utils/*'], |
| 8 | + 'api/*': ['src/api/*'], |
| 9 | + 'test/*': ['test/*'], |
| 10 | + 'mocks/*': ['test/mocks/*'], |
| 11 | + 'test/*/mock': ['test/mocks/*', 'test/__mocks__/*'], |
| 12 | + '@foo-bar/common': ['../common/dist/library'], |
| 13 | + '@pkg/*': ['./packages/*'], |
| 14 | +} |
| 15 | + |
| 16 | +describe('pathsToModuleNameMapper', () => { |
| 17 | + test('should convert tsconfig mapping with no given prefix', () => { |
| 18 | + expect(pathsToModuleNameMapper(tsconfigMap)).toMatchInlineSnapshot(` |
| 19 | + Object { |
| 20 | + "^@foo\\\\-bar/common$": "../common/dist/library", |
| 21 | + "^@pkg/(.*)$": "./packages/$1", |
| 22 | + "^api/(.*)$": "src/api/$1", |
| 23 | + "^client$": Array [ |
| 24 | + "src/client", |
| 25 | + "src/client/index", |
| 26 | + ], |
| 27 | + "^log$": "src/utils/log", |
| 28 | + "^mocks/(.*)$": "test/mocks/$1", |
| 29 | + "^server$": "src/server", |
| 30 | + "^test/(.*)$": "test/$1", |
| 31 | + "^test/(.*)/mock$": Array [ |
| 32 | + "test/mocks/$1", |
| 33 | + "test/__mocks__/$1", |
| 34 | + ], |
| 35 | + "^util/(.*)$": "src/utils/$1", |
| 36 | + } |
| 37 | + `) |
| 38 | + }) |
| 39 | + |
| 40 | + test('should add `js` extension to resolved config with useESM: true', () => { |
| 41 | + expect(pathsToModuleNameMapper(tsconfigMap, {useESM: true})).toEqual({ |
| 42 | + /** |
| 43 | + * Why not using snapshot here? |
| 44 | + * Because the snapshot does not keep the property order, which is important for jest. |
| 45 | + * A pattern ending with `\\.js` should appear before another pattern without the extension does. |
| 46 | + */ |
| 47 | + '^log$': 'src/utils/log', |
| 48 | + '^server$': 'src/server', |
| 49 | + '^client$': ['src/client', 'src/client/index'], |
| 50 | + '^util/(.*)\\.js$': 'src/utils/$1', |
| 51 | + '^util/(.*)$': 'src/utils/$1', |
| 52 | + '^api/(.*)\\.js$': 'src/api/$1', |
| 53 | + '^api/(.*)$': 'src/api/$1', |
| 54 | + '^test/(.*)\\.js$': 'test/$1', |
| 55 | + '^test/(.*)$': 'test/$1', |
| 56 | + '^mocks/(.*)\\.js$': 'test/mocks/$1', |
| 57 | + '^mocks/(.*)$': 'test/mocks/$1', |
| 58 | + '^test/(.*)/mock\\.js$': ['test/mocks/$1', 'test/__mocks__/$1'], |
| 59 | + '^test/(.*)/mock$': ['test/mocks/$1', 'test/__mocks__/$1'], |
| 60 | + '^@foo\\-bar/common$': '../common/dist/library', |
| 61 | + '^@pkg/(.*)\\.js$': './packages/$1', |
| 62 | + '^@pkg/(.*)$': './packages/$1', |
| 63 | + '^(\\.{1,2}/.*)\\.js$': '$1', |
| 64 | + }) |
| 65 | + }) |
| 66 | + |
| 67 | + test.each(['<rootDir>/', 'foo'])( |
| 68 | + 'should convert tsconfig mapping with given prefix', |
| 69 | + prefix => { |
| 70 | + expect(pathsToModuleNameMapper(tsconfigMap, {prefix})).toMatchSnapshot( |
| 71 | + prefix, |
| 72 | + ) |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + describe('warnings', () => { |
| 77 | + beforeEach(() => { |
| 78 | + jest.spyOn(console, 'warn').mockImplementation() |
| 79 | + }) |
| 80 | + |
| 81 | + afterEach(() => jest.mocked(console.warn).mockRestore()) |
| 82 | + |
| 83 | + test('should warn about mapping it cannot handle', () => { |
| 84 | + expect( |
| 85 | + pathsToModuleNameMapper({ |
| 86 | + kept: ['src/kept'], |
| 87 | + 'no-target': [], |
| 88 | + 'too/*/many/*/stars': ['to/*/many/*/stars'], |
| 89 | + }), |
| 90 | + ).toMatchInlineSnapshot(` |
| 91 | + Object { |
| 92 | + "^kept$": "src/kept", |
| 93 | + } |
| 94 | + `) |
| 95 | + |
| 96 | + expect(jest.mocked(console.warn)).toHaveBeenCalledWith( |
| 97 | + 'Not mapping "no-target" because it has no target.', |
| 98 | + ) |
| 99 | + expect(jest.mocked(console.warn)).toHaveBeenCalledWith( |
| 100 | + 'Not mapping "too/*/many/*/stars" because it has more than one star (`*`).', |
| 101 | + ) |
| 102 | + }) |
| 103 | + }) |
| 104 | +}) |
0 commit comments