From fd7b813569e17e3799ff936bffaf5fd3c674a763 Mon Sep 17 00:00:00 2001 From: Josh Goldberg Date: Tue, 30 Jul 2019 06:11:46 -0400 Subject: [PATCH] Added options for CLI flags to end-to-end tests --- package.json | 4 +- test/createTestArgs.ts | 4 +- test/createTests.ts | 59 +++++++++++++------ test/tests/custom eslint path/eslintrc.js | 31 ++++++++++ test/tests/custom eslint path/expected.json | 31 ++++++++++ test/tests/custom eslint path/stderr.txt | 4 ++ test/tests/custom eslint path/stdout.txt | 0 test/tests/custom eslint path/test.ts | 8 +++ test/tests/custom eslint path/tslint.json | 35 +++++++++++ test/tests/custom package path/expected.json | 31 ++++++++++ .../tests/custom package path/my-package.json | 24 ++++++++ test/tests/custom package path/stderr.txt | 4 ++ test/tests/custom package path/stdout.txt | 0 test/tests/custom package path/test.ts | 5 ++ test/tests/custom package path/tslint.json | 35 +++++++++++ test/tests/missing tslint.json/stderr.txt | 9 ++- test/tests/standalone tslint.json/stderr.txt | 4 ++ test/tests/standalone tslint.json/stdout.txt | 4 -- 18 files changed, 262 insertions(+), 30 deletions(-) create mode 100644 test/tests/custom eslint path/eslintrc.js create mode 100644 test/tests/custom eslint path/expected.json create mode 100644 test/tests/custom eslint path/stderr.txt create mode 100644 test/tests/custom eslint path/stdout.txt create mode 100644 test/tests/custom eslint path/test.ts create mode 100644 test/tests/custom eslint path/tslint.json create mode 100644 test/tests/custom package path/expected.json create mode 100644 test/tests/custom package path/my-package.json create mode 100644 test/tests/custom package path/stderr.txt create mode 100644 test/tests/custom package path/stdout.txt create mode 100644 test/tests/custom package path/test.ts create mode 100644 test/tests/custom package path/tslint.json diff --git a/package.json b/package.json index 0b3f7475d..1d9912f8b 100644 --- a/package.json +++ b/package.json @@ -61,8 +61,8 @@ "prettier": "prettier \"./src/*.{js,json,ts,xml,yaml}\" \"./src/**/*.{js,json,ts,xml,yaml}\" --ignore-path .prettierignore", "prettier:write": "npm run prettier -- --write", "test:unit": "jest", - "test:end-to-end": "jest --config=test/jest.config.js", - "test:end-to-end:accept": "jest --config=test/jest.config.js --globals=\"{\\\"acceptTestChanges\\\": true }\"", + "test:end-to-end": "jest --config=test/jest.config.js --runInBand", + "test:end-to-end:accept": "jest --config=test/jest.config.js --globals=\"{\\\"acceptTestChanges\\\": true }\" --runInBand", "tsc": "tsc" }, "version": "0.2.2" diff --git a/test/createTestArgs.ts b/test/createTestArgs.ts index 02e528c00..84efb2306 100644 --- a/test/createTestArgs.ts +++ b/test/createTestArgs.ts @@ -4,7 +4,7 @@ import { promisify } from "util"; const readdir = promisify(fs.readdir); -export const createTestArgs = async (cwd: string) => { +export const createTestArgs = async (cwd: string, extraArgs: string[]) => { const items = new Set(await readdir(cwd)); const flags = ["--config", path.join(cwd, ".eslintrc.json")]; @@ -12,5 +12,5 @@ export const createTestArgs = async (cwd: string) => { flags.push("--tslint", path.join(cwd, "tslint.json")); } - return flags.map(flag => `"${flag}"`).join(" "); + return [...flags.map(flag => `"${flag}"`).join(" "), ...extraArgs]; }; diff --git a/test/createTests.ts b/test/createTests.ts index c17971379..d1b228807 100644 --- a/test/createTests.ts +++ b/test/createTests.ts @@ -4,45 +4,70 @@ import * as path from "path"; import { promisify } from "util"; import { createTestArgs } from "./createTestArgs"; -import { PromiseValue } from "../src/utils"; import { assertFileContents } from "./expectFileContains"; const exec = promisify(cp.exec); const readFile = promisify(fs.readFile); -export const createTests = (cwd: string) => { +export type TestSettings = { + /** + * Expected location of the output ESLint configuration file. + */ + eslint?: string; + + /** + * Any extra commands to pass to the CLI. + */ + extraArgs?: string[]; +}; + +jest.setTimeout(10000); + +const act = async (testArgs: string[]) => { + try { + return await exec(`ts-node bin/tslint-to-eslint-config ${testArgs.join(" ")}`); + } catch (error) { + return error; + } +}; + +export const createTests = ( + cwd: string, + { eslint = "./.eslintrc.json", extraArgs = [] }: TestSettings = {}, +) => { const testName = path.basename(cwd); const accept = "acceptTestChanges" in globalThis; const cwdPath = (fileName: string) => path.join(cwd, fileName); const readTestFile = async (fileName: string) => (await readFile(cwdPath(fileName))).toString(); describe(testName, () => { - let result: PromiseValue>; - beforeAll(async () => { + test("configuration output", async () => { // Arrange - const args = await createTestArgs(cwd); + const testArgs = await createTestArgs(cwd, extraArgs); // Act - try { - result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`); - } catch (error) { - result = error; - } - }); + await act(testArgs); - test("configuration output", async () => { - await assertFileContents( - cwdPath("expected.json"), - await readTestFile(".eslintrc.json"), - accept, - ); + await assertFileContents(cwdPath("expected.json"), await readTestFile(eslint), accept); }); test("stderr", async () => { + // Arrange + const testArgs = await createTestArgs(cwd, extraArgs); + + // Act + const result = await act(testArgs); + await assertFileContents(cwdPath("stderr.txt"), result.stderr, accept); }); test("stdout", async () => { + // Arrange + const testArgs = await createTestArgs(cwd, extraArgs); + + // Act + const result = await act(testArgs); + await assertFileContents(cwdPath("stdout.txt"), result.stdout, accept); }); }); diff --git a/test/tests/custom eslint path/eslintrc.js b/test/tests/custom eslint path/eslintrc.js new file mode 100644 index 000000000..06585f8f1 --- /dev/null +++ b/test/tests/custom eslint path/eslintrc.js @@ -0,0 +1,31 @@ +module.exports = { + env: { + es6: true, + node: true, + }, + parser: "@typescript-eslint/parser", + parserOptions: { + project: "tsconfig.json", + sourceType: "module", + }, + plugins: ["@typescript-eslint", "@typescript-eslint/tslint"], + rules: { + "@typescript-eslint/array-type": "error", + "@typescript-eslint/interface-name-prefix": "error", + "no-magic-numbers": "off", + "prefer-template": "off", + "@typescript-eslint/tslint/config": [ + "error", + { + rules: { + "no-implicit-dependencies": [true, "dev"], + "strict-boolean-expressions": [ + true, + "allow-boolean-or-undefined", + "allow-number", + ], + }, + }, + ], + }, +}; diff --git a/test/tests/custom eslint path/expected.json b/test/tests/custom eslint path/expected.json new file mode 100644 index 000000000..06585f8f1 --- /dev/null +++ b/test/tests/custom eslint path/expected.json @@ -0,0 +1,31 @@ +module.exports = { + env: { + es6: true, + node: true, + }, + parser: "@typescript-eslint/parser", + parserOptions: { + project: "tsconfig.json", + sourceType: "module", + }, + plugins: ["@typescript-eslint", "@typescript-eslint/tslint"], + rules: { + "@typescript-eslint/array-type": "error", + "@typescript-eslint/interface-name-prefix": "error", + "no-magic-numbers": "off", + "prefer-template": "off", + "@typescript-eslint/tslint/config": [ + "error", + { + rules: { + "no-implicit-dependencies": [true, "dev"], + "strict-boolean-expressions": [ + true, + "allow-boolean-or-undefined", + "allow-number", + ], + }, + }, + ], + }, +}; diff --git a/test/tests/custom eslint path/stderr.txt b/test/tests/custom eslint path/stderr.txt new file mode 100644 index 000000000..a1d984416 --- /dev/null +++ b/test/tests/custom eslint path/stderr.txt @@ -0,0 +1,4 @@ +❌ Could not start tslint-to-eslint: ❌ +Command failed: tslint --print-config "./tslint.json" +Could not find configuration path. Try passing a --config to your tslint.json. + diff --git a/test/tests/custom eslint path/stdout.txt b/test/tests/custom eslint path/stdout.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/tests/custom eslint path/test.ts b/test/tests/custom eslint path/test.ts new file mode 100644 index 000000000..31c11e9b1 --- /dev/null +++ b/test/tests/custom eslint path/test.ts @@ -0,0 +1,8 @@ +import { createTests } from "../../createTests"; + +const eslint = "./eslintrc.js"; + +createTests(__dirname, { + eslint, + extraArgs: ["--eslint", eslint], +}); \ No newline at end of file diff --git a/test/tests/custom eslint path/tslint.json b/test/tests/custom eslint path/tslint.json new file mode 100644 index 000000000..4a2aa47f4 --- /dev/null +++ b/test/tests/custom eslint path/tslint.json @@ -0,0 +1,35 @@ +{ + "rules": { + "array-type": [true, "array"], + "arrow-return-shorthand": false, + "completed-docs": false, + "comment-format": false, + "file-name-casing": false, + "linebreak-style": false, + "interface-name": [true, "never-prefix"], + "member-ordering": false, + "newline-before-return": false, + "no-any": false, + "no-bitwise": false, + "no-empty": false, + "no-magic-numbers": false, + "no-import-side-effect": false, + "no-implicit-dependencies": [true, "dev"], + "no-null-keyword": false, + "no-parameter-reassignment": false, + "no-parameter-properties": false, + "no-submodule-imports": false, + "no-unbound-method": false, + "no-unused-variable": false, + "no-use-before-declare": false, + "prefer-conditional-expression": false, + "prefer-method-signature": false, + "prefer-switch": false, + "prefer-template": false, + "promise-function-async": false, + "strict-boolean-expressions": [true, "allow-boolean-or-undefined", "allow-number"], + "switch-default": false, + "switch-final-break": false, + "typedef": false + } +} diff --git a/test/tests/custom package path/expected.json b/test/tests/custom package path/expected.json new file mode 100644 index 000000000..06585f8f1 --- /dev/null +++ b/test/tests/custom package path/expected.json @@ -0,0 +1,31 @@ +module.exports = { + env: { + es6: true, + node: true, + }, + parser: "@typescript-eslint/parser", + parserOptions: { + project: "tsconfig.json", + sourceType: "module", + }, + plugins: ["@typescript-eslint", "@typescript-eslint/tslint"], + rules: { + "@typescript-eslint/array-type": "error", + "@typescript-eslint/interface-name-prefix": "error", + "no-magic-numbers": "off", + "prefer-template": "off", + "@typescript-eslint/tslint/config": [ + "error", + { + rules: { + "no-implicit-dependencies": [true, "dev"], + "strict-boolean-expressions": [ + true, + "allow-boolean-or-undefined", + "allow-number", + ], + }, + }, + ], + }, +}; diff --git a/test/tests/custom package path/my-package.json b/test/tests/custom package path/my-package.json new file mode 100644 index 000000000..22a1726cf --- /dev/null +++ b/test/tests/custom package path/my-package.json @@ -0,0 +1,24 @@ +{ + "dependencies": { + "chalk": "2.4.2", + "commander": "2.20.0", + "tslint": "5.18.0", + "typescript": "3.5.3" + }, + "devDependencies": { + "@babel/core": "7.5.5", + "@babel/preset-env": "7.5.5", + "@babel/preset-typescript": "7.3.3", + "@types/jest": "24.0.15", + "@types/node": "12.6.8", + "@typescript-eslint/eslint-plugin": "1.12.0", + "@typescript-eslint/parser": "1.12.0", + "babel-jest": "24.8.0", + "eslint": "6.0.1", + "eslint-config-airbnb": "17.1.1", + "eslint-config-airbnb-base": "13.2.0", + "eslint-config-prettier": "6.0.0", + "eslint-plugin-import": "2.18.2" + }, + "version": "1.2.3" +} diff --git a/test/tests/custom package path/stderr.txt b/test/tests/custom package path/stderr.txt new file mode 100644 index 000000000..a1d984416 --- /dev/null +++ b/test/tests/custom package path/stderr.txt @@ -0,0 +1,4 @@ +❌ Could not start tslint-to-eslint: ❌ +Command failed: tslint --print-config "./tslint.json" +Could not find configuration path. Try passing a --config to your tslint.json. + diff --git a/test/tests/custom package path/stdout.txt b/test/tests/custom package path/stdout.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/tests/custom package path/test.ts b/test/tests/custom package path/test.ts new file mode 100644 index 000000000..f6cb1d22a --- /dev/null +++ b/test/tests/custom package path/test.ts @@ -0,0 +1,5 @@ +import { createTests } from "../../createTests"; + +createTests(__dirname, { + extraArgs: ["--package", "my-package.json"], +}); \ No newline at end of file diff --git a/test/tests/custom package path/tslint.json b/test/tests/custom package path/tslint.json new file mode 100644 index 000000000..4a2aa47f4 --- /dev/null +++ b/test/tests/custom package path/tslint.json @@ -0,0 +1,35 @@ +{ + "rules": { + "array-type": [true, "array"], + "arrow-return-shorthand": false, + "completed-docs": false, + "comment-format": false, + "file-name-casing": false, + "linebreak-style": false, + "interface-name": [true, "never-prefix"], + "member-ordering": false, + "newline-before-return": false, + "no-any": false, + "no-bitwise": false, + "no-empty": false, + "no-magic-numbers": false, + "no-import-side-effect": false, + "no-implicit-dependencies": [true, "dev"], + "no-null-keyword": false, + "no-parameter-reassignment": false, + "no-parameter-properties": false, + "no-submodule-imports": false, + "no-unbound-method": false, + "no-unused-variable": false, + "no-use-before-declare": false, + "prefer-conditional-expression": false, + "prefer-method-signature": false, + "prefer-switch": false, + "prefer-template": false, + "promise-function-async": false, + "strict-boolean-expressions": [true, "allow-boolean-or-undefined", "allow-number"], + "switch-default": false, + "switch-final-break": false, + "typedef": false + } +} diff --git a/test/tests/missing tslint.json/stderr.txt b/test/tests/missing tslint.json/stderr.txt index f248378ec..a1d984416 100644 --- a/test/tests/missing tslint.json/stderr.txt +++ b/test/tests/missing tslint.json/stderr.txt @@ -1,5 +1,4 @@ -❌ Could not start tslint-to-eslint: ❌ -Command failed: tslint --print-config "./tslint.json" -Could not find configuration path. Try passing a --config to your tslint.json. - - \ No newline at end of file +❌ Could not start tslint-to-eslint: ❌ +Command failed: tslint --print-config "./tslint.json" +Could not find configuration path. Try passing a --config to your tslint.json. + diff --git a/test/tests/standalone tslint.json/stderr.txt b/test/tests/standalone tslint.json/stderr.txt index e69de29bb..a1d984416 100644 --- a/test/tests/standalone tslint.json/stderr.txt +++ b/test/tests/standalone tslint.json/stderr.txt @@ -0,0 +1,4 @@ +❌ Could not start tslint-to-eslint: ❌ +Command failed: tslint --print-config "./tslint.json" +Could not find configuration path. Try passing a --config to your tslint.json. + diff --git a/test/tests/standalone tslint.json/stdout.txt b/test/tests/standalone tslint.json/stdout.txt index fd7041491..e69de29bb 100644 --- a/test/tests/standalone tslint.json/stdout.txt +++ b/test/tests/standalone tslint.json/stdout.txt @@ -1,4 +0,0 @@ -✨ 17 rules replaced with their ESLint equivalents. ✨ -️👀 2 rules do not yet have ESLint equivalents; defaulting to eslint-plugin-tslint. 👀 -✅ All is well! ✅ - \ No newline at end of file