Skip to content

Commit

Permalink
fix: throw a human readable error during resolve
Browse files Browse the repository at this point in the history
closes #133
  • Loading branch information
satanTime committed May 30, 2020
1 parent 3dd4357 commit 284e848
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 7 deletions.
10 changes: 9 additions & 1 deletion lib/mock-component/mock-component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,15 @@ export function MockComponent<TComponent>(
return cacheHit as Type<MockedComponent<TComponent>>;
}

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 = `<ng-content></ng-content>`;
const viewChildRefs = new Map<string, string>();
Expand Down
11 changes: 10 additions & 1 deletion lib/mock-directive/mock-directive.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -34,7 +35,15 @@ export function MockDirective<TDirective>(directive: Type<TDirective>): Type<Moc
return cacheHit as Type<MockedDirective<TDirective>>;
}

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,
Expand Down
23 changes: 21 additions & 2 deletions lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion lib/mock-module/mock-module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -121,7 +122,17 @@ const NEVER_MOCK: Array<Type<any>> = [CommonModule];

function MockIt(module: Type<any>): 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<any>) => {
Expand Down
12 changes: 11 additions & 1 deletion lib/mock-pipe/mock-pipe.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { core } from '@angular/compiler';
import { Pipe, PipeTransform, Type } from '@angular/core';

import { Mock, MockOf } from '../common';
Expand All @@ -14,7 +15,16 @@ export function MockPipe<TPipe extends PipeTransform>(
pipe: Type<TPipe>,
transform: TPipe['transform'] = defaultTransform
): Type<MockedPipe<TPipe>> {
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,
Expand Down
12 changes: 11 additions & 1 deletion lib/mock-render/mock-render.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -75,7 +76,16 @@ function MockRender<MComponent, TComponent extends { [key: string]: any }>(
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) => {
Expand Down

0 comments on commit 284e848

Please sign in to comment.