Skip to content

Commit

Permalink
feature(genericReuse): add types with generic to mock factory
Browse files Browse the repository at this point in the history
BREAKING CHANGE: extensions (Provider) provideMethod will be deprecated in
future releases in favour of provideMethodWithDeferredValue

Provider.provideMethod is deprecated changed:
Before:
        Provider.instance.provideMethod((name: string, value: any) => {
            ...
        });
After:
    Provider.instance.provideMethodWithDeferredValue((name: string, value: () => any) => {
        ...
    });

Read the DOCS for more information

* add global scope and move type reference cache in the scope

* remove space

* init scope

* add first version - extend generic still doesnt work

* remove unused method

* add back the new generic tests

* remove unused file, disable extensions, intersection and ts lib (WIP)

* simplify generic function

* merge generic tests and add reuse test

* remove unused files

* re enable working tests and add some info for this branch

* add test and comment to find test to fix

* add enumerable to object so it will have the correct output and add support to recursive call signatures

* fix type generic case

* restric interface to specific types so it will be easier to extend it

* rename function

* first working version generic extends

* remove unused descriptor and add more test to support types

* add more tests

* make sure factory cache is not exposed, divide caches

* refactor type parameter

* add more test and refactor the mess in mock factory call

* add more tests

* add more tests

* added back promises implementation and all test

* remove comment we will write test scenario when available

* add direct type test mock

* remove unused import

* remove comments

* remove unused field

* document playground command

* Update README.md

* remove unnecessary code, rename text and simplify for

* remove unnecessary create mock in the test

* remove unnecessary if

* deprecate getMethod and add new method to make sure with don't introduce breaking changes

* update deprecated comment

* update
  • Loading branch information
uittorio authored Nov 14, 2019
1 parent e3a77ff commit ddd94b0
Show file tree
Hide file tree
Showing 75 changed files with 1,501 additions and 561 deletions.
Binary file added .DS_Store
Binary file not shown.
7 changes: 7 additions & 0 deletions config/karma/karma.config.framework.deprecated.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const karmaBaseConfig = require('./karma.config.base');

module.exports = function(config) {
const karmaConfig = karmaBaseConfig(config, "../../test/framework/contextDeprecated.ts");

config.set(karmaConfig);
};
7 changes: 7 additions & 0 deletions config/karma/karma.config.transformer.playground.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const karmaBaseConfig = require('./karma.config.base');

module.exports = function(config) {
const karmaConfig = karmaBaseConfig(config, '../../test/transformer/playground.test.ts');

config.set(karmaConfig);
};
2 changes: 1 addition & 1 deletion config/modules/transformer/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../../tsconfig.json",
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"outDir": "../../../dist/transformer"
}
Expand Down
2 changes: 1 addition & 1 deletion config/modules/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist"
Expand Down
10 changes: 8 additions & 2 deletions docs/EXTENSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ For this reason if you need your custom spies you need to use our framework to s
To extend a method you need to:
1) set your spy function (jasmine.createSpy(name))

Please note that the value returned from provideMethodWithDeferredValue will be a function.
You will need to make sure that the method you are providing will not execute the function directly because it will cause Maximum call stack size.

In the example below it's using callFake that will prevent to execute the function directly.


```ts
import { Provider } from "ts-auto-mock/extension";

Provider.instance.provideMethod((name: string, value: any) => {
return jasmine.createSpy(name).and.returnValue(value);
Provider.instance.provideMethodWithDeferredValue((name: string, value: () => any)) => {
return jasmine.createSpy(name).and.callFake(value);
});
```
2) override the type of the return value
Expand Down
102 changes: 49 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
"build:transformer": "webpack --config config/modules/transformer/webpack.js",
"build:modules": "webpack --config config/modules/webpack.js",
"build": "npm run build:modules && npm run build:transformer",
"test": "npm run test:transformer && npm run test:framework",
"test": "npm run test:transformer && npm run test:framework && npm run test:frameworkDeprecated",
"test:debug": "npm run test:transformer DEBUG=true && npm run test:framework DEBUG=true",
"test:transformer": "karma start config/karma/karma.config.transformer.js",
"test:playground": "karma start config/karma/karma.config.transformer.playground.js",
"test:framework": "karma start config/karma/karma.config.framework.js",
"test:frameworkDeprecated": "karma start config/karma/karma.config.framework.deprecated.js",
"version": "conventional-changelog -p angular -i CHANGELOG.md -s && git add CHANGELOG.md",
"copyForPublish": "cp -rf package.json README.md CHANGELOG.md dist",
"preparePublish": "npm run build && npm run copyForPublish"
"preparePublish": "npm run build && npm run copyForPublish",
"build:playground": "ttsc test/transformer/playground.test.ts"
},
"keywords": [
"typescript",
Expand Down Expand Up @@ -48,6 +51,7 @@
"ts-node": "^8.4.1",
"tslint": "^5.20.0",
"tslint-loader": "^3.5.4",
"ttypescript": "1.5.7",
"typescript": "^3.6.4",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.9",
Expand Down
4 changes: 2 additions & 2 deletions src/extension/method/provider/functionMethod.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// tslint:disable:no-any
export function functionMethod(name: string, value: any): any {
export function functionMethod(name: string, value: () => any): any {
return (): any => {
return value;
return value();
};
}
27 changes: 25 additions & 2 deletions src/extension/method/provider/provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { functionMethod } from './functionMethod';
// tslint:disable-next-line:no-any
type Method = (name: string, value: any) => () => any;
// tslint:disable-next-line:no-any
type MethodWithDeferredValue = (name: string, value: () => any) => () => any;

export class Provider {
private _method: Method;
private _isUsingDeprecatedProvideMethod: boolean;

private constructor() {}
private constructor() {
this._isUsingDeprecatedProvideMethod = false;
}

private static _instance: Provider;

Expand All @@ -14,14 +19,32 @@ export class Provider {
return this._instance;
}

/**
* @deprecated ts-auto-mock will disable this functionality from version 2 because is not fully compatible with
* call signatures. It will cause an maximum call stack exceeded error.
* @see (https://github.com/uittorio/ts-auto-mock/blob/master/test/framework/recursive/recursive.deprecated.ts)
*
* Please use provideMethodWithDeferredValue instead
*
* @see [Extension](https://github.com/uittorio/ts-auto-mock/blob/master/docs/EXTENSION.md)
*/
public provideMethod(method: Method): void {
this._isUsingDeprecatedProvideMethod = true;
this._method = method;
}

public provideMethodWithDeferredValue(method: MethodWithDeferredValue): void {
this._method = method;
}

// tslint:disable-next-line:no-any
public getMethod(name: string, value: any): Method {
public getMethod(name: string, value: () => any): Method {
this._method = this._method || functionMethod;

if (this._isUsingDeprecatedProvideMethod) {
return this._method(name, value());
}

return this._method(name, value);
}
}
Loading

0 comments on commit ddd94b0

Please sign in to comment.