From 84fa5b4b423fec810a4fd677d7143e04719c54c8 Mon Sep 17 00:00:00 2001 From: Ahn <27772165+ahnpnl@users.noreply.github.com> Date: Sat, 30 Apr 2022 12:24:35 +0200 Subject: [PATCH] refactor(resolver): use ts types from `jest-resolve` (#1439) --- jest.config.js | 1 - src/resolvers/ng-jest-resolver.spec.ts | 4 +++- src/resolvers/ng-jest-resolver.ts | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/jest.config.js b/jest.config.js index 83a43cec14..aa6cc6a9eb 100644 --- a/jest.config.js +++ b/jest.config.js @@ -11,7 +11,6 @@ module.exports = { }, testEnvironment: 'jsdom', modulePathIgnorePatterns: ['examples/.*', 'website/.*'], - resolver: '/build/resolvers/ng-jest-resolver', snapshotSerializers: [require.resolve('jest-snapshot-serializer-raw')], testPathIgnorePatterns: ['/node_modules/', '/examples/', '/e2e/.*/__tests__', '\\.snap$'], transform: { diff --git a/src/resolvers/ng-jest-resolver.spec.ts b/src/resolvers/ng-jest-resolver.spec.ts index 8d218ae546..1063858f32 100644 --- a/src/resolvers/ng-jest-resolver.spec.ts +++ b/src/resolvers/ng-jest-resolver.spec.ts @@ -1,3 +1,5 @@ +import type { ResolverOptions } from 'jest-resolve'; + import ngJestResolver from './ng-jest-resolver'; test.each([ @@ -9,7 +11,7 @@ test.each([ ngJestResolver('foo', { defaultResolver: mockedDefaultResolver, - }); + } as unknown as ResolverOptions); expect(mockedDefaultResolver).toHaveBeenCalled(); expect(mockedDefaultResolver.mock.calls[0][0]).toEqual('foo'); diff --git a/src/resolvers/ng-jest-resolver.ts b/src/resolvers/ng-jest-resolver.ts index 2b65470279..d0b3511515 100644 --- a/src/resolvers/ng-jest-resolver.ts +++ b/src/resolvers/ng-jest-resolver.ts @@ -1,14 +1,15 @@ -const ngJestResolver = ( - path: string, - // `jest-resolve` didn't expose `ResolverOptions` type - options: { defaultResolver: (path: string, options: Record) => string }, -): string => - options.defaultResolver(path, { +import type { SyncResolver } from 'jest-resolve'; + +const ngJestResolver: SyncResolver = (path, options) => { + return options.defaultResolver(path, { ...options, - packageFilter: (pkg: Record) => ({ - ...pkg, - main: pkg.main || pkg.es2015 || pkg.module, - }), + packageFilter(pkg) { + return { + ...pkg, + main: pkg.main || pkg.es2015 || pkg.module, + }; + }, }); +}; export = ngJestResolver;