Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mocking native async methods when targeting es2017 #109

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@
"lodash": "^4.17.5"
},
"jest": {
"globals": {
"ts-jest": {
"tsConfigFile": "tsconfig.test.json"
}
},
"testEnvironment": "jsdom",
"transform": {
"^.+\\.ts$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"^.+\\.ts$": "ts-jest"
},
"testRegex": "(/__tests__/.*|\\.(spec))\\.(ts|js)$",
"moduleFileExtensions": [
Expand Down
3 changes: 2 additions & 1 deletion src/utils/MockableFunctionsFinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
* - [.]functionName = (
* - [.]functionName = function(
* - [.]functionName = function otherName(
* - [.]functionName = async .
*/
export class MockableFunctionsFinder {
private functionNameRegex = /[.\s]([^.\s]+?)(?:\(|\s+=\s+(?:function\s*(?:[^.\s]+?\s*)?)?\()/g;
private functionNameRegex = /[.\s]([^.\s]+?)(?:\(|\s+=\s+(?:function\s*(?:[^.\s]+?\s*)?)?\(|\s+=\s+async\s)/g;
private cleanFunctionNameRegex = /^[.\s]([^.\s]+?)[\s(]/;
private excludedFunctionNames: string[] = ["hasOwnProperty", "function"];

Expand Down
61 changes: 61 additions & 0 deletions test/mocking.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,57 @@ describe("mocking", () => {

});
});

describe("mocking class with async methods", () => {
let mockedFoo: SampleClassWithAsync;
let foo: SampleClassWithAsync;

it("does create own method descriptors on instance 0", () => {
// given
mockedFoo = mock(SampleClassWithAsync);
foo = instance(mockedFoo);

// when
when(mockedFoo.asyncMethod0()).thenResolve(42);

// then
return foo.asyncMethod0()
.then(result => {
expect(result).toBe(42);
});
});

it("does create own method descriptors on instance 1", () => {
// given
mockedFoo = mock(SampleClassWithAsync);
foo = instance(mockedFoo);

// when
when(mockedFoo.asyncMethod1('bar')).thenResolve(42);

// then
return foo.asyncMethod1('bar')
.then(result => {
expect(result).toBe(42);
});
});

it("does create own method descriptors on instance 2", () => {
// given
mockedFoo = mock(SampleClassWithAsync);
foo = instance(mockedFoo);

// when
when(mockedFoo.asyncMethod2('bar', 2)).thenResolve(42);

// then
return foo.asyncMethod2('bar', 2)
.then(result => {
expect(result).toBe(42);
});
});
});

});

abstract class SampleAbstractClass {
Expand Down Expand Up @@ -211,3 +262,13 @@ class SampleGeneric<T> {
return null;
}
}

class SampleClassWithAsync {

protected constructor() {}

// because the compiler produces different signatures for each of these
public asyncMethod0 = async () => 4;
public asyncMethod1 = async (foo: string) => Number(foo);
public asyncMethod2 = async (foo: string, bar: number) => bar;
}
9 changes: 9 additions & 0 deletions test/utils/MockableFunctionsFinder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ describe("MockableFunctionsFinder", () => {
expect(result).toContain("log");
expect(result).toContain("toString");
expect(result).toContain("anonymousMethod");
expect(result).toContain("asyncMethod1");
expect(result).toContain("asyncMethod2");
});

it("should not find hasOwnProperty as it should not be mocked (because its used by mockito to evaluate properties)", () => {
Expand All @@ -36,6 +38,13 @@ export class Foo {
console.log(arg);
temp.hasOwnProperty("fakeProperty");
}

this.asyncMethod1 = async arg => {
}

this.asyncMethod2 = async (foo, bar) => {
}

}

private convertNumberToString(value:number):string {
Expand Down
2 changes: 1 addition & 1 deletion test/utils/ObjectPropertyCodeRetriever.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("ObjectPropertyCodeRetriever", () => {
expect(objectPropertyCodeRetriever.get(object, "nanProperty")).toBe("NaN");
expect(objectPropertyCodeRetriever.get(object, "stringProperty")).toBe("stringProperty");
expect(objectPropertyCodeRetriever.get(object, "booleanProperty")).toBe("true");
expect(objectPropertyCodeRetriever.get(object, "testMethod")).toMatch(/function \(\)/);
expect(objectPropertyCodeRetriever.get(object, "testMethod")).toMatch(/() => true/);
});

it("Provides code of given existing property accessors", () => {
Expand Down
9 changes: 9 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "./tsconfig",
"compilerOptions": {
"target": "es2017",
"lib": [
"es2017"
]
}
}