diff --git a/.gitignore b/.gitignore index 5635acd8f..7b7044a9b 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ coverage/ node_modules/ src/**/*.js test/*.js +~test/jest.config.js test/tests/**/.eslintrc* diff --git a/docs/Testing.md b/docs/Testing.md index f7178a55f..3b28d335e 100644 --- a/docs/Testing.md +++ b/docs/Testing.md @@ -31,10 +31,14 @@ npm run test:end-to-end End-to-end tests that execute the `bin/tslint-to-eslint` command and validate outputs are generated from the directories in `test/tests/`. Each directory there contains: +- `test.ts`: Test file that runs `createTests(__dirname);` to set up tests in that directory - `.eslintrc.json`: `.gitignore`d output from the most recent test run - `expected.json`: Expected output ESLint configuration -- `stderr.txt`: Any output written to the process `stderr` -- `stdout.txt`: Any output written to the process `stdout` +- `stderr.txt`: Expected output written to the process `stderr` +- `stdout.txt`: Expected output written to the process `stdout` - `tslint.json`: Original TSLint configuration file to convert Within each directory, a test suite will execute `bin/tslint-to-eslint` and validate the outputs match what's on disk. + +Use `npm run test:end-to-end:accept` to overwrite the expected contents of files with what is actually written. +These behave similarly to updating snapshots in snapshot testing. diff --git a/test/createTestArgs.ts b/test/createTestArgs.ts index a4eac8f0a..02e528c00 100644 --- a/test/createTestArgs.ts +++ b/test/createTestArgs.ts @@ -6,14 +6,10 @@ const readdir = promisify(fs.readdir); export const createTestArgs = async (cwd: string) => { const items = new Set(await readdir(cwd)); - const flags = [ - "--config", - path.join(cwd, ".eslintrc.json"), - "--tslint", - path.join(cwd, "tslint.json"), - ]; + const flags = ["--config", path.join(cwd, ".eslintrc.json")]; if (items.has("tslint.json")) { + flags.push("--tslint", path.join(cwd, "tslint.json")); } return flags.map(flag => `"${flag}"`).join(" "); diff --git a/test/createTests.ts b/test/createTests.ts index c7d2b7afc..c17971379 100644 --- a/test/createTests.ts +++ b/test/createTests.ts @@ -10,19 +10,24 @@ import { assertFileContents } from "./expectFileContains"; const exec = promisify(cp.exec); const readFile = promisify(fs.readFile); -export const createTests = (testName: string, accept: boolean) => { - const cwd = path.join(__dirname, "tests", testName); +export const createTests = (cwd: string) => { + 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(); - return () => { + describe(testName, () => { let result: PromiseValue>; beforeAll(async () => { // Arrange const args = await createTestArgs(cwd); // Act - result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`); + try { + result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`); + } catch (error) { + result = error; + } }); test("configuration output", async () => { @@ -33,8 +38,6 @@ export const createTests = (testName: string, accept: boolean) => { ); }); - // test("info log output", () => {}); - test("stderr", async () => { await assertFileContents(cwdPath("stderr.txt"), result.stderr, accept); }); @@ -42,5 +45,5 @@ export const createTests = (testName: string, accept: boolean) => { test("stdout", async () => { await assertFileContents(cwdPath("stdout.txt"), result.stdout, accept); }); - }; + }); }; diff --git a/test/jest.config.js b/test/jest.config.js index 922024be5..7b1c51589 100644 --- a/test/jest.config.js +++ b/test/jest.config.js @@ -1,5 +1,5 @@ module.exports = { moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"], - testRegex: "test/runEndToEndTests.ts", + testRegex: "test(.*)\\test\\.ts$", testEnvironment: "node", }; diff --git a/test/runEndToEndTests.ts b/test/runEndToEndTests.ts deleted file mode 100644 index f6349ff48..000000000 --- a/test/runEndToEndTests.ts +++ /dev/null @@ -1,12 +0,0 @@ -import * as fs from "fs"; -import * as path from "path"; - -import { createTests } from "./createTests"; - -const testNames = fs.readdirSync(path.join(__dirname, "tests")); - -const accept = "acceptTestChanges" in globalThis; - -for (const testName of testNames) { - describe(testName, createTests(testName, accept)); -} diff --git a/test/tests/missing tslint.json/expected.json b/test/tests/missing tslint.json/expected.json new file mode 100644 index 000000000..9e8bc631d --- /dev/null +++ b/test/tests/missing tslint.json/expected.json @@ -0,0 +1,50 @@ +{ + "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", + "@typescript-eslint/member-ordering": "off", + "@typescript-eslint/no-explicit-any": "off", + "@typescript-eslint/no-param-reassign": "off", + "@typescript-eslint/no-parameter-properties": "off", + "@typescript-eslint/no-use-before-declare": "off", + "@typescript-eslint/promise-function-async": "off", + "@typescript-eslint/unbound-method": "off", + "arrow-body-style": "off", + "default-case": "off", + "linebreak-style": "off", + "no-bitwise": "off", + "no-empty": "off", + "no-empty-functions": "off", + "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/missing tslint.json/stderr.txt b/test/tests/missing tslint.json/stderr.txt new file mode 100644 index 000000000..f248378ec --- /dev/null +++ b/test/tests/missing tslint.json/stderr.txt @@ -0,0 +1,5 @@ +❌ 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 diff --git a/test/tests/missing tslint.json/stdout.txt b/test/tests/missing tslint.json/stdout.txt new file mode 100644 index 000000000..e69de29bb diff --git a/test/tests/missing tslint.json/test.ts b/test/tests/missing tslint.json/test.ts new file mode 100644 index 000000000..ba6db888f --- /dev/null +++ b/test/tests/missing tslint.json/test.ts @@ -0,0 +1,3 @@ +import { createTests } from "../../createTests"; + +createTests(__dirname); diff --git a/test/tests/standalone tslint.json/.eslintrc.json b/test/tests/standalone tslint.json/.eslintrc.json index 495475d49..9e8bc631d 100644 --- a/test/tests/standalone tslint.json/.eslintrc.json +++ b/test/tests/standalone tslint.json/.eslintrc.json @@ -47,4 +47,4 @@ } ] } -} \ No newline at end of file +} diff --git a/test/tests/standalone tslint.json/expected.json b/test/tests/standalone tslint.json/expected.json index 495475d49..9e8bc631d 100644 --- a/test/tests/standalone tslint.json/expected.json +++ b/test/tests/standalone tslint.json/expected.json @@ -47,4 +47,4 @@ } ] } -} \ No newline at end of file +} diff --git a/test/tests/standalone tslint.json/stdout.txt b/test/tests/standalone tslint.json/stdout.txt index fcf5848f3..fd7041491 100644 --- a/test/tests/standalone tslint.json/stdout.txt +++ b/test/tests/standalone tslint.json/stdout.txt @@ -1,3 +1,4 @@ -✨ 17 rules replaced with their ESLint equivalents. ✨ -️👀 2 rules do not yet have ESLint equivalents; defaulting to eslint-plugin-tslint. 👀 -✅ All is well! ✅ +✨ 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 diff --git a/test/tests/standalone tslint.json/test.ts b/test/tests/standalone tslint.json/test.ts new file mode 100644 index 000000000..ba6db888f --- /dev/null +++ b/test/tests/standalone tslint.json/test.ts @@ -0,0 +1,3 @@ +import { createTests } from "../../createTests"; + +createTests(__dirname); diff --git a/tsconfig.json b/tsconfig.json index 2f8598069..ad0c7da74 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -19,5 +19,6 @@ "strictNullChecks": true, "strictPropertyInitialization": true, "target": "esnext" - } + }, + "exclude": ["test/tests/**/*"] }