Skip to content

Commit

Permalink
Merge pull request #704 from satanTime/issues/702
Browse files Browse the repository at this point in the history
fix(core): building cjs and mjs #702
  • Loading branch information
satanTime authored Jun 16, 2021
2 parents f88e567 + f11c086 commit b0da1f1
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 101 deletions.
12 changes: 9 additions & 3 deletions libs/ng-mocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,18 @@
},
"homepage": "https://ng-mocks.sudo.eu",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "./cjs/index.js",
"types": "./cjs/index.d.ts",
"module": "./mjs/index.js",
"exports": {
"require": "./cjs/index.js",
"import": "./mjs/index.js"
},
"files": [
"cjs",
"examples",
"migrations.json",
"dist"
"mjs"
],
"ng-update": {
"migrations": "./migrations.json"
Expand Down
4 changes: 2 additions & 2 deletions libs/ng-mocks/src/lib/mock-helper/mock-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,12 +275,12 @@ export const ngMocks: {
/**
* @see https://ng-mocks.sudo.eu/api/ngMocks/get
*/
get<T>(elSelector: DebugNodeSelector, directive: AnyType<T>): T;
get<T>(elSelector: DebugNodeSelector, provider: AnyType<T> | InjectionToken<T>): T;

/**
* @see https://ng-mocks.sudo.eu/api/ngMocks/get
*/
get<T, D>(elSelector: DebugNodeSelector, directive: AnyType<T>, notFoundValue: D): D | T;
get<T, D>(elSelector: DebugNodeSelector, provider: AnyType<T> | InjectionToken<T>, notFoundValue: D): D | T;

/**
* @see https://ng-mocks.sudo.eu/api/ngMocks/globalExclude
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"scripts": {
"postinstall": "husky install",
"release": "export $(cat .env) && semantic-release",
"build": "npm run clean && tsc && cp CHANGELOG.md dist/libs/ng-mocks && cp README.md dist/libs/ng-mocks && cp LICENSE dist/libs/ng-mocks && cp libs/ng-mocks/package.json dist/libs/ng-mocks/package.json && cp libs/ng-mocks/migrations.json dist/libs/ng-mocks/migrations.json && cp -R examples dist/libs/ng-mocks",
"build": "npm run clean && npm run build:cjs && npm run build:mjs && cp CHANGELOG.md dist/libs/ng-mocks && cp README.md dist/libs/ng-mocks && cp LICENSE dist/libs/ng-mocks && cp libs/ng-mocks/package.json dist/libs/ng-mocks/package.json && cp libs/ng-mocks/migrations.json dist/libs/ng-mocks/migrations.json && cp -R examples dist/libs/ng-mocks",
"build:cjs": "tsc -p ./tsconfig.json",
"build:mjs": "tsc -p ./tsconfig.esm.json",
"build:all": "npm run lint && npm run build && npm run test",
"build:docs": "cd docs && CI=true npm run build",
"clean": "rm -rf dist test-reports tmp",
Expand Down
3 changes: 1 addition & 2 deletions tests-angular/e2e/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"module": "es2020",
"lib": ["es2018", "dom"],
"paths": {
"ng-mocks": ["../../libs/ng-mocks/src/index"],
"ng-mocks/dist/*": ["../../libs/ng-mocks/src/*"]
"ng-mocks": ["../../libs/ng-mocks/src/index"]
}
}
}
19 changes: 8 additions & 11 deletions tests-failures/mock-builder-constructor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { InjectionToken, PipeTransform } from '@angular/core';
import * as assert from 'assert';
import { IMockBuilderResult, MockBuilder } from 'ng-mocks';
import { EMPTY, Observable } from 'rxjs';

Expand Down Expand Up @@ -73,13 +72,11 @@ MockBuilder(null, moduleWithProviders);
// @ts-expect-error: does not support modules with providers.
MockBuilder(moduleWithProviders);

const promise1: Promise<IMockBuilderResult> = MockBuilder();
const promise2: Promise<IMockBuilderResult> = MockBuilder().mock(MyModule);
const promise3: Promise<IMockBuilderResult> = MockBuilder().keep(MyModule);
const promise4: Promise<IMockBuilderResult> = MockBuilder().exclude(MyModule);
const promise5: Promise<IMockBuilderResult> = MockBuilder().replace(MyModule, MyComponent);
assert(promise1);
assert(promise2);
assert(promise3);
assert(promise4);
assert(promise5);
const promise: Promise<IMockBuilderResult> =
MockBuilder() ??
MockBuilder().mock(MyModule) ??
MockBuilder().keep(MyModule) ??
MockBuilder().exclude(MyModule) ??
MockBuilder().replace(MyModule, MyComponent);

Promise.all([promise]);
70 changes: 31 additions & 39 deletions tests/issue-145/components.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Component } from '@angular/core';
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MockComponent } from 'ng-mocks';
import coreReflectDirectiveResolve from 'ng-mocks/dist/lib/common/core.reflect.directive-resolve';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Component({
selector: 'component',
selector: 'component1',
template: '',
})
export class ComponentDefault {}
Expand All @@ -17,7 +16,7 @@ export class ComponentDefault {}
useExisting: ComponentValueAccessor,
},
],
selector: 'component',
selector: 'component2',
template: '',
})
export class ComponentValueAccessor {}
Expand All @@ -30,52 +29,45 @@ export class ComponentValueAccessor {}
useExisting: ComponentValidator,
},
],
selector: 'component',
selector: 'component3',
template: '',
})
export class ComponentValidator {}

describe('issue-145:components', () => {
ngMocks.faster();

beforeAll(() =>
MockBuilder()
.mock(ComponentDefault)
.mock(ComponentValueAccessor)
.mock(ComponentValidator),
);

it('does not add NG_VALUE_ACCESSOR to components', () => {
const mock = MockComponent(ComponentDefault);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: ComponentDefault,
useExisting: jasmine.anything(),
},
]);
const mock = MockRender(ComponentDefault);
expect(() =>
ngMocks.get(mock.point, ComponentDefault),
).not.toThrow();
});

it('adds NG_VALUE_ACCESSOR to components that provide it', () => {
const mock = MockComponent(ComponentValueAccessor);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: ComponentValueAccessor,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALUE_ACCESSOR,
useFactory: jasmine.anything(),
},
]);
const mock = MockRender(ComponentValueAccessor);
expect(() =>
ngMocks.get(mock.point, ComponentValueAccessor),
).not.toThrow();
expect(() =>
ngMocks.get(mock.point, NG_VALUE_ACCESSOR),
).not.toThrow();
});

it('respects NG_VALIDATORS too', () => {
const mock = MockComponent(ComponentValidator);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: ComponentValidator,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALIDATORS,
useFactory: jasmine.anything(),
},
]);
const mock = MockRender(ComponentValidator);
expect(() =>
ngMocks.get(mock.point, ComponentValidator),
).not.toThrow();
expect(() =>
ngMocks.get(mock.point, NG_VALIDATORS),
).not.toThrow();
});
});
70 changes: 31 additions & 39 deletions tests/issue-145/directives.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Directive } from '@angular/core';
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms';
import { MockDirective } from 'ng-mocks';
import coreReflectDirectiveResolve from 'ng-mocks/dist/lib/common/core.reflect.directive-resolve';
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks';

@Directive({
selector: 'directive',
selector: 'directive1',
})
export class DirectiveDefault {}

Expand All @@ -16,7 +15,7 @@ export class DirectiveDefault {}
useExisting: DirectiveValueAccessor,
},
],
selector: 'directive',
selector: 'directive2',
})
export class DirectiveValueAccessor {}

Expand All @@ -28,52 +27,45 @@ export class DirectiveValueAccessor {}
useExisting: DirectiveValidator,
},
],
selector: 'directive',
selector: 'directive3',
})
export class DirectiveValidator {}

// providers should be added to directives only in case if they were specified in the original directive.
describe('issue-145:directives', () => {
ngMocks.faster();

beforeAll(() =>
MockBuilder()
.mock(DirectiveDefault)
.mock(DirectiveValueAccessor)
.mock(DirectiveValidator),
);

it('does not add NG_VALUE_ACCESSOR to directives', () => {
const mock = MockDirective(DirectiveDefault);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: DirectiveDefault,
useExisting: jasmine.anything(),
},
]);
const mock = MockRender(DirectiveDefault);
expect(() =>
ngMocks.get(mock.point, DirectiveDefault),
).not.toThrow();
});

it('adds NG_VALUE_ACCESSOR to directives that provide it', () => {
const mock = MockDirective(DirectiveValueAccessor);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: DirectiveValueAccessor,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALUE_ACCESSOR,
useFactory: jasmine.anything(),
},
]);
const mock = MockRender(DirectiveValueAccessor);
expect(() =>
ngMocks.get(mock.point, DirectiveValueAccessor),
).not.toThrow();
expect(() =>
ngMocks.get(mock.point, NG_VALUE_ACCESSOR),
).not.toThrow();
});

it('respects NG_VALIDATORS too', () => {
const mock = MockDirective(DirectiveValidator);
const { providers } = coreReflectDirectiveResolve(mock);
expect(providers).toEqual([
{
provide: DirectiveValidator,
useExisting: jasmine.anything(),
},
{
multi: true,
provide: NG_VALIDATORS,
useFactory: jasmine.anything(),
},
]);
const mock = MockRender(DirectiveValidator);
expect(() =>
ngMocks.get(mock.point, DirectiveValidator),
).not.toThrow();
expect(() =>
ngMocks.get(mock.point, NG_VALIDATORS),
).not.toThrow();
});
});
9 changes: 9 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ES2015",
"module": "ES2020",
"outDir": "dist/libs/ng-mocks/mjs",
"declarationDir": "dist/libs/ng-mocks/mjs"
}
}
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
"module": "commonjs",
"target": "es5",
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"emitDecoratorMetadata": true,
"downlevelIteration": true,
"inlineSourceMap": true,
"inlineSources": true,
"declaration": true,
"outDir": "dist/libs/ng-mocks/dist",
"declarationDir": "dist/libs/ng-mocks/dist",
"outDir": "dist/libs/ng-mocks/cjs",
"declarationDir": "dist/libs/ng-mocks/cjs",
"allowSyntheticDefaultImports": true,
"removeComments": false,
"experimentalDecorators": true,
"types": ["node", "jasmine", "jest"],
"baseUrl": ".",
"paths": {
"ng-mocks": ["libs/ng-mocks/src/index"],
"ng-mocks/dist/*": ["libs/ng-mocks/src/*"]
"ng-mocks": ["libs/ng-mocks/src/index"]
},
"skipLibCheck": true,

Expand Down

0 comments on commit b0da1f1

Please sign in to comment.