|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +/* eslint-disable no-console */ |
| 4 | + |
| 5 | +const chalk = require('chalk'); |
| 6 | + |
| 7 | +module.exports = (sinon) => { |
| 8 | + const {stub} = sinon; |
| 9 | + |
| 10 | + sinon.stub = () => { |
| 11 | + const fn = stub(); |
| 12 | + const {calledWith:original} = fn; |
| 13 | + |
| 14 | + fn.calledWith = (...args) => { |
| 15 | + if (original.apply(fn, args)) |
| 16 | + return true; |
| 17 | + |
| 18 | + return calledWith.apply(fn, args); |
| 19 | + }; |
| 20 | + |
| 21 | + return fn; |
| 22 | + }; |
| 23 | + |
| 24 | + Object.assign(sinon.stub, stub); |
| 25 | + |
| 26 | + return sinon; |
| 27 | +}; |
| 28 | + |
| 29 | +function calledWith(...args) { |
| 30 | + if (!this.called) { |
| 31 | + write(`expected to call with ${JSON.stringify(this.args)}, but not called at all\n`); |
| 32 | + return false; |
| 33 | + } |
| 34 | + |
| 35 | + const actual = this.args.pop(); |
| 36 | + |
| 37 | + write(`wrong arguments in ${this.func.name}`); |
| 38 | + writeObjectActual('actual:', actual); |
| 39 | + writeObjectExpected('expected:', args); |
| 40 | + |
| 41 | + return false; |
| 42 | +} |
| 43 | + |
| 44 | +function write(str) { |
| 45 | + process.stdout.write(chalk.red(str) + '\n'); |
| 46 | +} |
| 47 | + |
| 48 | +function writeObjectActual(str, object) { |
| 49 | + const json = JSON.stringify(object, null, 2); |
| 50 | + console.log(str, chalk.yellow(json) + '\n'); |
| 51 | +} |
| 52 | + |
| 53 | +function writeObjectExpected(str, object) { |
| 54 | + const json = JSON.stringify(object, null, 2); |
| 55 | + console.log(str, chalk.green(json) + '\n'); |
| 56 | +} |
| 57 | + |
0 commit comments