From 284e8489a763980f033ca5f78954f5baed4ba8ff Mon Sep 17 00:00:00 2001 From: MG Date: Sat, 30 May 2020 14:43:35 +0200 Subject: [PATCH] fix: throw a human readable error during resolve closes #133 --- lib/mock-component/mock-component.ts | 10 +++++++++- lib/mock-directive/mock-directive.ts | 11 ++++++++++- lib/mock-helper/mock-helper.ts | 23 +++++++++++++++++++++-- lib/mock-module/mock-module.ts | 13 ++++++++++++- lib/mock-pipe/mock-pipe.ts | 12 +++++++++++- lib/mock-render/mock-render.ts | 12 +++++++++++- 6 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/mock-component/mock-component.ts b/lib/mock-component/mock-component.ts index e1686d1b32..c39b9017df 100644 --- a/lib/mock-component/mock-component.ts +++ b/lib/mock-component/mock-component.ts @@ -39,7 +39,15 @@ export function MockComponent( return cacheHit as Type>; } - const { exportAs, inputs, outputs, queries, selector } = metaData || directiveResolver.resolve(component); + let meta: core.Directive | undefined = metaData; + if (!meta) { + try { + meta = directiveResolver.resolve(component); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + const { exportAs, inputs, outputs, queries, selector } = meta; let template = ``; const viewChildRefs = new Map(); diff --git a/lib/mock-directive/mock-directive.ts b/lib/mock-directive/mock-directive.ts index d02552288d..d41bd696bf 100644 --- a/lib/mock-directive/mock-directive.ts +++ b/lib/mock-directive/mock-directive.ts @@ -1,3 +1,4 @@ +import { core } from '@angular/compiler'; import { Directive, ElementRef, forwardRef, Optional, TemplateRef, Type, ViewContainerRef } from '@angular/core'; import { MockControlValueAccessor, MockOf } from '../common'; @@ -34,7 +35,15 @@ export function MockDirective(directive: Type): Type>; } - const { selector, exportAs, inputs, outputs, queries } = directiveResolver.resolve(directive); + let meta: core.Directive | undefined; + if (!meta) { + try { + meta = directiveResolver.resolve(directive); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + const { selector, exportAs, inputs, outputs, queries } = meta; const options: Directive = { exportAs, diff --git a/lib/mock-helper/mock-helper.ts b/lib/mock-helper/mock-helper.ts index d252139b2e..6b173f6855 100644 --- a/lib/mock-helper/mock-helper.ts +++ b/lib/mock-helper/mock-helper.ts @@ -1,5 +1,6 @@ /* tslint:disable:variable-name unified-signatures */ +import { core } from '@angular/compiler'; import { EventEmitter, Type } from '@angular/core'; import { By } from '@angular/platform-browser'; @@ -196,7 +197,16 @@ export const ngMocks: { const notFoundValue: any = args.length === 3 ? args[2] : defaultNotFoundValue; for (const token of el.providerTokens) { - const { inputs } = directiveResolver.resolve(token); + let meta: core.Directive | undefined; + if (!meta) { + try { + meta = directiveResolver.resolve(token); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + + const { inputs } = meta; if (!inputs) { continue; } @@ -230,7 +240,16 @@ export const ngMocks: { const notFoundValue: any = args.length === 3 ? args[2] : defaultNotFoundValue; for (const token of el.providerTokens) { - const { outputs } = directiveResolver.resolve(token); + let meta: core.Directive | undefined; + if (!meta) { + try { + meta = directiveResolver.resolve(token); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + + const { outputs } = meta; if (!outputs) { continue; } diff --git a/lib/mock-module/mock-module.ts b/lib/mock-module/mock-module.ts index 19ed2b0b60..8fd28383cd 100644 --- a/lib/mock-module/mock-module.ts +++ b/lib/mock-module/mock-module.ts @@ -1,4 +1,5 @@ import { CommonModule } from '@angular/common'; +import { core } from '@angular/compiler'; import { ModuleWithProviders, NgModule, Provider, Type } from '@angular/core'; import { Mock, MockOf } from '../common'; @@ -121,7 +122,17 @@ const NEVER_MOCK: Array> = [CommonModule]; function MockIt(module: Type): NgModule { const mockedModule: NgModule = {}; - const { declarations = [], entryComponents = [], imports = [], providers = [] } = ngModuleResolver.resolve(module); + + let meta: core.NgModule | undefined; + if (!meta) { + try { + meta = ngModuleResolver.resolve(module); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + + const { declarations = [], entryComponents = [], imports = [], providers = [] } = meta; if (imports.length) { mockedModule.imports = flatten(imports).map((instance: Type) => { diff --git a/lib/mock-pipe/mock-pipe.ts b/lib/mock-pipe/mock-pipe.ts index 526c93566e..b5ccc3bb56 100644 --- a/lib/mock-pipe/mock-pipe.ts +++ b/lib/mock-pipe/mock-pipe.ts @@ -1,3 +1,4 @@ +import { core } from '@angular/compiler'; import { Pipe, PipeTransform, Type } from '@angular/core'; import { Mock, MockOf } from '../common'; @@ -14,7 +15,16 @@ export function MockPipe( pipe: Type, transform: TPipe['transform'] = defaultTransform ): Type> { - const { name } = pipeResolver.resolve(pipe); + let meta: core.Pipe | undefined; + if (!meta) { + try { + meta = pipeResolver.resolve(pipe); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + + const { name } = meta; const options: Pipe = { name, diff --git a/lib/mock-render/mock-render.ts b/lib/mock-render/mock-render.ts index 9f441abf8e..0a64c74c48 100644 --- a/lib/mock-render/mock-render.ts +++ b/lib/mock-render/mock-render.ts @@ -1,5 +1,6 @@ // tslint:disable:unified-signatures +import { core } from '@angular/compiler'; import { Component, DebugElement, DebugNode, Provider, Type } from '@angular/core'; import { ComponentFixture, getTestBed, TestBed } from '@angular/core/testing'; @@ -75,7 +76,16 @@ function MockRender( if (typeof template === 'string') { mockedTemplate = template; } else { - const { inputs, outputs, selector } = directiveResolver.resolve(template); + let meta: core.Directive | undefined; + if (!meta) { + try { + meta = directiveResolver.resolve(template); + } catch (e) { + throw new Error('ng-mocks is not in JIT mode and cannot resolve declarations'); + } + } + + const { inputs, outputs, selector } = meta; mockedTemplate += `<${selector}`; if (inputs) { inputs.forEach((definition: string) => {