Last active
March 26, 2018 19:03
-
-
Save bryanjknight/81017e38390762e3d550261231afd3e0 to your computer and use it in GitHub Desktop.
MochaJS, SinonJS, Proxyquire, and how to stub internally instantiated classes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const fs = require('fs'); | |
const path = require('path'); | |
const NodeZip = require('node-zip'); | |
class JsonZipper { | |
constructor(path) { | |
this.path = path; | |
} | |
zipJsonFiles() { | |
const self = this; | |
return new Promise( function(resolve, reject) { | |
const files = fs.readdirSync(self.path); | |
const zippedFiles = []; | |
for (let i = 0; i < files.length; i++) { | |
const file = files[i]; | |
if (file.endsWith('.json')) { | |
const fullPath = path.join(self.path, file); | |
const zipFullPath = fullPath + '.zip'; | |
const zip = new NodeZip(); | |
zip.file(file, fs.readFileSync(fullPath)); | |
const data = zip.generate({ base64: false, compression: 'DEFLATE' }); | |
fs.writeFileSync(zipFullPath, data, 'binary'); | |
zippedFiles.push(zipFullPath); | |
} | |
} | |
resolve(zippedFiles); | |
}); | |
} | |
} | |
module.exports = JsonZipper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const assert = require('assert'); | |
const fs = require('fs'); | |
const sinon = require('sinon'); | |
const proxyquire = require('proxyquire'); | |
let zipFileStub, zipGenerateStub; | |
let fsReadDir, fsReadFile, fsWriteFile; | |
let jsonZipperStub; | |
describe('JsonZipper', function() { | |
beforeEach(function() { | |
zipFileStub = sinon.stub(); | |
zipGenerateStub = sinon.stub(); | |
fsReadDir = sinon.stub(); | |
fsReadFile = sinon.stub(); | |
fsWriteFile = sinon.stub(); | |
function MockNodeZip() { | |
MockNodeZip.prototype.file = zipFileStub; | |
MockNodeZip.prototype.generate = zipGenerateStub; | |
} | |
jsonZipperStub = proxyquire('../JsonZipper', { | |
'node-zip': MockNodeZip, // needs to be what goes through require | |
fs: { | |
readdirSync: fsReadDir, | |
readFileSync: fsReadFile, | |
writeFileSync: fsWriteFile, | |
} | |
}); | |
}); | |
it('fails when NodeZip fails', function(done) { | |
fsReadDir.returns(['file1.json']); | |
fsReadFile.returns('{}'); | |
zipGenerateStub.throws(); | |
const jz = new jsonZipperStub('./somewhere'); | |
jz.zipJsonFiles() | |
.catch(function(err) { | |
assert.ok(true); | |
}) | |
.then(done); | |
}); | |
it('can zip files', function() { | |
fsReadDir.returns(['file1.json']); | |
fsReadFile.returns('{}'); | |
const jz = new jsonZipperStub('./somewhere'); | |
return jz.zipJsonFiles() | |
.then(function() { | |
assert.equal(fsReadDir.callCount, 1); | |
assert.equal(fsReadFile.callCount, 1); | |
assert.equal(fsWriteFile.callCount, 1); | |
assert.equal(zipGenerateStub.callCount, 1); | |
}); | |
}); | |
it('can zip 2 files', function() { | |
fsReadDir.returns([ 'file.json', 'file2.json' ]); | |
fsReadFile.returns( '{}'); | |
const jz = new jsonZipperStub('./somewhere'); | |
return jz.zipJsonFiles() | |
.then(function() { | |
assert.equal(fsReadDir.callCount, 1); | |
assert.equal(fsReadFile.callCount, 2); | |
assert.equal(fsWriteFile.callCount, 2); | |
assert.equal(zipGenerateStub.callCount, 2); | |
}); | |
}); | |
it('gracefully does not zip zero files', function() { | |
fsReadDir.returns([ ]); | |
fsReadFile.throws(); | |
const jz = new jsonZipperStub('./somewhere'); | |
return jz.zipJsonFiles() | |
.then(function() { | |
assert.equal(fsReadDir.callCount, 1); | |
assert.equal(fsReadFile.callCount, 0); | |
assert.equal(fsWriteFile.callCount, 0); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment