Skip to content

Commit

Permalink
fix: building es5 only that supports es2015
Browse files Browse the repository at this point in the history
closes #158
  • Loading branch information
satanTime committed Jul 10, 2020
1 parent 55a0031 commit bc53de5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
27 changes: 22 additions & 5 deletions lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -130,11 +130,28 @@ export function MockModule(module: any): any {
if (mockModuleDef) {
const parent = ngMocksUniverse.flags.has('skipMock') ? ngModule : Mock;

@NgModule(mockModuleDef)
@MockOf(ngModule)
class ModuleMock extends parent {}
(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;

// that's dark magic to support es2015 classes in es5 build.
if (mockModule) {
(mockModule as any).parameters = jitReflector.parameters(parent);
}
NgModule(mockModuleDef)(mockModule as any);
MockOf(ngModule)(mockModule as any);

mockModule = ModuleMock;
if (ngMocksUniverse.flags.has('cacheModule')) {
ngMocksUniverse.cache.set(ngModule, mockModule);
}
Expand Down
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"lib": ["dom", "es7"],
"module": "commonjs",
"target": "es2015",
"target": "es5",
"noImplicitAny": true,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
Expand Down

0 comments on commit bc53de5

Please sign in to comment.