From a550234df4ce5864dfd3607d78d6a753f8eb09db Mon Sep 17 00:00:00 2001 From: Stephan Kaag Date: Tue, 21 Jul 2020 10:22:41 +0200 Subject: [PATCH 1/4] Replace custom stubbing with sinon --- src/stub.ts | 33 +++++++++------------------------ test/stub.test.ts | 2 +- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/stub.ts b/src/stub.ts index 3ff293e..152bd8b 100644 --- a/src/stub.ts +++ b/src/stub.ts @@ -1,32 +1,17 @@ -import * as _ from 'lodash' +import * as sinon from 'sinon' -// eslint-disable-next-line valid-jsdoc /** * mocks an object's property */ -export default function (object: T, path: K, value: () => T[K]) { +export default function (object: T, path: K, value: any) { if (object === undefined || path === undefined) throw new Error('should not be undefined') + return { - run(ctx: {stubs: any[]}) { - ctx.stubs = ctx.stubs || [] - const descriptor = Object.getOwnPropertyDescriptor(object, path) - if (descriptor && descriptor.get) { - ctx.stubs.push(descriptor.get) - descriptor.get = value - Object.defineProperty(object, path, descriptor) - } else { - ctx.stubs.push(_.get(object, path)) - _.set(object, path, value) - } - }, finally(ctx: {stubs: any[]}) { - const stub = ctx.stubs.pop() - const descriptor = Object.getOwnPropertyDescriptor(object, path) - if (descriptor && descriptor.get) { - descriptor.get = stub - Object.defineProperty(object, path, descriptor) - } else { - _.set(object, path, stub) - } + run(ctx: {sandbox: sinon.SinonSandbox}) { + const sandbox = ctx.sandbox = ctx.sandbox || sinon.createSandbox() + sandbox.stub(object, path).value(value) + }, finally(ctx: {sandbox: sinon.SinonSandbox}) { + ctx.sandbox.restore() }, } -} +} \ No newline at end of file diff --git a/test/stub.test.ts b/test/stub.test.ts index f985845..7be60b4 100644 --- a/test/stub.test.ts +++ b/test/stub.test.ts @@ -38,7 +38,7 @@ describe('stub', () => { fancy .stdout() - .stub(mrGetter, 'foo', () => 2) + .stub(mrGetter, 'foo', 2) .end('resets getter', output => { console.log(mrGetter.foo) expect(output.stdout).to.equal('2\n') From 216d511cdd73b60094da2b481cbb2bc21b9aef6a Mon Sep 17 00:00:00 2001 From: Stephan Kaag Date: Tue, 21 Jul 2020 10:32:28 +0200 Subject: [PATCH 2/4] Satify linter --- src/stub.ts | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/stub.ts b/src/stub.ts index 152bd8b..95ddd41 100644 --- a/src/stub.ts +++ b/src/stub.ts @@ -3,15 +3,23 @@ import * as sinon from 'sinon' /** * mocks an object's property */ -export default function (object: T, path: K, value: any) { - if (object === undefined || path === undefined) throw new Error('should not be undefined') +export default function ( + object: T, + path: K, + value: any, +) { + if (object === undefined || path === undefined) + throw new Error('should not be undefined') return { - run(ctx: {sandbox: sinon.SinonSandbox}) { - const sandbox = ctx.sandbox = ctx.sandbox || sinon.createSandbox() - sandbox.stub(object, path).value(value) - }, finally(ctx: {sandbox: sinon.SinonSandbox}) { + run(ctx: { sandbox: sinon.SinonSandbox }) { + if (!ctx.sandbox) { + ctx.sandbox = sinon.createSandbox() + } + ctx.sandbox.stub(object, path).value(value) + }, + finally(ctx: { sandbox: sinon.SinonSandbox }) { ctx.sandbox.restore() }, } -} \ No newline at end of file +} From 122d47c447237bf92088775f1b826651ad8dd40d Mon Sep 17 00:00:00 2001 From: Stephan Kaag Date: Tue, 21 Jul 2020 10:55:24 +0200 Subject: [PATCH 3/4] Sinon is a dependency --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9d9dc65..47c0cb3 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "@types/lodash": "*", "@types/node": "*", "@types/sinon": "*", + "sinon": "^9.0.2", "lodash": "^4.17.13", "mock-stdin": "^1.0.0", "stdout-stderr": "^0.1.9" @@ -24,7 +25,6 @@ "markdown-toc": "^1.2.0", "mocha": "^5.2.0", "nock": "^13.0.0", - "sinon": "^9.0.2", "ts-node": "^8.0.2", "tslib": "^2.0.0", "typescript": "3.8.3" From 61a3d67d344319048179bc08fc73440d7737158d Mon Sep 17 00:00:00 2001 From: Stephan Kaag Date: Tue, 21 Jul 2020 12:54:07 +0200 Subject: [PATCH 4/4] Expose stub to user --- src/stub.ts | 9 +++++---- test/stub.test.ts | 8 +++----- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/stub.ts b/src/stub.ts index 95ddd41..975cada 100644 --- a/src/stub.ts +++ b/src/stub.ts @@ -6,20 +6,21 @@ import * as sinon from 'sinon' export default function ( object: T, path: K, - value: any, + fn: (stub: sinon.SinonStub) => sinon.SinonStub, ) { if (object === undefined || path === undefined) throw new Error('should not be undefined') + let stub: sinon.SinonStub return { run(ctx: { sandbox: sinon.SinonSandbox }) { if (!ctx.sandbox) { ctx.sandbox = sinon.createSandbox() } - ctx.sandbox.stub(object, path).value(value) + stub = fn(ctx.sandbox.stub(object, path)) }, - finally(ctx: { sandbox: sinon.SinonSandbox }) { - ctx.sandbox.restore() + finally() { + stub.restore() }, } } diff --git a/test/stub.test.ts b/test/stub.test.ts index 7be60b4..74857be 100644 --- a/test/stub.test.ts +++ b/test/stub.test.ts @@ -1,7 +1,5 @@ // tslint:disable no-console -import * as sinon from 'sinon' - import {expect, fancy} from '../src' const os = require('os') @@ -16,13 +14,13 @@ const mrGetter = { describe('stub', () => { // from readme fancy - .stub(os, 'platform', () => 'foobar') + .stub(os, 'platform', stub => stub.callsFake(() => 'foobar')) .end('sets os', () => { expect(os.platform()).to.equal('foobar') }) fancy - .stub(os, 'platform', sinon.stub().returns('foobar')) + .stub(os, 'platform', stub => stub.callsFake(() => 'foobar')) .end('uses sinon', () => { expect(os.platform()).to.equal('foobar') expect(os.platform.called).to.equal(true) @@ -38,7 +36,7 @@ describe('stub', () => { fancy .stdout() - .stub(mrGetter, 'foo', 2) + .stub(mrGetter, 'foo', stub => stub.get(() => 2)) .end('resets getter', output => { console.log(mrGetter.foo) expect(output.stdout).to.equal('2\n')