|  | 
| 7 | 7 |  */ | 
| 8 | 8 | 
 | 
| 9 | 9 | import * as path from 'path'; | 
| 10 |  | -import {fileURLToPath, pathToFileURL} from 'url'; | 
|  | 10 | +import {pathToFileURL} from 'url'; | 
| 11 | 11 | import * as fs from 'graceful-fs'; | 
| 12 |  | -import {sync as resolveSync} from 'resolve'; | 
| 13 | 12 | import {type IModuleMap, ModuleMap} from 'jest-haste-map'; | 
| 14 | 13 | import userResolver from '../__mocks__/userResolver'; | 
| 15 | 14 | import userResolverAsync from '../__mocks__/userResolverAsync'; | 
| 16 |  | -import defaultResolver, {type PackageFilter} from '../defaultResolver'; | 
|  | 15 | +import defaultResolver from '../defaultResolver'; | 
| 17 | 16 | import nodeModulesPaths from '../nodeModulesPaths'; | 
| 18 | 17 | import Resolver from '../resolver'; | 
| 19 | 18 | import type {ResolverConfig} from '../types'; | 
|  | 19 | +import type {MockInstance} from 'jest-mock'; | 
| 20 | 20 | 
 | 
| 21 | 21 | jest.mock('../__mocks__/userResolver').mock('../__mocks__/userResolverAsync'); | 
| 22 | 22 | 
 | 
| 23 |  | -// Do not fully mock `resolve` because it is used by Jest. Doing it will crash | 
| 24 |  | -// in very strange ways. Instead, just spy on it and its `sync` method. | 
| 25 |  | -jest.mock('resolve', () => { | 
| 26 |  | -  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'); | 
|  | 23 | +let mockResolveSync: MockInstance< | 
|  | 24 | +  import('unrs-resolver').ResolverFactory['sync'] | 
|  | 25 | +>; | 
| 34 | 26 | 
 | 
|  | 27 | +// Do not fully mock `unrs-resolver` because it is used by Jest. Doing it will crash | 
|  | 28 | +// in very strange ways. Instead, just spy on it and its `sync` method. | 
|  | 29 | +jest.mock('unrs-resolver', () => { | 
|  | 30 | +  const originalResolverFactory = | 
|  | 31 | +    jest.requireActual<typeof import('unrs-resolver')>( | 
|  | 32 | +      'unrs-resolver', | 
|  | 33 | +    ).ResolverFactory; | 
|  | 34 | + | 
|  | 35 | +  const m = jest.fn<import('unrs-resolver').ResolverFactory['sync']>(); | 
|  | 36 | +  mockResolveSync = jest.spyOn(originalResolverFactory.prototype, 'sync'); | 
| 35 | 37 |   return m; | 
| 36 | 38 | }); | 
| 37 | 39 | 
 | 
| 38 | 40 | const mockUserResolver = jest.mocked(userResolver); | 
| 39 | 41 | const mockUserResolverAsync = jest.mocked(userResolverAsync); | 
| 40 |  | -const mockResolveSync = jest.mocked(resolveSync); | 
| 41 | 42 | 
 | 
| 42 | 43 | beforeEach(() => { | 
| 43 | 44 |   mockUserResolver.mockClear(); | 
| @@ -134,25 +135,6 @@ describe('findNodeModule', () => { | 
| 134 | 135 |     }); | 
| 135 | 136 |   }); | 
| 136 | 137 | 
 | 
| 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 | 138 |   it('supports file URLs', () => { | 
| 157 | 139 |     const path = pathToFileURL(__filename).href; | 
| 158 | 140 |     const newPath = Resolver.findNodeModule(path, { | 
| @@ -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