chai-spies-decorators
Advanced tools
Comparing version
@@ -15,72 +15,72 @@ import chai from 'chai' | ||
| const checklist = [] | ||
| const checklist = [] | ||
| Object.defineProperty ($global, 'will', { | ||
| Object.defineProperty ($global, 'will', { | ||
| configurable: true, | ||
| get () { | ||
| configurable: true, | ||
| get () { | ||
| return new Proxy (Object.assign (function will (chain, Class, name, desc) { | ||
| return new Proxy (Object.assign (function will (chain, Class, name, desc) { | ||
| const spied = chai.spy (desc.value) | ||
| const check = () => { | ||
| const spied = chai.spy (desc.value) | ||
| const check = () => { | ||
| let prev = undefined, | ||
| next = chai.expect (spied).to | ||
| let prev = undefined, | ||
| next = chai.expect (spied).to | ||
| chain.forEach (what => { | ||
| chain.forEach (what => { | ||
| if (Array.isArray (what)) { // call args | ||
| if (Array.isArray (what)) { // call args | ||
| next = next.apply (prev, what) // prev.next (what) | ||
| next = next.apply (prev, what) // prev.next (what) | ||
| } else { // .what | ||
| } else { // .what | ||
| prev = next | ||
| next = next[what] | ||
| } | ||
| }) | ||
| } | ||
| prev = next | ||
| next = next[what] | ||
| } | ||
| }) | ||
| } | ||
| checklist.push (check) | ||
| checklist.push (check) | ||
| return { | ||
| return { | ||
| configurable: desc.configurable, | ||
| enumerable: desc.enumerable, | ||
| configurable: desc.configurable, | ||
| enumerable: desc.enumerable, | ||
| get () { | ||
| return (...args) => spied.apply (this, args) | ||
| } | ||
| } | ||
| get () { | ||
| return (...args) => spied.apply (this, args) | ||
| } | ||
| } | ||
| }, { chain: [] }), { | ||
| }, { chain: [] }), { | ||
| apply (target, proxy, args) { | ||
| apply (target, proxy, args) { | ||
| if (args[2] && (args[2].value instanceof Function)) { | ||
| if (args[2] && (args[2].value instanceof Function)) { | ||
| return target (target.chain, ...args) | ||
| return target (target.chain, ...args) | ||
| } else { | ||
| } else { | ||
| target.chain.push (args) | ||
| } | ||
| return proxy | ||
| }, | ||
| target.chain.push (args) | ||
| } | ||
| return proxy | ||
| }, | ||
| get (target, prop, proxy) { | ||
| get (target, prop, proxy) { | ||
| target.chain.push (prop) | ||
| target.chain.push (prop) | ||
| return proxy | ||
| } | ||
| return proxy | ||
| } | ||
| }) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
| return checklist | ||
| return checklist | ||
| } | ||
@@ -92,30 +92,30 @@ | ||
| new Proxy (prevIt, { | ||
| new Proxy (prevIt, { | ||
| apply (target, thisArg, args) { const [ testName, testFn ] = args | ||
| apply (target, thisArg, args) { const [ testName, testFn ] = args | ||
| return prevIt.call (thisArg, testName, function (...args) { | ||
| return prevIt.call (thisArg, testName, function (...args) { | ||
| const checks = gatherContractChecks () | ||
| const checks = gatherContractChecks () | ||
| return Promise.resolve ().then (testFn.bind (this, ...args)).then (result => { | ||
| return Promise.resolve ().then (testFn.bind (this, ...args)).then (result => { | ||
| checks.forEach (check => check ()) | ||
| checks.forEach (check => check ()) | ||
| if (shouldFail) { | ||
| shouldFail = false | ||
| throw new Error ('expected to fail, but it did not happen') | ||
| } | ||
| if (shouldFail) { | ||
| shouldFail = false | ||
| throw new Error ('expected to fail, but it did not happen') | ||
| } | ||
| return result | ||
| return result | ||
| }).catch (e => { | ||
| }).catch (e => { | ||
| if (!shouldFail) { throw e } | ||
| if (!shouldFail) { throw e } | ||
| }) | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
| }) | ||
@@ -122,0 +122,0 @@ /* ------------------------------------------------------------------------ */ |
| { | ||
| "name": "chai-spies-decorators", | ||
| "version": "0.0.4", | ||
| "version": "0.0.5", | ||
| "description": "Chai Spies + ES7 decorators", | ||
@@ -5,0 +5,0 @@ "main": "chai-spies-decorators.js", |
72
test.js
@@ -8,57 +8,57 @@ import './chai-spies-decorators' | ||
| it ('works', function () { | ||
| it ('works', function () { | ||
| this.timeout.should.be.an.instanceof (Function) // it() hook should correctly pass 'this' | ||
| this.timeout.should.be.an.instanceof (Function) // it() hook should correctly pass 'this' | ||
| const foo = new (class Foo { | ||
| const foo = new (class Foo { | ||
| @will.have.been.called.with ('qux').once | ||
| bar (qux) { return qux + qux } | ||
| }) | ||
| @will.have.been.called.with ('qux').once | ||
| bar (qux) { return qux + qux } | ||
| }) | ||
| foo.bar ('qux').should.equal ('quxqux') | ||
| }) | ||
| foo.bar ('qux').should.equal ('quxqux') | ||
| }) | ||
| it.fails ('because of explicit throw', () => { | ||
| it.fails ('because of explicit throw', () => { | ||
| throw new Error () | ||
| }) | ||
| throw new Error () | ||
| }) | ||
| it.fails ('fails due to calling more than once', () => { | ||
| it.fails ('fails due to calling more than once', () => { | ||
| const foo = new (class Foo { | ||
| const foo = new (class Foo { | ||
| @will.have.been.called.once | ||
| bar () { } | ||
| }) | ||
| @will.have.been.called.once | ||
| bar () { } | ||
| }) | ||
| foo.bar () | ||
| foo.bar () | ||
| }) | ||
| foo.bar () | ||
| foo.bar () | ||
| }) | ||
| it.fails ('due to invalid argument', () => { | ||
| it.fails ('due to invalid argument', () => { | ||
| const foo = new (class Foo { | ||
| const foo = new (class Foo { | ||
| @will.have.been.called.with ('qux') | ||
| bar (qux) { } | ||
| }) | ||
| @will.have.been.called.with ('qux') | ||
| bar (qux) { } | ||
| }) | ||
| foo.bar ('zap') | ||
| }) | ||
| foo.bar ('zap') | ||
| }) | ||
| it ('multiple @will dont mess up things', () => { | ||
| it ('multiple @will dont mess up things', () => { | ||
| const foo = new (class Foo { | ||
| const foo = new (class Foo { | ||
| @will.have.been.called.once | ||
| bar () { } | ||
| @will.have.been.called.once | ||
| bar () { } | ||
| @will.have.been.called.twice | ||
| baz () { } | ||
| }) | ||
| @will.have.been.called.twice | ||
| baz () { } | ||
| }) | ||
| foo.bar () | ||
| foo.baz (); foo.baz () | ||
| }) | ||
| foo.bar () | ||
| foo.baz (); foo.baz () | ||
| }) | ||
| }) |
8192
13.79%