Skip to content

Commit

Permalink
Chore: refactor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
snitin315 committed Apr 24, 2021
1 parent adec7fc commit 6f8815f
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 83 deletions.
9 changes: 4 additions & 5 deletions lib/init/config-initializer.js
Expand Up @@ -38,10 +38,9 @@ const debug = require("debug")("eslint:config-initializer");
* Create .eslintrc file in the current working directory
* @param {Object} config object that contains user's answers
* @param {string} format The file format to write to.
* @param {Object} options filePath: write file here, startDir: Starting directory for searching pkgJSON. Handy for writing tests to avoid reading/writing root files.
* @returns {void}
*/
function writeFile(config, format, options = {}) {
function writeFile(config, format) {

// default is .js
let extname = ".js";
Expand All @@ -51,7 +50,7 @@ function writeFile(config, format, options = {}) {
} else if (format === "JSON") {
extname = ".json";
} else if (format === "JavaScript") {
const pkgJSONPath = npmUtils.findPackageJson(options.startDir || process.cwd());
const pkgJSONPath = npmUtils.findPackageJson();

if (pkgJSONPath) {
const pkgJSONContents = JSON.parse(fs.readFileSync(pkgJSONPath, "utf8"));
Expand All @@ -66,8 +65,8 @@ function writeFile(config, format, options = {}) {

delete config.installedESLint;

ConfigFile.write(config, options.filePath ? path.resolve(options.filePath, `.eslintrc${extname}`) : `./.eslintrc${extname}`);
log.info(`Successfully created .eslintrc${extname} file in ${options.filePath || process.cwd()}`);
ConfigFile.write(config, `./.eslintrc${extname}`);
log.info(`Successfully created .eslintrc${extname} file in ${process.cwd()}`);

if (installedESLint) {
log.info("ESLint was installed locally. We recommend using this local copy instead of your globally-installed copy.");
Expand Down
199 changes: 121 additions & 78 deletions tests/lib/init/config-initializer.js
Expand Up @@ -27,6 +27,7 @@ const proxyquire = require("proxyquire").noPreserveCache();
//------------------------------------------------------------------------------

let answers = {};
let pkgJSONContens = {};

describe("configInitializer", () => {

Expand Down Expand Up @@ -207,84 +208,6 @@ describe("configInitializer", () => {
});
});

describe("writeFile()", () => {
it("should create eslintrc.json", () => {
const config = init.processAnswers(answers);
const filePath = path.resolve(__dirname, ".eslintrc.json");

init.writeFile(config, answers.format, { filePath: __dirname });

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

it("should create eslintrc.js", () => {
answers.format = "JavaScript";

const config = init.processAnswers(answers);
const filePath = path.resolve(__dirname, ".eslintrc.js");

init.writeFile(config, answers.format, { filePath: __dirname });

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

it("should create eslintrc.yml", () => {
answers.format = "YAML";

const config = init.processAnswers(answers);
const filePath = path.resolve(__dirname, ".eslintrc.yml");

init.writeFile(config, answers.format, { filePath: __dirname });

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

// For https://github.com/eslint/eslint/issues/14137
it("should create eslintrc.cjs", () => {
answers.format = "JavaScript";

// create package.json with "type": "module"
const pkgJSONContens = { type: "module" };

fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens));

const config = init.processAnswers(answers);
const filePath = path.resolve(__dirname, ".eslintrc.cjs");

init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname });

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(path.resolve(__dirname, "package.json"));
});

it("should create eslintrc.json even with type: 'module'", () => {
answers.format = "JSON";

// create package.json with "type": "module"
const pkgJSONContens = { type: "module" };

fs.writeFileSync(path.resolve(__dirname, "package.json"), JSON.stringify(pkgJSONContens));

const config = init.processAnswers(answers);
const filePath = path.resolve(__dirname, ".eslintrc.json");

init.writeFile(config, answers.format, { filePath: __dirname, startDir: __dirname });

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(path.resolve(__dirname, "package.json"));
});
});

describe("guide", () => {
it("should support the google style guide", () => {
const config = { extends: "google" };
Expand Down Expand Up @@ -525,4 +448,124 @@ describe("configInitializer", () => {
});
});
});

describe("writeFile()", () => {

beforeEach(() => {
answers = {
purpose: "style",
source: "prompt",
extendDefault: true,
indent: 2,
quotes: "single",
linebreak: "unix",
semi: true,
moduleType: "esm",
es6Globals: true,
env: ["browser"],
format: "JSON"
};

pkgJSONContens = {
name: "config-initializer",
version: "1.0.0"
};
});

afterEach(() => {
process.chdir(originalDir);
});

it("should create eslintrc.json", () => {
process.chdir(fixtureDir);

const config = init.processAnswers(answers);
const filePath = path.resolve(process.cwd(), ".eslintrc.json");

fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

it("should create eslintrc.js", () => {
answers.format = "JavaScript";

process.chdir(fixtureDir);

const config = init.processAnswers(answers);
const filePath = path.resolve(process.cwd(), ".eslintrc.js");

fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

it("should create eslintrc.yml", () => {
answers.format = "YAML";

process.chdir(fixtureDir);

const config = init.processAnswers(answers);
const filePath = path.resolve(process.cwd(), ".eslintrc.yml");

fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens));

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
});

// For https://github.com/eslint/eslint/issues/14137
it("should create eslintrc.cjs", () => {
answers.format = "JavaScript";

process.chdir(fixtureDir);

// create package.json with "type": "module"
pkgJSONContens.type = "module";

fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens));

const config = init.processAnswers(answers);
const filePath = path.resolve(process.cwd(), ".eslintrc.cjs");

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(path.resolve(process.cwd(), "package.json"));
});

it("should create eslintrc.json even with type: 'module'", () => {
answers.format = "JSON";

process.chdir(fixtureDir);

// create package.json with "type": "module"
pkgJSONContens.type = "module";

fs.writeFileSync(path.resolve(process.cwd(), "package.json"), JSON.stringify(pkgJSONContens));

const config = init.processAnswers(answers);
const filePath = path.resolve(process.cwd(), ".eslintrc.json");

init.writeFile(config, answers.format);

assert.isTrue(fs.existsSync(filePath));

fs.unlinkSync(filePath);
fs.unlinkSync(path.resolve(process.cwd(), "package.json"));
});
});
});

0 comments on commit 6f8815f

Please sign in to comment.