diff --git a/chai-spies/chai-spies-tests.ts b/chai-spies/chai-spies-tests.ts new file mode 100644 index 0000000000..7ac0610e4d --- /dev/null +++ b/chai-spies/chai-spies-tests.ts @@ -0,0 +1,135 @@ +/// +/// + +import * as chai from 'chai'; +import * as spies from 'chai-spies'; +import * as Mocha from 'mocha'; + +function original(): void { + // do something cool +} + +let ee = { + on(name: string, fn: () => void) { + } +}; + +let spiedFn = chai.spy(original); + +// then use in place of original +ee.on('some event', spiedFn); + +// or use without original +let spy_again = chai.spy(); +ee.on('some other event', spy_again); + +// or you can track an object's method +let array = [ 1, 2, 3 ]; +chai.spy.on(array, 'push'); + +// or you can track multiple object's methods +chai.spy.on(array, 'push', 'pop'); + +array.push(5); + +// and you can reset the object calls +// array.push.reset(); + +// or you can create spy object +let object = chai.spy.object([ 'push', 'pop' ]); +object.push(5); + +// or you create spy which returns static value +spiedFn = chai.spy.returns(true); + +spiedFn(); // true + + +let should = chai.should() + , expect = chai.expect; + +const spy = chai.spy(); + +// .spy + +expect(spy).to.be.spy; +spy.should.be.spy; + +// .called + +expect(spy).to.have.been.called(); +spy.should.have.been.called(); + +// .with +const spyStringArg = chai.spy((arg: string) => arg); +spyStringArg('foo'); +expect(spyStringArg).to.have.been.called.with('foo'); +spyStringArg.should.have.been.called.with('foo'); + +const spyTwoStringArgsAndOneNumber = chai.spy((arg1: string, arg2: string, arg3: number) => arg3); +spyTwoStringArgsAndOneNumber('foo', 'bar', 1); +expect(spyTwoStringArgsAndOneNumber).to.have.been.called.with('bar', 'foo'); +spyTwoStringArgsAndOneNumber.should.have.been.called.with('bar', 'foo'); + +// .with.exactly +const spyTwoStringArgs = chai.spy((arg1: string, arg2: string) => arg1); +spyTwoStringArgs('', ''); +spyTwoStringArgs('foo', 'bar'); +expect(spyTwoStringArgs).to.have.been.called.with.exactly('foo', 'bar'); +spyTwoStringArgs.should.have.been.called.with.exactly('foo', 'bar'); + +// .always.with +const spyThreeAnyArgs = chai.spy((arg1: any, arg2: any, arg3: any) => arg1); +spyThreeAnyArgs('foo', null, null); +spyThreeAnyArgs('foo', 'bar', null); +spyThreeAnyArgs(1, 2, 'foo'); +expect(spy).to.have.been.called.always.with('foo'); +spy.should.have.been.called.always.with('foo'); + +// .always.with.exactly +spyStringArg('foo'); +spyStringArg('foo'); +expect(spyStringArg).to.have.been.called.always.with.exactly('foo'); +spyStringArg.should.have.been.called.always.with.exactly('foo'); + +// .once +expect(spy).to.have.been.called.once; +expect(spy).to.not.have.been.called.once; +spy.should.have.been.called.once; +spy.should.not.have.been.called.once; + +// .twice +expect(spy).to.have.been.called.twice; +expect(spy).to.not.have.been.called.twice; +spy.should.have.been.called.twice; +spy.should.not.have.been.called.twice; + +// .exactly(n) +expect(spy).to.have.been.called.exactly(3); +expect(spy).to.not.have.been.called.exactly(3); +spy.should.have.been.called.exactly(3); +spy.should.not.have.been.called.exactly(3); + +// .min(n) / .at.least(n) +expect(spy).to.have.been.called.min(3); +expect(spy).to.not.have.been.called.at.least(3); +spy.should.have.been.called.at.least(3); +spy.should.not.have.been.called.min(3); + +// .max(n) / .at.most(n) +expect(spy).to.have.been.called.max(3); +expect(spy).to.not.have.been.called.at.most(3); +spy.should.have.been.called.at.most(3); +spy.should.not.have.been.called.max(3); + +// .above(n) / .gt(n) +expect(spy).to.have.been.called.above(3); +expect(spy).to.not.have.been.called.gt(3); +spy.should.have.been.called.gt(3); +spy.should.not.have.been.called.above(3); + +// .below(n) / .lt(n) +expect(spy).to.have.been.called.below(3); +expect(spy).to.not.have.been.called.lt(3); +spy.should.have.been.called.lt(3); +spy.should.not.have.been.called.below(3); \ No newline at end of file diff --git a/chai-spies/index.d.ts b/chai-spies/index.d.ts new file mode 100644 index 0000000000..ffed6e594b --- /dev/null +++ b/chai-spies/index.d.ts @@ -0,0 +1,411 @@ +// Type definitions for chai-spies +// Project: https://github.com/chaijs/chai-spies +// Definitions by: Ilya Kuznetsov +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare namespace Chai { + interface ChaiStatic { + spy: ChaiSpies.Spy; + } + + interface Assertion { + /** + * ####.spy + * Asserts that object is a spy. + * ```ts + * expect(spy).to.be.spy; + * spy.should.be.spy; + * ``` + */ + spy: Assertion; + + /** + * ####.called + * Assert that a spy has been called. Negation passes through. + * ```ts + * expect(spy).to.have.been.called(); + * spy.should.have.been.called(); + * ``` + * Note that ```called``` can be used as a chainable method. + */ + called: ChaiSpies.Called; + } +} + +declare namespace ChaiSpies { + + interface Spy { + /** + * #### chai.spy (function) + * + * Wraps a function in a proxy function. All calls will pass through to the original function. + * ```ts + * function original() {} + * var spy = chai.spy(original) + * , e_spy = chai.spy(); + * ``` + * @param fn function to spy on. @default ```function () {}``` + * @returns function to actually call + */ + (): SpyFunc0Proxy; + (fn: SpyFunc0): SpyFunc0Proxy; + (fn: SpyFunc1): SpyFunc1Proxy; + (fn: SpyFunc2): SpyFunc2Proxy; + (fn: SpyFunc3): SpyFunc3Proxy; + (fn: SpyFunc4): SpyFunc4Proxy; + (fn: SpyFunc5): SpyFunc5Proxy; + (fn: SpyFunc6): SpyFunc6Proxy; + (fn: SpyFunc7): SpyFunc7Proxy; + (fn: SpyFunc8): SpyFunc8Proxy; + (fn: SpyFunc9): SpyFunc9Proxy; + (fn: SpyFunc10): SpyFunc10Proxy; + (name: string, fn: SpyFunc0): SpyFunc0Proxy; + (name: string, fn: SpyFunc1): SpyFunc1Proxy; + (name: string, fn: SpyFunc2): SpyFunc2Proxy; + (name: string, fn: SpyFunc3): SpyFunc3Proxy; + (name: string, fn: SpyFunc4): SpyFunc4Proxy; + (name: string, fn: SpyFunc5): SpyFunc5Proxy; + (name: string, fn: SpyFunc6): SpyFunc6Proxy; + (name: string, fn: SpyFunc7): SpyFunc7Proxy; + (name: string, fn: SpyFunc8): SpyFunc8Proxy; + (name: string, fn: SpyFunc9): SpyFunc9Proxy; + (name: string, fn: SpyFunc10): SpyFunc10Proxy; + + /** + * #### chai.spy.on (function) + * + * Wraps an object method into spy. All calls will pass through to the original function. + * ```ts + * var spy = chai.spy.on(Array, 'isArray'); + * ``` + * @param {Object} object + * @param {String} method name to spy on + * @returns function to actually call + */ + on(object: Object, ...methodNames: string[]): any; + + /** + * #### chai.spy.object (function) + * + * Creates an object with spied methods. + * ```ts + * var object = chai.spy.object('Array', [ 'push', 'pop' ]); + * ``` + * @param {String} [name] object name + * @param {String[]|Object} method names or method definitions + * @returns object with spied methods + */ + object(name: string, methods: string[]): any; + object(methods: string[]): any; + object(name: string, methods: T): T; + object(methods: T): T; + + /** + * #### chai.spy.returns (function) + * + * Creates a spy which returns static value. + *```ts + * var method = chai.spy.returns(true); + *``` + * @param {*} value static value which is returned by spy + * @returns new spy function which returns static value + * @api public + */ + + returns(value: T): SpyFunc0Proxy; + } + + interface Called { + (): Chai.Assertion; + with: With; + always: Always; + + /** + * ####.once + * Assert that a spy has been called exactly once. + * ```ts + * expect(spy).to.have.been.called.once; + * expect(spy).to.not.have.been.called.once; + * spy.should.have.been.called.once; + * spy.should.not.have.been.called.once; + * ``` + */ + once: Chai.Assertion; + + /** + * ####.twice + * Assert that a spy has been called exactly twice. + * ```ts + * expect(spy).to.have.been.called.twice; + * expect(spy).to.not.have.been.called.twice; + * spy.should.have.been.called.twice; + * spy.should.not.have.been.called.twice; + * ``` + */ + twice: Chai.Assertion; + + /** + * ####.exactly(n) + * Assert that a spy has been called exactly ```n``` times. + * ```ts + * expect(spy).to.have.been.called.exactly(3); + * expect(spy).to.not.have.been.called.exactly(3); + * spy.should.have.been.called.exactly(3); + * spy.should.not.have.been.called.exactly(3); + * ``` + */ + exactly(n: number): Chai.Assertion; + + /** + * ####.min(n) / .at.least(n) + * Assert that a spy has been called minimum of ```n``` times. + * ```ts + * expect(spy).to.have.been.called.min(3); + * expect(spy).to.not.have.been.called.at.least(3); + * spy.should.have.been.called.at.least(3); + * spy.should.not.have.been.called.min(3); + * ``` + */ + min(n: number): Chai.Assertion; + + /** + * ####.max(n) / .at.most(n) + * Assert that a spy has been called maximum of ```n``` times. + * ```ts + * expect(spy).to.have.been.called.max(3); + * expect(spy).to.not.have.been.called.at.most(3); + * spy.should.have.been.called.at.most(3); + * spy.should.not.have.been.called.max(3); + * ``` + */ + max(n: number): Chai.Assertion; + + at: At; + /** + * ####.above(n) / .gt(n) + * Assert that a spy has been called more than ```n``` times. + * ```ts + * expect(spy).to.have.been.called.above(3); + * spy.should.not.have.been.called.above(3); + * ``` + */ + above(n: number): Chai.Assertion; + + /** + * ####.above(n) / .gt(n) + * Assert that a spy has been called more than ```n``` times. + * ```ts + * expect(spy).to.have.been.called.gt(3); + * spy.should.not.have.been.called.gt(3); + * ``` + */ + gt(n: number): Chai.Assertion; + + /** + * ####.below(n) / .lt(n) + * Assert that a spy has been called fewer than ```n``` times. + * ```ts + * expect(spy).to.have.been.called.below(3); + * spy.should.not.have.been.called.below(3); + * ``` + */ + below(n: number): Chai.Assertion; + + /** + * ####.below(n) / .lt(n) + * Assert that a spy has been called fewer than ```n``` times. + * ```ts + * expect(spy).to.have.been.called.lt(3); + * spy.should.not.have.been.called.lt(3); + * ``` + */ + lt(n: number): Chai.Assertion; + } + + interface With { + /** + * ####.with + * Assert that a spy has been called with a given argument at least once, even if more arguments were provided. + * ```ts + * spy('foo'); + * expect(spy).to.have.been.called.with('foo'); + * spy.should.have.been.called.with('foo'); + * ``` + * Will also pass for ```spy('foo', 'bar')``` and ```spy(); spy('foo')```. + * If used with multiple arguments, assert that a spy has been called with all the given arguments at least once. + * ```ts + * spy('foo', 'bar', 1); + * expect(spy).to.have.been.called.with('bar', 'foo'); + * spy.should.have.been.called.with('bar', 'foo'); + * ``` + */ + (a: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion; + + /** + * ####.with.exactly + * Similar to .with, but will pass only if the list of arguments is exactly the same as the one provided. + * ```ts + * spy(); + * spy('foo', 'bar'); + * expect(spy).to.have.been.called.with.exactly('foo', 'bar'); + * spy.should.have.been.called.with.exactly('foo', 'bar'); + * ``` + * Will not pass for ```spy('foo')```, ```spy('bar')```, ```spy('bar'); spy('foo')```, ```spy('foo'); spy('bar')```, ```spy('bar', 'foo')``` or ```spy('foo', 'bar', 1)```. + * Can be used for calls with a single argument too. + */ + + exactly(a?: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion; + } + + interface Always { + with: AlwaysWith; + } + + interface AlwaysWith { + /** + * ####.always.with + * Assert that every time the spy has been called the argument list contained the given arguments. + * ```ts + * spy('foo'); + * spy('foo', 'bar'); + * spy(1, 2, 'foo'); + * expect(spy).to.have.been.called.always.with('foo'); + * spy.should.have.been.called.always.with('foo'); + * ``` + */ + (a: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion; + + /** + * ####.always.with.exactly + * Assert that the spy has never been called with a different list of arguments than the one provided. + * ```ts + * spy('foo'); + * spy('foo'); + * expect(spy).to.have.been.called.always.with.exactly('foo'); + * spy.should.have.been.called.always.with.exactly('foo'); + * ``` + */ + exactly(a?: any, b?: any, c?: any, d?: any, e?: any, f?: any, g?: any, h?: any, i?: any, j?: any): Chai.Assertion; + } + + interface At { + /** + * ####.min(n) / .at.least(n) + * Assert that a spy has been called minimum of ```n``` times. + * ```ts + * expect(spy).to.have.been.called.min(3); + * expect(spy).to.not.have.been.called.at.least(3); + * spy.should.have.been.called.at.least(3); + * spy.should.not.have.been.called.min(3); + * ``` + */ + least(n: number): Chai.Assertion; + + /** + * ####.max(n) / .at.most(n) + * Assert that a spy has been called maximum of ```n``` times. + * ```ts + * expect(spy).to.have.been.called.max(3); + * expect(spy).to.not.have.been.called.at.most(3); + * spy.should.have.been.called.at.most(3); + * spy.should.not.have.been.called.max(3); + * ``` + */ + most(n: number): Chai.Assertion; + } + + interface Resetable { + /** + * #### proxy.reset (function) + * + * Resets __spy object parameters for instantiation and reuse + * @returns proxy spy object + */ + reset(): this; + } + + interface SpyFunc0 { + (): R; + } + + interface SpyFunc1 { + (a: A1): R; + } + + interface SpyFunc2 { + (a: A1, b: A2): R; + } + + interface SpyFunc3 { + (a: A1, b: A2, c: A3): R; + } + + interface SpyFunc4 { + (a: A1, b: A2, c: A3, d: A4): R; + } + + interface SpyFunc5 { + (a: A1, b: A2, c: A3, d: A4, e: A5): R; + } + + interface SpyFunc6 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6): R; + } + + interface SpyFunc7 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7): R; + } + + interface SpyFunc8 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8): R; + } + + interface SpyFunc9 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9): R; + } + + interface SpyFunc10 { + (a: A1, b: A2, c: A3, d: A4, e: A5, f: A6, g: A7, h: A8, i: A9, j: A10): R; + } + + interface SpyFunc0Proxy extends SpyFunc0, Resetable { + } + + interface SpyFunc1Proxy extends SpyFunc1, Resetable { + } + + interface SpyFunc2Proxy extends SpyFunc2, Resetable { + } + + interface SpyFunc3Proxy extends SpyFunc3, Resetable { + } + + interface SpyFunc4Proxy extends SpyFunc4, Resetable { + } + + interface SpyFunc5Proxy extends SpyFunc5, Resetable { + } + + interface SpyFunc6Proxy extends SpyFunc6, Resetable { + } + + interface SpyFunc7Proxy extends SpyFunc7, Resetable { + } + + interface SpyFunc8Proxy extends SpyFunc8, Resetable { + } + + interface SpyFunc9Proxy extends SpyFunc9, Resetable { + } + + interface SpyFunc10Proxy extends SpyFunc10, Resetable { + } +} + +declare var spies: ChaiSpies.Spy; + +declare module "chai-spies" { + export = spies; +} diff --git a/chai-spies/tsconfig.json b/chai-spies/tsconfig.json new file mode 100644 index 0000000000..91c115a7a5 --- /dev/null +++ b/chai-spies/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "chai-spies-tests.ts" + ] +} \ No newline at end of file