marvinhagemeister
marvinhagemeister

Reputation: 3144

Unable to extend typings of sinon

I'm having trouble getting sinon-stub-promise to work with typescript. The definition file in @types/sinon-stub-promise lacks the default export. I followed the docs on declaration merging closely but it doesn't compile.

Compile Error:

index.ts(4,18): error TS2345: Argument of type 'typeof 'sinon'' is not assignable to parameter of type 'SinonSandbox'.                                            
  Property 'clock' is missing in type 'typeof 'sinon''.                                                                                                           
index.ts(6,25): error TS2339: Property 'stub' does not exist on type 'typeof 'sinon''. 

Sinon's definition file:

declare namespace Sinon {
  // ...

  interface SinonStubStatic {
    (): SinonStub;
    (obj: any): SinonStub;
    (obj: any, method: string): SinonStub;
    (obj: any, method: string, func: any): SinonStub;
  }

  // ...

  interface SinonFakeTimers {
    now: number;
    create(now: number): SinonFakeTimers;
    setTimeout(callback: (...args: any[]) => void, timeout: number, ...args: any[]): number;
    // ...
  }

  interface SinonSandbox {
    clock: SinonFakeTimers;
    // ...
    stub: SinonStubStatic;
    // ...
  }
}

Adjusted sinon-stub-promise typings:

// Generated by typings
// Source: https://raw.githubusercontent.com/DefinitelyTyped/DefinitelyTyped/7de6c3dd94feaeb21f20054b9f30d5dabc5efabd/sinon-stub-promise/sinon-stub-promise.d.ts
/// <reference path="../../../node_modules/@types/sinon/index.d.ts" />
declare module 'sinon' {
  interface SinonPromise {
    resolves(value?: any): void;
    rejects(value?: any): void;
  }

  interface SinonStub {
    returnsPromise(): SinonPromise;
  }
}

// Added by me, because this is missing in the typings on dt
declare module 'sinon-stub-promise' {
  function setup(sinon: Sinon.SinonSandbox): void;
  export = setup;
}

index.ts

import * as sinon from 'sinon';
import sinonStubPromise = require('sinon-stub-promise');

sinonStubPromise(sinon);

Minimal sample repo: https://github.com/marvinhagemeister/typings-bug

Upvotes: 4

Views: 2722

Answers (1)

artem
artem

Reputation: 51689

The problem is that typings for sinon have export at top level, at the very end of @types/sinon/index.d.ts file:

declare var Sinon: Sinon.SinonStatic;

export = Sinon;

Which seems to make it impossible to extend using ambient modules.

But it's doable if you make all the modules external as well, however I have no idea how to make it work with typings (distributing it as @types on the other hand seems possible).

Put this in the sinon.d.ts file next to your index.d.ts (this is how you declare an extension for external sinon module - all interfaces will be merged):

import "sinon";
declare module "sinon" {

    export interface SinonPromise {
        resolves(value?: any): void;
        rejects(value?: any): void;
    }

    export interface SinonStub {
        returnsPromise(): SinonPromise;
    }

    // had to add these from SinonSandbox to SinonStatic
    // 
    // no idea if it's really supposed to have those methods
    export interface SinonStatic {
        requests: SinonFakeXMLHttpRequest;
        server: SinonFakeServer;
        useFakeServer(): SinonFakeServer;
        restore(): void;
    }
}

Put this in the sinon-stub-promise.d.ts file next to your index.ts:

import * as sinon from 'sinon';

declare function setup(sinon: sinon.SinonSandbox): void;

export = setup;

Then, in your index.ts

// in order to use your extension you have to import both here
import * as sinon from "sinon";
import "./sinon";


import sinonStubPromise = require("./sinon-stub-promise");

sinonStubPromise(sinon);

const mkdirStub = sinon.stub()
  .returnsPromise().resolves(null);

The generated javascript code looks good:

"use strict";
var sinon = require("sinon");
var sinonStubPromise = require("sinon-stub-promise");
sinonStubPromise(sinon);
var mkdirStub = sinon.stub()
    .returnsPromise().resolves(null);

Upvotes: 2

Related Questions