Skip to content

Instantly share code, notes, and snippets.

@mmanishh
Last active February 9, 2021 13:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmanishh/b489cae69946bd4c0bd77218a6733e66 to your computer and use it in GitHub Desktop.
Save mmanishh/b489cae69946bd4c0bd77218a6733e66 to your computer and use it in GitHub Desktop.
var fs = require('fs');
var expect = require('chai').expect;
var { createDirIfNotExists, writeJSON } = require('../writeFile.js');
describe('createDirIfNotExists', function () {
context('without arguments', function () {
it('should throw error if path is null', async function () {
return createDirIfNotExists()
.then()
.catch(function (err) {
// add an assertion to check the error
expect(function () { throw err })
.to.throw(Error, "Path should not be null");
})
});
})
context('with arguments', function () {
it('should return path of dir that is passed', async function () {
let path = "Users/user/codes/sarojtask/a/b/c";
let dirPath = await createDirIfNotExists(path);
expect(dirPath).to.be.equal(path)
expect(fs.existsSync(path)).to.be.true
});
})
});
describe('writeJSON', function () {
context('with wrong arguments', function () {
it('should throw error if data is not object', async function () {
return writeJSON("/Users/user/codes/sarojtask/a/tes.json", "name")
.catch(function (err) {
// add an assertion to check the error
expect(function () { throw err })
.to.throw(Error, "Invalid data");
})
});
})
context('with correct arguments', function () {
it('should create a file under passed dir path', async function () {
let filePath = "/Users/user/codes/sarojtask/a/b/c/test.json";
writeJSON(filePath, { name: "test" }).then(result => {
expect(fs.existsSync(filePath)).to.be.true
})
});
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment