Skip to content

Commit

Permalink
Merge pull request #643 from satanTime/issues/629
Browse files Browse the repository at this point in the history
fix(mock-render): factory can be used in describes #629
  • Loading branch information
satanTime authored Jun 3, 2021
2 parents d7b5526 + e4ac201 commit 6a1dca8
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 74 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
- run:
name: Collecting https://codeclimate.com/github/ike18t/ng-mocks
command: |
curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 \
curl -sL https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 \
> $HOME/codeclimate
chmod +x $HOME/codeclimate
$HOME/codeclimate before-build
Expand Down
11 changes: 4 additions & 7 deletions docs/articles/api/MockRender.md
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ Especially, in cases, when the same setup should be used in different tests.

For example, we have 5 tests and every test calls `MockRender(MyComponent)`.
It means that every time a middleware component has been created and injected into `TestBed`,
whereas `MockRender` could have reused the existing middleware component and simply to create a new fixture out of it.
whereas `MockRender` could reuse the existing middleware component and simply would create a new fixture out of it.

In such situations, `MockRenderFactory` can be used instead of `MockRender`.
It accepts `bindings` and `providers`, but instead of an instant render,
Expand All @@ -390,14 +390,11 @@ and then 5 tests should call the factory in order to create fixtures.

```ts
describe('Maximum performance', () => {
const factory = MockRenderFactory(MyComponent, ['input1', 'input2']);

ngMocks.faster();

beforeAll(() => MockBuilder(MyComponent, MyModule));

let factory: MockRenderFactory<MyComponent>;
beforeAll(() => {
factory = MockRenderFactory(MyComponent, ['input1', 'input2']);
});
beforeAll(() => factory.configureTestBed());

it('covers one case', () => {
const fixture = factory({input1: 1});
Expand Down
11 changes: 6 additions & 5 deletions e2e/a10/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015:ivy -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5:ivy && npm run test:jasmine:es5:no-ivy && npm run test:jasmine:es2015:ivy && npm run test:jasmine:es2015:no-ivy",
"test:jasmine:es5:ivy": "ng test --ts-config ./tsconfig.es5ivy.spec.json --progress=false",
"test:jasmine:es5:no-ivy": "ng test --ts-config ./tsconfig.es5noivy.spec.json --progress=false",
"test:jasmine:es2015:ivy": "ng test --ts-config ./tsconfig.es2015ivy.spec.json --progress=false",
"test:jasmine:es2015:no-ivy": "ng test --ts-config ./tsconfig.es2015noivy.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5:ivy && npm run test:jest:es5:no-ivy &&npm run test:jest:es2015:ivy &&npm run test:jest:es2015:no-ivy",
"test:jest:es5:ivy": "jest -w 1 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 1 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 1 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 1 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5:ivy": "jest -w 2 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 2 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 2 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 2 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions e2e/a11/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015:ivy -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5:ivy && npm run test:jasmine:es5:no-ivy && npm run test:jasmine:es2015:ivy && npm run test:jasmine:es2015:no-ivy",
"test:jasmine:es5:ivy": "ng test --ts-config ./tsconfig.es5ivy.spec.json --progress=false",
"test:jasmine:es5:no-ivy": "ng test --ts-config ./tsconfig.es5noivy.spec.json --progress=false",
"test:jasmine:es2015:ivy": "ng test --ts-config ./tsconfig.es2015ivy.spec.json --progress=false",
"test:jasmine:es2015:no-ivy": "ng test --ts-config ./tsconfig.es2015noivy.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5:ivy && npm run test:jest:es5:no-ivy &&npm run test:jest:es2015:ivy &&npm run test:jest:es2015:no-ivy",
"test:jest:es5:ivy": "jest -w 1 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 1 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 1 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 1 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5:ivy": "jest -w 2 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 2 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 2 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 2 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 2 --watch"
},
"peerDependencies": {
"ng-mocks": "*"
Expand Down
7 changes: 4 additions & 3 deletions e2e/a12/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015:ivy -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5:ivy && npm run test:jasmine:es2015:ivy",
"test:jasmine:es5:ivy": "ng test --ts-config ./tsconfig.es5ivy.spec.json --progress=false",
"test:jasmine:es2015:ivy": "ng test --ts-config ./tsconfig.es2015ivy.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5:ivy && npm run test:jest:es2015:ivy",
"test:jest:es5:ivy": "jest -w 1 --config jest.es5ivy.js",
"test:jest:es2015:ivy": "jest -w 1 --config jest.es2015ivy.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5:ivy": "jest -w 2 --config jest.es5ivy.js",
"test:jest:es2015:ivy": "jest -w 2 --config jest.es2015ivy.js",
"test:jest:debug": "jest -w 2 --watch"
},
"peerDependencies": {
"ng-mocks": "*"
Expand Down
5 changes: 3 additions & 2 deletions e2e/a5es2015/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"start": "ng serve",
"build": "ng build --prod",
"test": "npm run test:jasmine -- --progress=false && npm run test:jest",
"test:debug": "npm run test:jasmine -- --browsers=Chrome --watch",
"test:jasmine": "ng test",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "jest -w 1",
"test:jest:debug": "jest -w 1 --watch"
"test:jest": "jest -w 2",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
5 changes: 3 additions & 2 deletions e2e/a5es5/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
"start": "ng serve",
"build": "ng build --prod",
"test": "npm run test:jasmine -- --progress=false && npm run test:jest",
"test:debug": "npm run test:jasmine -- --browsers=Chrome --watch",
"test:jasmine": "ng test",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "jest -w 1",
"test:jest:debug": "jest -w 1 --watch"
"test:jest": "jest -w 2",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions e2e/a6/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015 -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5 && npm run test:jasmine:es2015",
"test:jasmine:es5": "ng test --ts-config ./tsconfig.es5.spec.json --progress=false",
"test:jasmine:es2015": "ng test --ts-config ./tsconfig.es2015.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5 && npm run test:jest:es2015",
"test:jest:es5": "jest -w 1 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 1 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5": "jest -w 2 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 2 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions e2e/a7/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015 -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5 && npm run test:jasmine:es2015",
"test:jasmine:es5": "ng test --ts-config ./tsconfig.es5.spec.json --progress=false",
"test:jasmine:es2015": "ng test --ts-config ./tsconfig.es2015.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5 && npm run test:jest:es2015",
"test:jest:es5": "jest -w 1 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 1 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5": "jest -w 2 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 2 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
7 changes: 4 additions & 3 deletions e2e/a8/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015 -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5 && npm run test:jasmine:es2015",
"test:jasmine:es5": "ng test --ts-config ./tsconfig.es5.spec.json --progress=false",
"test:jasmine:es2015": "ng test --ts-config ./tsconfig.es2015.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5 && npm run test:jest:es2015",
"test:jest:es5": "jest -w 1 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 1 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5": "jest -w 2 --config ./jest.es5.js",
"test:jest:es2015": "jest -w 2 --config ./jest.es2015.js",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
11 changes: 6 additions & 5 deletions e2e/a9/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@
"start": "ng serve",
"build": "ng build",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015:ivy -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5:ivy && npm run test:jasmine:es5:no-ivy && npm run test:jasmine:es2015:ivy && npm run test:jasmine:es2015:no-ivy",
"test:jasmine:es5:ivy": "ng test --ts-config ./tsconfig.es5ivy.spec.json --progress=false",
"test:jasmine:es5:no-ivy": "ng test --ts-config ./tsconfig.es5noivy.spec.json --progress=false",
"test:jasmine:es2015:ivy": "ng test --ts-config ./tsconfig.es2015ivy.spec.json --progress=false",
"test:jasmine:es2015:no-ivy": "ng test --ts-config ./tsconfig.es2015noivy.spec.json --progress=false",
"test:jasmine:debug": "ng test -- --watch --browsers Chrome",
"test:jest": "npm run test:jest:es5:ivy && npm run test:jest:es5:no-ivy &&npm run test:jest:es2015:ivy &&npm run test:jest:es2015:no-ivy",
"test:jest:es5:ivy": "jest -w 1 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 1 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 1 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 1 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 1 --watch"
"test:jest:es5:ivy": "jest -w 2 --config jest.es5ivy.js",
"test:jest:es5:no-ivy": "jest -w 2 --config jest.es5noivy.js",
"test:jest:es2015:ivy": "jest -w 2 --config jest.es2015ivy.js",
"test:jest:es2015:no-ivy": "jest -w 2 --config jest.es2015noivy.js",
"test:jest:debug": "jest -w 2 --watch"
},
"private": true,
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions e2e/am/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"build:es5:ivy": "npm run build -- --ts-config ./tsconfig.es5ivy.app.json --prod",
"build:es2015:ivy": "npm run build -- --ts-config ./tsconfig.es2015ivy.app.json --prod",
"test": "npm run test:jasmine && npm run test:jest",
"test:debug": "npm run test:jasmine:es2015:ivy -- --browsers=Chrome --watch",
"test:jasmine": "npm run test:jasmine:es5:ivy && npm run test:jasmine:es2015:ivy",
"test:jasmine:es5:ivy": "ng test --ts-config ./tsconfig.es5ivy.spec.json --progress=false",
"test:jasmine:es2015:ivy": "ng test --ts-config ./tsconfig.es2015ivy.spec.json --progress=false",
Expand Down
48 changes: 36 additions & 12 deletions libs/ng-mocks/src/lib/common/ng-mocks-stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import ngMocksUniverse from './ng-mocks-universe';

export interface NgMocksStack {
id: object;
level: 'root' | 'suite' | 'test';
mockInstance?: any[];
}

type NgMocksStackCallback = (state: NgMocksStack, stack: NgMocksStack[]) => void;

// istanbul ignore next
const stack: NgMocksStack[] = ngMocksUniverse.global.get('reporter-stack') ?? [];
const stackRoot: NgMocksStack = { id: {}, level: 'root' };
const stack: NgMocksStack[] = ngMocksUniverse.global.get('reporter-stack') || [{ ...stackRoot }];
ngMocksUniverse.global.set('reporter-stack', stack);
const current = () => stack[stack.length - 1];

// istanbul ignore next
const listenersPush: NgMocksStackCallback[] = ngMocksUniverse.global.get('reporter-stack-push') ?? [];
Expand All @@ -19,10 +21,10 @@ ngMocksUniverse.global.set('reporter-stack-push', listenersPush);
const listenersPop: NgMocksStackCallback[] = ngMocksUniverse.global.get('reporter-stack-pop') ?? [];
ngMocksUniverse.global.set('reporter-stack-pop', listenersPop);

const stackPush = () => {
const stackPush = (level: NgMocksStack['level']) => {
const id = {};
ngMocksUniverse.global.set('reporter-stack-id', id);
const state = { id };
const state = { id, level };
stack.push(state);

for (const callback of listenersPush) {
Expand All @@ -31,10 +33,11 @@ const stackPush = () => {
};
const stackPop = () => {
const state = stack.pop();

// this code is actually needed for jest tests.
// istanbul ignore if
if (stack.length === 0) {
const id = {};
stack.push({ id });
stack.push({ ...stackRoot });
}

// istanbul ignore else
Expand All @@ -49,18 +52,38 @@ const stackPop = () => {

const reporterStack: jasmine.CustomReporter = {
jasmineDone: stackPop,
jasmineStarted: stackPush,
jasmineStarted: () => stackPush('root'),
specDone: stackPop,
specStarted: stackPush,
specStarted: () => stackPush('test'),
suiteDone: stackPop,
suiteStarted: stackPush,
suiteStarted: () => stackPush('suite'),
};

const messageCore = [
'ng-mocks cannot install own spec reporter.',
'This affects its core features for MockInstance and MockRender.',
'Please report an issue on github.',
'If you use jest v27, please add to its config testRunner=jest-jasmine2 for now',
'and upvote the issue on github: /~https://github.com/facebook/jest/issues/11483.',
].join(' ');

// istanbul ignore next
const messageCoreChecker = () => {
if (current().level === 'root') {
throw new Error(messageCore);
}
};

const install = () => {
if (!ngMocksUniverse.global.has('reporter-stack-install')) {
jasmine.getEnv().addReporter(reporterStack);
ngMocksUniverse.global.set('reporter-stack-install', true);
stackPush();
// istanbul ignore if
// tslint:disable-next-line strict-type-predicates
if (typeof jasmine === 'undefined') {
messageCoreChecker();
} else {
jasmine.getEnv().addReporter(reporterStack);
ngMocksUniverse.global.set('reporter-stack-install', true);
}
}

return ngMocksUniverse.global.has('reporter-stack-install');
Expand Down Expand Up @@ -100,6 +123,7 @@ const unsubscribePop = (callback: NgMocksStackCallback) => {
};

export default {
current,
install,
subscribePop,
subscribePush,
Expand Down
5 changes: 0 additions & 5 deletions libs/ng-mocks/src/lib/mock-render/func.create-wrapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, Directive } from '@angular/core';
import { TestBed } from '@angular/core/testing';

import coreDefineProperty from '../common/core.define-property';
import { Type } from '../common/core.types';
Expand Down Expand Up @@ -39,11 +38,7 @@ const generateWrapper = ({ bindings, options, inputs }: any) => {
}
}
}

Component(options)(MockRenderComponent);
TestBed.configureTestingModule({
declarations: [MockRenderComponent],
});

return MockRenderComponent;
};
Expand Down
Loading

0 comments on commit 6a1dca8

Please sign in to comment.