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

Fix bug mocking toString() #64

Merged
merged 4 commits into from
May 20, 2022
Merged
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
3 changes: 1 addition & 2 deletions .github/workflows/create-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Create Release
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
Expand All @@ -27,6 +25,7 @@ jobs:
run: npm ci && npm run build-release

- name: Release
if: github.repository == 'morganstanley/ts-mocking-bird'
uses: justincy/[email protected]
id: release
with:
Expand Down
10 changes: 7 additions & 3 deletions main/mock/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export function setupFunction<T, C extends ConstructorFunction<T>, U extends Fun
mocked.functionReplacementLookup['function'] || {});
functionLookup[functionName as string] = mockFunction;

if (mocked.mock[functionName] == null) {
// we do not replace an existing function in case it has already been destructured and sut already has a reference to it
// we do replace the mocked implementation above though
// eslint-disable-next-line @typescript-eslint/ban-types
if ((mocked.mock[functionName] as Function)?.name != functionReplacement.name) {
mocked.mock[functionName] = functionReplacement as any;
}
mocked.functionCallLookup[functionName] = [];
Expand All @@ -53,7 +56,7 @@ export function setupFunction<T, C extends ConstructorFunction<T>, U extends Fun
}

/**
* Mocks a staticfunction on an existing Mock.
* Mocks a static function on an existing Mock.
* Allows function call verification to be performed later in the test.
* You can optionally set a mock function implementation that will be called.
*
Expand Down Expand Up @@ -83,7 +86,8 @@ export function setupStaticFunction<
mocked.functionReplacementLookup['staticFunction'] || {});
staticFunctionLookup[functionName as string] = mockFunction;

if (mocked.mockConstructor[functionName] == null) {
// eslint-disable-next-line @typescript-eslint/ban-types
if ((mocked.mockConstructor[functionName] as Function)?.name != functionReplacement.name) {
mocked.mockConstructor[functionName] = functionReplacement as any;
}
mocked.staticFunctionCallLookup[functionName] = [];
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@morgan-stanley/ts-mocking-bird",
"version": "0.6.1",
"version": "0.6.2",
"description": "A fully type safe mocking, call verification and import replacement library for jasmine and jest",
"license": "Apache-2.0",
"author": "Morgan Stanley",
Expand Down
13 changes: 13 additions & 0 deletions spec/mock/mock-static.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,15 @@ describe('mock with statics', () => {
expect(newMock.mockConstructor.functionWithParamsAndNoReturn('', 123, true)).toBeUndefined();
expect(newMock.mockConstructor.functionWithParamsAndReturn('')).toBeUndefined();
});

it(`should work with predefined function toString`, () => {
const mockedValue = 'mocked toString return value';
const toStringMock = Mock.create<SampleMockedClass, typeof SampleMockedClass>().setup(
setupStaticFunction('toString', () => mockedValue),
);

expect(toStringMock.mockConstructor.toString()).toEqual(mockedValue);
});
});
});

Expand All @@ -1456,6 +1465,10 @@ class SampleMockedClass {

constructor(_paramsOne: {}, _paramTwo: Date) {}

public static toString(): string {
return 'original toString';
}

public static functionWithNoParamsAndNoReturn(): void {}

public functionWithNoParamsAndNoReturn(): void {}
Expand Down
7 changes: 7 additions & 0 deletions spec/mock/mock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1653,6 +1653,13 @@ describe('mock', () => {
expect(newMock.functionWithParamsAndNoReturn('', 123, true)).toBeUndefined();
expect(newMock.functionWithParamsAndReturn('')).toBeUndefined();
});

it(`should work with predefined function toString`, () => {
const mockedValue = 'mocked toString return value';
const toStringMock = Mock.create<Buffer>().setup(setupFunction('toString', () => mockedValue));

expect(toStringMock.mock.toString()).toEqual(mockedValue);
});
});

describe('parameter comparison', () => {
Expand Down