From 5a367293ba490d7d717963b099db7531c347d1f4 Mon Sep 17 00:00:00 2001 From: Ahn <27772165+ahnpnl@users.noreply.github.com> Date: Wed, 27 Apr 2022 11:57:21 +0200 Subject: [PATCH] feat: remove `reflect-metadata` in `setup-jest` (#1428) BREAKING CHANGE Since now we are using Angular AST transformers, `reflect-metadata` is not needed anymore --- src/config/setup-jest.ts | 2 -- src/utils/reflect-metadata.spec.ts | 23 ------------------ src/utils/reflect-metadata.ts | 39 ------------------------------ 3 files changed, 64 deletions(-) delete mode 100644 src/utils/reflect-metadata.spec.ts delete mode 100644 src/utils/reflect-metadata.ts diff --git a/src/config/setup-jest.ts b/src/config/setup-jest.ts index 3c58bcdb0c..8deb84e5c8 100644 --- a/src/config/setup-jest.ts +++ b/src/config/setup-jest.ts @@ -1,5 +1,3 @@ -require('../utils/reflect-metadata'); - !process.execArgv.includes('--experimental-vm-modules') ? require('zone.js/bundles/zone-testing-bundle.umd.js') : require('zone.js/fesm2015/zone-testing-bundle.min.js'); diff --git a/src/utils/reflect-metadata.spec.ts b/src/utils/reflect-metadata.spec.ts deleted file mode 100644 index b00e4de6c3..0000000000 --- a/src/utils/reflect-metadata.spec.ts +++ /dev/null @@ -1,23 +0,0 @@ -import './reflect-metadata'; - -describe('using minimal reflect metadata', () => { - // to avoid type conflicts with global.Reflect comfortably - const reflect: any = Reflect; // eslint-disable-line @typescript-eslint/no-explicit-any - - it(`should make metadata() and getOwnMetadata() available on global.Reflect`, () => { - expect(typeof reflect.metadata).toBe('function'); - expect(typeof reflect.getOwnMetadata).toBe('function'); - }); - - it('should set and retrieve metadata using Reflect.metadata and Reflect.getOwnMetadata', () => { - const metadataValue = () => ({ test: 'object' }); - // eslint-disable-next-line @typescript-eslint/no-empty-function - const functionClass = function FunctionClass() {}; - - reflect.metadata('design:paramtypes', metadataValue)(functionClass); - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment - const retrieved = reflect.getOwnMetadata('design:paramtypes', functionClass); - expect(retrieved).toBe(metadataValue); - expect(retrieved().test).toBe('object'); - }); -}); diff --git a/src/utils/reflect-metadata.ts b/src/utils/reflect-metadata.ts deleted file mode 100644 index a727b6222a..0000000000 --- a/src/utils/reflect-metadata.ts +++ /dev/null @@ -1,39 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-namespace, @typescript-eslint/no-unused-vars -declare namespace Reflect { - // here we extend the global Reflect definition by these two functions used in Angular - function metadata( - metadataKey: string, - metadataValue: unknown, - ): (target: Record, key: string | undefined) => void; - function getOwnMetadata(metadata: unknown, target: Record, key: string | undefined): unknown; -} - -const METADATA_KEY_PARAMTYPES = 'design:paramtypes'; - -// weird workaround to avoid 'ReferenceError: globalThis is not defined' in node version < 11 -// eslint-disable-next-line @typescript-eslint/no-explicit-any -(global as any).globalThis = global.globalThis || undefined; - -const globalRef = globalThis || global; // globalThis available since node v12/TS v3.4 -const reflect = globalRef['Reflect']; // reflect type in global has not these methods - -const metadataValueStore = new WeakMap, unknown>(); - -// let's not blindly override, maybe there is already a reflect lib in use -// but just overriding one of the two functions does not serve any purpose -if (!reflect.metadata && !reflect.getOwnMetadata) { - reflect.metadata = - (metadataKey: string, metadataValue: unknown) => (target: Record, key: string | undefined) => { - if (metadataKey === METADATA_KEY_PARAMTYPES && key === undefined) { - // key undefined is ctor - metadataValueStore.set(target, metadataValue); - } - }; - reflect.getOwnMetadata = (metadata: unknown, target: Record, key: string | undefined) => { - if (metadata === METADATA_KEY_PARAMTYPES && key === undefined && metadataValueStore.has(target)) { - // key undefined is ctor - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - return metadataValueStore.get(target); - } - }; -}