|
6 | 6 | * |
7 | 7 | */ |
8 | 8 |
|
9 | | -import * as path from 'path'; |
10 | | -import {fileURLToPath, pathToFileURL} from 'url'; |
11 | 9 | import * as fs from 'graceful-fs'; |
12 | | -import {sync as resolveSync} from 'resolve'; |
13 | 10 | import {type IModuleMap, ModuleMap} from 'jest-haste-map'; |
| 11 | +import * as path from 'path'; |
| 12 | +import {sync as resolveSync} from 'unrs-resolver'; |
| 13 | +import {pathToFileURL} from 'url'; |
| 14 | + |
14 | 15 | import userResolver from '../__mocks__/userResolver'; |
15 | 16 | import userResolverAsync from '../__mocks__/userResolverAsync'; |
16 | | -import defaultResolver, {type PackageFilter} from '../defaultResolver'; |
| 17 | +import defaultResolver from '../defaultResolver'; |
17 | 18 | import nodeModulesPaths from '../nodeModulesPaths'; |
18 | 19 | import Resolver from '../resolver'; |
19 | 20 | import type {ResolverConfig} from '../types'; |
20 | 21 |
|
21 | 22 | jest.mock('../__mocks__/userResolver').mock('../__mocks__/userResolverAsync'); |
22 | 23 |
|
23 | | -// Do not fully mock `resolve` because it is used by Jest. Doing it will crash |
| 24 | +// Do not fully mock `unrs-resolver` because it is used by Jest. Doing it will crash |
24 | 25 | // in very strange ways. Instead, just spy on it and its `sync` method. |
25 | | -jest.mock('resolve', () => { |
| 26 | +jest.mock('unrs-resolver', () => { |
26 | 27 | const originalModule = |
27 | | - jest.requireActual<typeof import('resolve')>('resolve'); |
28 | | - |
29 | | - const m = jest.fn<typeof import('resolve')>((...args) => |
30 | | - originalModule(...args), |
31 | | - ); |
32 | | - Object.assign(m, originalModule); |
33 | | - m.sync = jest.spyOn(originalModule, 'sync'); |
34 | | - |
35 | | - return m; |
| 28 | + jest.requireActual<typeof import('unrs-resolver')>('unrs-resolver'); |
| 29 | + return { |
| 30 | + ...originalModule, |
| 31 | + sync: jest |
| 32 | + .spyOn(originalModule, 'sync') |
| 33 | + .mockImplementation((...args) => originalModule.sync(...args)), |
| 34 | + }; |
36 | 35 | }); |
37 | 36 |
|
38 | 37 | const mockUserResolver = jest.mocked(userResolver); |
@@ -134,25 +133,6 @@ describe('findNodeModule', () => { |
134 | 133 | }); |
135 | 134 | }); |
136 | 135 |
|
137 | | - it('wraps passed packageFilter to the resolve module when using the default resolver', () => { |
138 | | - const packageFilter = jest.fn<PackageFilter>(); |
139 | | - |
140 | | - // A resolver that delegates to defaultResolver with a packageFilter implementation |
141 | | - mockUserResolver.mockImplementation((request, opts) => |
142 | | - opts.defaultResolver(request, {...opts, packageFilter}), |
143 | | - ); |
144 | | - |
145 | | - Resolver.findNodeModule('./test', { |
146 | | - basedir: path.resolve(__dirname, '../__mocks__/'), |
147 | | - resolver: require.resolve('../__mocks__/userResolver'), |
148 | | - }); |
149 | | - |
150 | | - expect(packageFilter).toHaveBeenCalledWith( |
151 | | - expect.objectContaining({name: '__mocks__'}), |
152 | | - expect.any(String), |
153 | | - ); |
154 | | - }); |
155 | | - |
156 | 136 | it('supports file URLs', () => { |
157 | 137 | const path = pathToFileURL(__filename).href; |
158 | 138 | const newPath = Resolver.findNodeModule(path, { |
@@ -407,7 +387,9 @@ describe('findNodeModule', () => { |
407 | 387 | basedir: path.resolve(importsRoot, './foo-import/index.js'), |
408 | 388 | conditions: [], |
409 | 389 | }); |
410 | | - }).toThrow('Missing "#something-else" specifier in "foo-import" package'); |
| 390 | + }).toThrow( |
| 391 | + `Package import specifier "#something-else" is not defined in package ${path.join(importsRoot, 'foo-import/package.json')}`, |
| 392 | + ); |
411 | 393 | }); |
412 | 394 | }); |
413 | 395 | }); |
@@ -446,27 +428,6 @@ describe('findNodeModuleAsync', () => { |
446 | 428 | }); |
447 | 429 | }); |
448 | 430 |
|
449 | | - it('passes packageFilter to the resolve module when using the default resolver', async () => { |
450 | | - const packageFilter = jest.fn<PackageFilter>(); |
451 | | - |
452 | | - // A resolver that delegates to defaultResolver with a packageFilter implementation |
453 | | - mockUserResolverAsync.async.mockImplementation((request, opts) => |
454 | | - Promise.resolve(opts.defaultResolver(request, {...opts, packageFilter})), |
455 | | - ); |
456 | | - |
457 | | - await Resolver.findNodeModuleAsync('test', { |
458 | | - basedir: '/', |
459 | | - resolver: require.resolve('../__mocks__/userResolverAsync'), |
460 | | - }); |
461 | | - |
462 | | - expect(mockResolveSync).toHaveBeenCalledWith( |
463 | | - 'test', |
464 | | - expect.objectContaining({ |
465 | | - packageFilter, |
466 | | - }), |
467 | | - ); |
468 | | - }); |
469 | | - |
470 | 431 | it('supports file URLs', async () => { |
471 | 432 | const path = pathToFileURL(__filename).href; |
472 | 433 | const newPath = await Resolver.findNodeModuleAsync(path, { |
|
0 commit comments