From f379b36e6174848d769b9344a2b0fe23d005dd42 Mon Sep 17 00:00:00 2001 From: Giles Roadnight <10414642+Roaders@users.noreply.github.com> Date: Thu, 19 May 2022 11:03:22 +0100 Subject: [PATCH 1/4] Fix toString bug --- main/mock/operators.ts | 10 +++++++--- spec/mock/mock-static.spec.ts | 13 +++++++++++++ spec/mock/mock.spec.ts | 7 +++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/main/mock/operators.ts b/main/mock/operators.ts index 928ae91..6446e5c 100644 --- a/main/mock/operators.ts +++ b/main/mock/operators.ts @@ -43,7 +43,10 @@ export function setupFunction, 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] = []; @@ -53,7 +56,7 @@ export function setupFunction, 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. * @@ -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] = []; diff --git a/spec/mock/mock-static.spec.ts b/spec/mock/mock-static.spec.ts index da88e43..6a94a28 100644 --- a/spec/mock/mock-static.spec.ts +++ b/spec/mock/mock-static.spec.ts @@ -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().setup( + setupStaticFunction('toString', () => mockedValue), + ); + + expect(toStringMock.mockConstructor.toString()).toEqual(mockedValue); + }); }); }); @@ -1456,6 +1465,10 @@ class SampleMockedClass { constructor(_paramsOne: {}, _paramTwo: Date) {} + public static toString(): string { + return 'original toString'; + } + public static functionWithNoParamsAndNoReturn(): void {} public functionWithNoParamsAndNoReturn(): void {} diff --git a/spec/mock/mock.spec.ts b/spec/mock/mock.spec.ts index 1a1290d..a2d4a39 100644 --- a/spec/mock/mock.spec.ts +++ b/spec/mock/mock.spec.ts @@ -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().setup(setupFunction('toString', () => mockedValue)); + + expect(toStringMock.mock.toString()).toEqual(mockedValue); + }); }); describe('parameter comparison', () => { From bc74594e6d504a30ee82799e7766930d4a40bdaf Mon Sep 17 00:00:00 2001 From: Giles Roadnight <10414642+Roaders@users.noreply.github.com> Date: Thu, 19 May 2022 11:03:38 +0100 Subject: [PATCH 2/4] Only run release tasks on ms repo --- .github/workflows/create-release.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/create-release.yaml b/.github/workflows/create-release.yaml index f947381..5739548 100644 --- a/.github/workflows/create-release.yaml +++ b/.github/workflows/create-release.yaml @@ -27,6 +27,7 @@ jobs: run: npm ci && npm run build-release - name: Release + if: github.repository == 'morganstanley/ts-mocking-bird' uses: justincy/github-action-npm-release@2.0.2 id: release with: From 27a3a9eef5a5583addfb417b48fde9248cd371c1 Mon Sep 17 00:00:00 2001 From: Giles Roadnight <10414642+Roaders@users.noreply.github.com> Date: Thu, 19 May 2022 13:20:41 +0100 Subject: [PATCH 3/4] 0.6.2 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4e68092..71aea03 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@morgan-stanley/ts-mocking-bird", - "version": "0.6.1", + "version": "0.6.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@morgan-stanley/ts-mocking-bird", - "version": "0.6.1", + "version": "0.6.2", "license": "Apache-2.0", "dependencies": { "lodash": "^4.17.16", diff --git a/package.json b/package.json index 70a4434..20c5105 100644 --- a/package.json +++ b/package.json @@ -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", From a38d5444dc810b1b654353733d2b32d947c349ae Mon Sep 17 00:00:00 2001 From: Giles Roadnight <10414642+Roaders@users.noreply.github.com> Date: Thu, 19 May 2022 13:28:51 +0100 Subject: [PATCH 4/4] Remove Pull Request from release build --- .github/workflows/create-release.yaml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.github/workflows/create-release.yaml b/.github/workflows/create-release.yaml index 5739548..20d6888 100644 --- a/.github/workflows/create-release.yaml +++ b/.github/workflows/create-release.yaml @@ -3,8 +3,6 @@ name: Create Release on: push: branches: [ main ] - pull_request: - branches: [ main ] workflow_dispatch: jobs: