From d11ed5afc3fc89b80a47b3e54617c18af24b3043 Mon Sep 17 00:00:00 2001 From: MG Date: Sat, 11 Jul 2020 01:28:32 +0200 Subject: [PATCH] fix: building es5 only that supports es2015 closes #158 --- README.md | 2 +- lib/mock-module/mock-module.ts | 30 +++++++++++++++++++++++++----- package.json | 7 ++----- tsconfig.json | 2 +- 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index f053eafed7..264da0c1c2 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Tested on: - Angular 7 (Jasmine, Jest, es5, es2015) - Angular 8 (Jasmine, Jest, es5, es2015) - Angular 9 (Jasmine, Jest, Ivy, es5, es2015) -- Angular 10 (Jasmine, Jest, Ivy, es2015), es5 isn't supported, use `target: es2015` in `tsconfig.spec.json` +- Angular 10 (Jasmine, Jest, Ivy, es5, es2015) ## Why use this? diff --git a/lib/mock-module/mock-module.ts b/lib/mock-module/mock-module.ts index cc89f9450f..8873eaa3b4 100644 --- a/lib/mock-module/mock-module.ts +++ b/lib/mock-module/mock-module.ts @@ -15,7 +15,7 @@ import { Type, } from '../common'; import { ngMocksUniverse } from '../common/ng-mocks-universe'; -import { ngModuleResolver } from '../common/reflect'; +import { jitReflector, ngModuleResolver } from '../common/reflect'; import { MockComponent } from '../mock-component'; import { MockDirective } from '../mock-directive'; import { MockPipe } from '../mock-pipe'; @@ -130,11 +130,31 @@ export function MockModule(module: any): any { if (mockModuleDef) { const parent = ngMocksUniverse.flags.has('skipMock') ? ngModule : Mock; - @NgModule(mockModuleDef) - @MockOf(ngModule) - class ModuleMock extends parent {} + // first we try to eval es2015 style and if it fails to use es5 transpilation in the catch block. + (window as any).ngMocksParent = parent; + try { + // tslint:disable-next-line:no-eval + eval(` + class mockModule extends window.ngMocksParent { + } + window.ngMocksResult = mockModule + `); + mockModule = (window as any).ngMocksResult; + } catch (e) { + class ClassEs5 extends parent {} + mockModule = ClassEs5; + } + (window as any).ngMocksParent = undefined; + + // the next step is to respect constructor parameters as the parent class. + if (mockModule) { + (mockModule as any).parameters = jitReflector.parameters(parent); + } + + // the last thing is to apply decorators. + NgModule(mockModuleDef)(mockModule as any); + MockOf(ngModule)(mockModule as any); - mockModule = ModuleMock; if (ngMocksUniverse.flags.has('cacheModule')) { ngMocksUniverse.cache.set(ngModule, mockModule); } diff --git a/package.json b/package.json index 33c355cd7a..f446a369a3 100644 --- a/package.json +++ b/package.json @@ -3,9 +3,6 @@ "version": "10.0.0", "description": "Functions for creating angular mocks", "main": "dist/index.js", - "module": "dist/es5/index.js", - "es5": "dist/es5/index.js", - "es2015": "dist/index.js", "types": "dist/index.d.ts", "files": [ "CHANGELOG", @@ -14,7 +11,7 @@ "README" ], "scripts": { - "build": "npm run clean && tsc && tsc -t es5 --outDir dist/es5", + "build": "npm run clean && tsc", "build:all": "npm run lint && npm run build && npm run test", "i:a": "npm run i:a5 & npm run i:a6 & npm run i:a7 & npm run i:a8 & npm run i:a9 & npm run i:a10 & wait", "i:a5": "npm run i:a5es5 && npm run i:a5es2015", @@ -138,7 +135,7 @@ "test:a9es5noivy": "cd e2e/a9es5noivy && npm run test", "test:a9es2015ivy": "cd e2e/a9es2015ivy && npm run test", "test:a9es2015noivy": "cd e2e/a9es2015noivy && npm run test", - "test:a10": "npm run test:a10es2015", + "test:a10": "npm run test:a10es5 && npm run test:a10es2015", "test:a10es5": "npm run test:a10es5ivy && npm run test:a10es5noivy", "test:a10es2015": "npm run test:a10es2015ivy && npm run test:a10es2015noivy", "test:a10es5ivy": "cd e2e/a10es5ivy && npm run test", diff --git a/tsconfig.json b/tsconfig.json index 8684cc6d2e..80b35857df 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,7 +2,7 @@ "compilerOptions": { "lib": ["dom", "es7"], "module": "commonjs", - "target": "es2015", + "target": "es5", "noImplicitAny": true, "moduleResolution": "node", "emitDecoratorMetadata": true,