Skip to content

Commit

Permalink
feat: mm.spy method (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse committed Mar 13, 2020
1 parent 18c47a9 commit 5ed8a73
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 10 deletions.
42 changes: 32 additions & 10 deletions README.md
Expand Up @@ -60,16 +60,38 @@ const target = {

mm.data(target, 'add', 3);

(await target.add(1, 1)).should.equal(3);
target.add.called.should.equal(1);
target.add.calledArguments.should.eql([[ 1, 1 ]]);
target.add.lastCalledArguments.should.eql([ 1, 1 ]);

(await target.add(2, 2)).should.equal(3);
target.add.called.should.equal(2);
target.add.calledArguments.should.eql([[ 1, 1 ], [ 2, 2 ]]);
target.add.lastCalledArguments.should.eql([ 2, 2 ]);
});
assert.equal(await target.add(1, 1), 3);
assert.equal(target.add.called, 1);
assert.deepEqual(target.add.calledArguments, [[ 1, 1 ]]);
assert.deepEqual(target.add.lastCalledArguments, [ 1, 1 ]);

assert.equal(await target.add(2, 2), 3);
assert.equal(target.add.called, 2);
assert.deepEqual(target.add.calledArguments, [[ 1, 1 ], [ 2, 2 ]]);
assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]);
```

If you only need spy and don't need mock, you can use `mm.spy` method directly:

```js
const target = {
async add(a, b) {
await this.foo();
return a + b;
},
async foo() { /* */ },
};

mm.spy(target, 'add');
assert.equal(await target.add(1, 1), 2);
assert.equal(target.add.called, 1);
assert.deepEqual(target.add.calledArguments, [[ 1, 1 ]]);
assert.deepEqual(target.add.lastCalledArguments, [ 1, 1 ]);

assert.equal(await target.add(2, 2), 4);
assert.equal(target.add.called, 2);
assert.deepEqual(target.add.calledArguments, [[ 1, 1 ], [ 2, 2 ]]);
assert.deepEqual(target.add.lastCalledArguments, [ 2, 2 ]);
```

### Support generator function
Expand Down
10 changes: 10 additions & 0 deletions lib/mm.js
Expand Up @@ -217,6 +217,16 @@ exports.empty = function(mod, method, timeout) {
return exports.datas(mod, method, null, timeout);
};

/**
* spy a function
* @param {Object} mod, module object
* @param {String} method, mock module object method name.
*/
exports.spy = function(mod, method) {
if (typeof mod[method] !== 'function') throw new Error(`spy target ${method} is not a function`);
mock(mod, method, mod[method]);
};

/**
* mock function sync throw error
*
Expand Down
18 changes: 18 additions & 0 deletions test/mm.test.js
Expand Up @@ -1086,7 +1086,25 @@ describe('test/mm.test.js', () => {
target.add(2, 2).should.equal(3);
assert(target.add === mockAdd);
});

it('should mm.spy() work', async () => {
const target = {
async add(a, b) {
await this.foo();
return a + b;
},
async foo() { /* */ },
};

mm.spy(target, 'add');
(await target.add(1, 1)).should.equal(2);
(await target.add(2, 2)).should.equal(4);
target.add.called.should.equal(2);
target.add.calledArguments.should.eql([[ 1, 1 ], [ 2, 2 ]]);
target.add.lastCalledArguments.should.eql([ 2, 2 ]);
});
});

});

const enable = require('enable');
Expand Down

0 comments on commit 5ed8a73

Please sign in to comment.