Skip to content

Commit

Permalink
feat: use simpler reflect-metadata implementation (#315)
Browse files Browse the repository at this point in the history
* remove core-js dependency in preset
* remove core-js import tests
* add small reflect metadata lib
* add test for relfect metadata lib
* update typescript version in preset to current
  • Loading branch information
wtho authored and thymikee committed Oct 18, 2019
1 parent 20ba6fd commit 12b0bfa
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 122 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
* (**BREAKING**): Refine ast-transformer behavior: only transform `styles`-assignments inside @Component ([#261](https://github.com/thymikee/jest-preset-angular/pull/261)) and TypeScript v2.9 `createStringLiteral` is polyfilled if an older version is used ([#272](https://github.com/thymikee/jest-preset-angular/issues/272)).
* (**BREAKING**): Restructure project with `src` and `build` folder ([#307](https://github.com/thymikee/jest-preset-angular/pull/307)).
* (**BREAKING**): Support `tsconfig.spec.json` in root folder by default ([#309](https://github.com/thymikee/jest-preset-angular/pull/309)).
* (**BREAKING**): Remove `core-js` peer dependency and instead add it as direct dependency ([#311](https://github.com/thymikee/jest-preset-angular/pull/309)).
* Remove `core-js` dependency by using internal, minimal reflect-metadata ([#315](https://github.com/thymikee/jest-preset-angular/pull/315)).

#### Chore && Maintenance
* Update example app to match Angular 8 Boilerplate ([#311](https://github.com/thymikee/jest-preset-angular/pull/309)).
* Update example app to match Angular 8 Boilerplate ([#311](https://github.com/thymikee/jest-preset-angular/pull/311)).
* Update example app to not use karma, extract jest config, fix paths ([#316](https://github.com/thymikee/jest-preset-angular/pull/316))

#### Migration Guide
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@ describe('Component snapshots', () => {

Problems may arise if you're using custom builds (this preset is tailored for `angular-cli` as firstly priority). Please be advised that every entry in default configuration may be overridden to best suite your app's needs.

### Can't resolve all parameters for SomeClass(?)

This is related to Angular's reflection and also depends on a reflection library, as e. g. included in `core-js`. We use our own minimal reflection that satisfy Angular's current requirements, but in case these change, you can install `core-js` and import the reflection library in your `setupJest.ts`:
```typescript
require('core-js/es/reflect');
require('core-js/proposals/reflect-metadata');
```
Note that this might also be related to other issues with the dependency injection and parameter type reflection.

### @Input() bindings are not reflected into fixture when `ChangeDetectionStrategy.OnPush` is used

This issue is not related to Jest, [it's a known Angular bug](https://github.com/angular/angular/issues/12313)
Expand Down
3 changes: 1 addition & 2 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2437,7 +2437,7 @@ core-js-compat@^3.1.1:
browserslist "^4.6.6"
semver "^6.3.0"

core-js@3.2.1, "core-js@>=2.0.0 < 4.0.0":
core-js@3.2.1:
version "3.2.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.2.1.tgz#cd41f38534da6cc59f7db050fe67307de9868b09"
integrity sha512-Qa5XSVefSVPRxy2XfUC13WbvqkxhkwB3ve+pgCQveNgYzbM/UxZeu1dcOX/xr4UmfUd+muuvsaxilQzCyUurMw==
Expand Down Expand Up @@ -4519,7 +4519,6 @@ jest-mock@^24.0.0:
"jest-preset-angular@file:..":
version "7.1.1"
dependencies:
core-js ">=2.0.0 < 4.0.0"
jest-environment-jsdom-thirteen "^1.0.0"
pretty-format "^24.0.0"
ts-jest "^24.0.0"
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
"author": "Michał Pierzchała <thymikee@gmail.com>",
"license": "MIT",
"dependencies": {
"core-js": ">=2.0.0 < 4.0.0",
"jest-environment-jsdom-thirteen": "^1.0.0",
"pretty-format": "^24.0.0",
"ts-jest": "^24.0.0"
Expand All @@ -17,7 +16,7 @@
"@types/jest": "^24.0.11",
"@types/node": "^11.11.5",
"jest": "^24.0.0",
"typescript": "^3.2.1"
"typescript": "^3.6.3"
},
"peerDependencies": {
"@angular/core": ">=2.0.0",
Expand Down
44 changes: 0 additions & 44 deletions src/__tests__/CoreJS-es6-reflect.test.ts

This file was deleted.

44 changes: 0 additions & 44 deletions src/__tests__/CoreJS-es7-reflect.test.ts

This file was deleted.

22 changes: 22 additions & 0 deletions src/__tests__/ReflectMetadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import '../reflectMetadata';

describe('using minimal reflect metadata', () => {

// to avoid type conflicts with global.Reflect comfortably
let reflect: any = Reflect;

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'});
const functionClass = function FunctionClass() {};

reflect.metadata('design:paramtypes', metadataValue)(functionClass);
const retrieved = reflect.getOwnMetadata('design:paramtypes', functionClass);
expect(retrieved).toBe(metadataValue);
expect(retrieved().test).toBe('object');
});
});
23 changes: 23 additions & 0 deletions src/reflectMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const METADATA_KEY_PARAMTYPES = "design:paramtypes";
const CTOR_PARAMETERS_JPA = "ctorParametersJPA";

// weird workaround to avoid 'ReferenceError: globalThis is not defined' in node version < 11
(global as any).globalThis = (global as any).globalThis || undefined

const _global = globalThis || global; // globalThis available since node v12/TS v3.4
const reflect: any = _global["Reflect"]; // reflect type in global has not these methods

// 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: any, metadataValue: any) => (target: any, key: any) => {
if (metadataKey === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor
target[CTOR_PARAMETERS_JPA] = metadataValue;
}
}
reflect.getOwnMetadata = (metadata: any, target: any, key: any) => {
if (metadata === METADATA_KEY_PARAMTYPES && key === undefined) { // key undefined is ctor
return target[CTOR_PARAMETERS_JPA];
}
}
}
20 changes: 1 addition & 19 deletions src/setupJest.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
'use strict';

try {
require('core-js/es6/reflect');
} catch (e) {
try {
require('core-js/es/reflect');
} catch(e) {
throw new Error('core-js es6-reflect not found!');
}
}

try {
require('core-js/es7/reflect');
} catch (e) {
try {
require('core-js/proposals/reflect-metadata');
} catch (e) {
throw new Error('core-js es7-reflect not found!');
}
}
require('./reflectMetadata');

require('zone.js/dist/zone.js');
require('zone.js/dist/proxy.js');
Expand Down
13 changes: 4 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -603,11 +603,6 @@ copy-descriptor@^0.1.0:
resolved "https://registry.yarnpkg.com/copy-descriptor/-/copy-descriptor-0.1.1.tgz#676f6eb3c39997c2ee1ac3a924fd6124748f578d"
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=

"core-js@>=2.0.0 < 4.0.0":
version "3.2.1"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.2.1.tgz#cd41f38534da6cc59f7db050fe67307de9868b09"
integrity sha512-Qa5XSVefSVPRxy2XfUC13WbvqkxhkwB3ve+pgCQveNgYzbM/UxZeu1dcOX/xr4UmfUd+muuvsaxilQzCyUurMw==

core-util-is@1.0.2, core-util-is@~1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
Expand Down Expand Up @@ -3299,10 +3294,10 @@ type-check@~0.3.2:
dependencies:
prelude-ls "~1.1.2"

typescript@^3.2.1:
version "3.3.3333"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.3.3333.tgz#171b2c5af66c59e9431199117a3bcadc66fdcfd6"
integrity sha512-JjSKsAfuHBE/fB2oZ8NxtRTk5iGcg6hkYXMnZ3Wc+b2RSqejEqTaem11mHASMnFilHrax3sLK0GDzcJrekZYLw==
typescript@^3.6.3:
version "3.6.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.6.3.tgz#fea942fabb20f7e1ca7164ff626f1a9f3f70b4da"
integrity sha512-N7bceJL1CtRQ2RiG0AQME13ksR7DiuQh/QehubYcghzv20tnh+MQnQIuJddTmsbqYj+dztchykemz0zFzlvdQw==

uglify-js@^3.1.4:
version "3.6.0"
Expand Down

0 comments on commit 12b0bfa

Please sign in to comment.