Skip to content

Commit

Permalink
feat: add test-util to append data to file
Browse files Browse the repository at this point in the history
  • Loading branch information
hemal7735 committed Jan 21, 2019
1 parent 9b6f267 commit d4ba95a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 2 deletions.
30 changes: 29 additions & 1 deletion test/test-utils.test.js
@@ -1,6 +1,8 @@
"use strict";

const { extractHash } = require("./testUtils");
const { extractHash, appendDataIfFileExists } = require("./testUtils");
const { writeFileSync, unlinkSync, readFileSync } = require("fs");
const { resolve } = require("path");

describe("extractHash functionality", () => {
it("should throw Error if there is empty string", () => {
Expand Down Expand Up @@ -101,3 +103,29 @@ Child ${config2Name}:
expect(hashInfo.config[1]).toEqual({ name: config2Name, hash: config2Hash });
});
});

describe("appendFile functionality", () => {
describe("positive test-cases", () => {
const junkFile = resolve(__dirname, "junkFile.js");

beforeEach(() => {
writeFileSync(junkFile, "");
});
afterEach(() => {
unlinkSync(junkFile);
});
it("should append data to file if file exists", () => {
const expectedData = "//junk comment";
appendDataIfFileExists(__dirname, junkFile, expectedData);
const actualData = readFileSync(junkFile).toString();

expect(actualData).toBe(expectedData);
});
});

describe("negative test-cases", () => {
it("should throw error if file does not exist", () => {
expect(() => appendDataIfFileExists(__dirname, "does-not-exist.js", "junk data")).toThrowError();
});
});
});
21 changes: 20 additions & 1 deletion test/testUtils.js
Expand Up @@ -148,4 +148,23 @@ function extractHash(stdout) {
return hashInfo;
}

module.exports = { run, runWatch, extractHash, extractSummary };
/*eslint valid-jsdoc: ["error", { "requireReturn": false }]*/
/**
*
* @param {*} testCase - testCase directory
* @param {*} file - file relative to testCase
* @param {*} data - data to append
* @throws - throw an Error if file does not exist
*/
function appendDataIfFileExists(testCase, file, data) {
const { existsSync, appendFileSync } = require("fs");

const filePath = path.resolve(testCase, file);
if (existsSync(filePath)) {
appendFileSync(filePath, data);
} else {
throw new Error(`Oops! ${filePath} does not exist!`);
}
}

module.exports = { run, runWatch, extractHash, extractSummary, appendDataIfFileExists };

0 comments on commit d4ba95a

Please sign in to comment.