Reputation: 11064
I am new to writing node tests.... this is a my first unit test for a gulp plugin I am trying to write:
var chai = require('chai');
var spy = require('chai-spies');
var es = require('event-stream');
var File = require('vinyl');
var mock = require('mock-fs');
var sinon = require('sinon');
var sinonChai = require("sinon-chai");
//var rewire = require("rewire");
//var myModule = rewire("./test.js");
var es = require('event-stream');
chai.should();
chai.use(sinonChai);
describe('gulp-newy', function() {
var fs = require('fs');
var fakeFile, pspy;
beforeEach(function() {
//myModule.__set__('__dirname', "/home/one");
mock({
__dirname: mock.directory({
mode: 0755,
items: {
file1: 'file one content',
file2: new Buffer([8, 6, 7, 5, 3, 0, 9])
}
})
});
});
afterEach(mock.restore);
describe('get files', function() {
it('should do something', function(done) {
mock({
foo: mock.file({
content: 'nothing',
mtime: new Date(Date.now())
}),
bar: mock.file({
content: 'nothing',
mtime: new Date(1,1)
})
});
fakeFile = new File({
contents: new Buffer('foo'),
history: ['foo']
});
var bar = function(dest) { return 'bar' };
spy1 = sinon.spy(console, "log");
stream = newy(bar);
stream.write(fakeFile);
stream.on('data', function() {
console.log("sss");
});
spy1.should.have.been.called();
done();
});
});
});
I get TypeError: Attempted to wrap log which is already wrapped
, but I don't see where it was previously wrapped before my spy.
Upvotes: 3
Views: 5774
Reputation: 472
With sinon you need to make sure you restore your stubs\mocks after every test. if you don't they remain stubs and if you try and stub them again in a different test it will shout about trying to wrap a method again. There are many ways to do this, contain all of the stubs within a sandbox, or just restore in the "after" clause. for example:
describe('This is a test', ()=> {
before(()=> {
sinon.stub(myObject,'myMethod', ()=> {
return 'stubbed result';
})
});
it('should stub my method', ()=>{
expect(myObject.myMethod).to.be.equal('stubbed result');
});
after(()=> {
//important part
myObject.myMethod.restore();
});
})
Upvotes: 3
Reputation: 11064
I was using Mocha --watch...which session never ends. Because of this.. the other spy existed. Answer is Cleaning up sinon stubs easily
Upvotes: 4