Skip to content

Commit 759411b

Browse files
author
Josh Goldberg
authored
Added tests for stderr from missing tslint.json files (#127)
1 parent 8df61df commit 759411b

File tree

15 files changed

+89
-34
lines changed

15 files changed

+89
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ coverage/
66
node_modules/
77
src/**/*.js
88
test/*.js
9+
~test/jest.config.js
910
test/tests/**/.eslintrc*

docs/Testing.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,14 @@ npm run test:end-to-end
3131
End-to-end tests that execute the `bin/tslint-to-eslint` command and validate outputs are generated from the directories in `test/tests/`.
3232
Each directory there contains:
3333

34+
- `test.ts`: Test file that runs `createTests(__dirname);` to set up tests in that directory
3435
- `.eslintrc.json`: `.gitignore`d output from the most recent test run
3536
- `expected.json`: Expected output ESLint configuration
36-
- `stderr.txt`: Any output written to the process `stderr`
37-
- `stdout.txt`: Any output written to the process `stdout`
37+
- `stderr.txt`: Expected output written to the process `stderr`
38+
- `stdout.txt`: Expected output written to the process `stdout`
3839
- `tslint.json`: Original TSLint configuration file to convert
3940

4041
Within each directory, a test suite will execute `bin/tslint-to-eslint` and validate the outputs match what's on disk.
42+
43+
Use `npm run test:end-to-end:accept` to overwrite the expected contents of files with what is actually written.
44+
These behave similarly to updating snapshots in snapshot testing.

test/createTestArgs.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,10 @@ const readdir = promisify(fs.readdir);
66

77
export const createTestArgs = async (cwd: string) => {
88
const items = new Set(await readdir(cwd));
9-
const flags = [
10-
"--config",
11-
path.join(cwd, ".eslintrc.json"),
12-
"--tslint",
13-
path.join(cwd, "tslint.json"),
14-
];
9+
const flags = ["--config", path.join(cwd, ".eslintrc.json")];
1510

1611
if (items.has("tslint.json")) {
12+
flags.push("--tslint", path.join(cwd, "tslint.json"));
1713
}
1814

1915
return flags.map(flag => `"${flag}"`).join(" ");

test/createTests.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,24 @@ import { assertFileContents } from "./expectFileContains";
1010
const exec = promisify(cp.exec);
1111
const readFile = promisify(fs.readFile);
1212

13-
export const createTests = (testName: string, accept: boolean) => {
14-
const cwd = path.join(__dirname, "tests", testName);
13+
export const createTests = (cwd: string) => {
14+
const testName = path.basename(cwd);
15+
const accept = "acceptTestChanges" in globalThis;
1516
const cwdPath = (fileName: string) => path.join(cwd, fileName);
1617
const readTestFile = async (fileName: string) => (await readFile(cwdPath(fileName))).toString();
1718

18-
return () => {
19+
describe(testName, () => {
1920
let result: PromiseValue<ReturnType<typeof exec>>;
2021
beforeAll(async () => {
2122
// Arrange
2223
const args = await createTestArgs(cwd);
2324

2425
// Act
25-
result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`);
26+
try {
27+
result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`);
28+
} catch (error) {
29+
result = error;
30+
}
2631
});
2732

2833
test("configuration output", async () => {
@@ -33,14 +38,12 @@ export const createTests = (testName: string, accept: boolean) => {
3338
);
3439
});
3540

36-
// test("info log output", () => {});
37-
3841
test("stderr", async () => {
3942
await assertFileContents(cwdPath("stderr.txt"), result.stderr, accept);
4043
});
4144

4245
test("stdout", async () => {
4346
await assertFileContents(cwdPath("stdout.txt"), result.stdout, accept);
4447
});
45-
};
48+
});
4649
};

test/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module.exports = {
22
moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "node"],
3-
testRegex: "test/runEndToEndTests.ts",
3+
testRegex: "test(.*)\\test\\.ts$",
44
testEnvironment: "node",
55
};

test/runEndToEndTests.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"env": {
3+
"es6": true,
4+
"node": true
5+
},
6+
"parser": "@typescript-eslint/parser",
7+
"parserOptions": {
8+
"project": "tsconfig.json",
9+
"sourceType": "module"
10+
},
11+
"plugins": [
12+
"@typescript-eslint",
13+
"@typescript-eslint/tslint"
14+
],
15+
"rules": {
16+
"@typescript-eslint/array-type": "error",
17+
"@typescript-eslint/interface-name-prefix": "error",
18+
"@typescript-eslint/member-ordering": "off",
19+
"@typescript-eslint/no-explicit-any": "off",
20+
"@typescript-eslint/no-param-reassign": "off",
21+
"@typescript-eslint/no-parameter-properties": "off",
22+
"@typescript-eslint/no-use-before-declare": "off",
23+
"@typescript-eslint/promise-function-async": "off",
24+
"@typescript-eslint/unbound-method": "off",
25+
"arrow-body-style": "off",
26+
"default-case": "off",
27+
"linebreak-style": "off",
28+
"no-bitwise": "off",
29+
"no-empty": "off",
30+
"no-empty-functions": "off",
31+
"no-magic-numbers": "off",
32+
"prefer-template": "off",
33+
"@typescript-eslint/tslint/config": [
34+
"error",
35+
{
36+
"rules": {
37+
"no-implicit-dependencies": [
38+
true,
39+
"dev"
40+
],
41+
"strict-boolean-expressions": [
42+
true,
43+
"allow-boolean-or-undefined",
44+
"allow-number"
45+
]
46+
}
47+
}
48+
]
49+
}
50+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
❌ Could not start tslint-to-eslint: ❌
2+
Command failed: tslint --print-config "./tslint.json"
3+
Could not find configuration path. Try passing a --config to your tslint.json.
4+

5+


test/tests/missing tslint.json/stdout.txt

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createTests } from "../../createTests";
2+
3+
createTests(__dirname);

test/tests/standalone tslint.json/.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
}
4848
]
4949
}
50-
}
50+
}

test/tests/standalone tslint.json/expected.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@
4747
}
4848
]
4949
}
50-
}
50+
}
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
✨ 17 rules replaced with their ESLint equivalents. ✨
2-
️👀 2 rules do not yet have ESLint equivalents; defaulting to eslint-plugin-tslint. 👀
3-
✅ All is well! ✅
1+
✨ 17 rules replaced with their ESLint equivalents. ✨
2+
️👀 2 rules do not yet have ESLint equivalents; defaulting to eslint-plugin-tslint. 👀
3+
✅ All is well! ✅
4+

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { createTests } from "../../createTests";
2+
3+
createTests(__dirname);

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
"strictNullChecks": true,
2020
"strictPropertyInitialization": true,
2121
"target": "esnext"
22-
}
22+
},
23+
"exclude": ["test/tests/**/*"]
2324
}

0 commit comments

Comments
 (0)