Skip to content

Commit 5498caf

Browse files
author
Josh Goldberg
authored
Added options for CLI flags to end-to-end tests (#131)
1 parent 764a5b7 commit 5498caf

File tree

18 files changed

+262
-30
lines changed

18 files changed

+262
-30
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@
6161
"prettier": "prettier \"./src/*.{js,json,ts,xml,yaml}\" \"./src/**/*.{js,json,ts,xml,yaml}\" --ignore-path .prettierignore",
6262
"prettier:write": "npm run prettier -- --write",
6363
"test:unit": "jest",
64-
"test:end-to-end": "jest --config=test/jest.config.js",
65-
"test:end-to-end:accept": "jest --config=test/jest.config.js --globals=\"{\\\"acceptTestChanges\\\": true }\"",
64+
"test:end-to-end": "jest --config=test/jest.config.js --runInBand",
65+
"test:end-to-end:accept": "jest --config=test/jest.config.js --globals=\"{\\\"acceptTestChanges\\\": true }\" --runInBand",
6666
"tsc": "tsc"
6767
},
6868
"version": "0.2.2"

test/createTestArgs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { promisify } from "util";
44

55
const readdir = promisify(fs.readdir);
66

7-
export const createTestArgs = async (cwd: string) => {
7+
export const createTestArgs = async (cwd: string, extraArgs: string[]) => {
88
const items = new Set(await readdir(cwd));
99
const flags = ["--config", path.join(cwd, ".eslintrc.json")];
1010

1111
if (items.has("tslint.json")) {
1212
flags.push("--tslint", path.join(cwd, "tslint.json"));
1313
}
1414

15-
return flags.map(flag => `"${flag}"`).join(" ");
15+
return [...flags.map(flag => `"${flag}"`).join(" "), ...extraArgs];
1616
};

test/createTests.ts

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,70 @@ import * as path from "path";
44
import { promisify } from "util";
55

66
import { createTestArgs } from "./createTestArgs";
7-
import { PromiseValue } from "../src/utils";
87
import { assertFileContents } from "./expectFileContains";
98

109
const exec = promisify(cp.exec);
1110
const readFile = promisify(fs.readFile);
1211

13-
export const createTests = (cwd: string) => {
12+
export type TestSettings = {
13+
/**
14+
* Expected location of the output ESLint configuration file.
15+
*/
16+
eslint?: string;
17+
18+
/**
19+
* Any extra commands to pass to the CLI.
20+
*/
21+
extraArgs?: string[];
22+
};
23+
24+
jest.setTimeout(10000);
25+
26+
const act = async (testArgs: string[]) => {
27+
try {
28+
return await exec(`ts-node bin/tslint-to-eslint-config ${testArgs.join(" ")}`);
29+
} catch (error) {
30+
return error;
31+
}
32+
};
33+
34+
export const createTests = (
35+
cwd: string,
36+
{ eslint = "./.eslintrc.json", extraArgs = [] }: TestSettings = {},
37+
) => {
1438
const testName = path.basename(cwd);
1539
const accept = "acceptTestChanges" in globalThis;
1640
const cwdPath = (fileName: string) => path.join(cwd, fileName);
1741
const readTestFile = async (fileName: string) => (await readFile(cwdPath(fileName))).toString();
1842

1943
describe(testName, () => {
20-
let result: PromiseValue<ReturnType<typeof exec>>;
21-
beforeAll(async () => {
44+
test("configuration output", async () => {
2245
// Arrange
23-
const args = await createTestArgs(cwd);
46+
const testArgs = await createTestArgs(cwd, extraArgs);
2447

2548
// Act
26-
try {
27-
result = await exec(`ts-node bin/tslint-to-eslint-config ${args}`);
28-
} catch (error) {
29-
result = error;
30-
}
31-
});
49+
await act(testArgs);
3250

33-
test("configuration output", async () => {
34-
await assertFileContents(
35-
cwdPath("expected.json"),
36-
await readTestFile(".eslintrc.json"),
37-
accept,
38-
);
51+
await assertFileContents(cwdPath("expected.json"), await readTestFile(eslint), accept);
3952
});
4053

4154
test("stderr", async () => {
55+
// Arrange
56+
const testArgs = await createTestArgs(cwd, extraArgs);
57+
58+
// Act
59+
const result = await act(testArgs);
60+
4261
await assertFileContents(cwdPath("stderr.txt"), result.stderr, accept);
4362
});
4463

4564
test("stdout", async () => {
65+
// Arrange
66+
const testArgs = await createTestArgs(cwd, extraArgs);
67+
68+
// Act
69+
const result = await act(testArgs);
70+
4671
await assertFileContents(cwdPath("stdout.txt"), result.stdout, accept);
4772
});
4873
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
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: ["@typescript-eslint", "@typescript-eslint/tslint"],
12+
rules: {
13+
"@typescript-eslint/array-type": "error",
14+
"@typescript-eslint/interface-name-prefix": "error",
15+
"no-magic-numbers": "off",
16+
"prefer-template": "off",
17+
"@typescript-eslint/tslint/config": [
18+
"error",
19+
{
20+
rules: {
21+
"no-implicit-dependencies": [true, "dev"],
22+
"strict-boolean-expressions": [
23+
true,
24+
"allow-boolean-or-undefined",
25+
"allow-number",
26+
],
27+
},
28+
},
29+
],
30+
},
31+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
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: ["@typescript-eslint", "@typescript-eslint/tslint"],
12+
rules: {
13+
"@typescript-eslint/array-type": "error",
14+
"@typescript-eslint/interface-name-prefix": "error",
15+
"no-magic-numbers": "off",
16+
"prefer-template": "off",
17+
"@typescript-eslint/tslint/config": [
18+
"error",
19+
{
20+
rules: {
21+
"no-implicit-dependencies": [true, "dev"],
22+
"strict-boolean-expressions": [
23+
true,
24+
"allow-boolean-or-undefined",
25+
"allow-number",
26+
],
27+
},
28+
},
29+
],
30+
},
31+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+

test/tests/custom eslint path/stdout.txt

Whitespace-only changes.

test/tests/custom eslint path/test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { createTests } from "../../createTests";
2+
3+
const eslint = "./eslintrc.js";
4+
5+
createTests(__dirname, {
6+
eslint,
7+
extraArgs: ["--eslint", eslint],
8+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"rules": {
3+
"array-type": [true, "array"],
4+
"arrow-return-shorthand": false,
5+
"completed-docs": false,
6+
"comment-format": false,
7+
"file-name-casing": false,
8+
"linebreak-style": false,
9+
"interface-name": [true, "never-prefix"],
10+
"member-ordering": false,
11+
"newline-before-return": false,
12+
"no-any": false,
13+
"no-bitwise": false,
14+
"no-empty": false,
15+
"no-magic-numbers": false,
16+
"no-import-side-effect": false,
17+
"no-implicit-dependencies": [true, "dev"],
18+
"no-null-keyword": false,
19+
"no-parameter-reassignment": false,
20+
"no-parameter-properties": false,
21+
"no-submodule-imports": false,
22+
"no-unbound-method": false,
23+
"no-unused-variable": false,
24+
"no-use-before-declare": false,
25+
"prefer-conditional-expression": false,
26+
"prefer-method-signature": false,
27+
"prefer-switch": false,
28+
"prefer-template": false,
29+
"promise-function-async": false,
30+
"strict-boolean-expressions": [true, "allow-boolean-or-undefined", "allow-number"],
31+
"switch-default": false,
32+
"switch-final-break": false,
33+
"typedef": false
34+
}
35+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module.exports = {
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: ["@typescript-eslint", "@typescript-eslint/tslint"],
12+
rules: {
13+
"@typescript-eslint/array-type": "error",
14+
"@typescript-eslint/interface-name-prefix": "error",
15+
"no-magic-numbers": "off",
16+
"prefer-template": "off",
17+
"@typescript-eslint/tslint/config": [
18+
"error",
19+
{
20+
rules: {
21+
"no-implicit-dependencies": [true, "dev"],
22+
"strict-boolean-expressions": [
23+
true,
24+
"allow-boolean-or-undefined",
25+
"allow-number",
26+
],
27+
},
28+
},
29+
],
30+
},
31+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"dependencies": {
3+
"chalk": "2.4.2",
4+
"commander": "2.20.0",
5+
"tslint": "5.18.0",
6+
"typescript": "3.5.3"
7+
},
8+
"devDependencies": {
9+
"@babel/core": "7.5.5",
10+
"@babel/preset-env": "7.5.5",
11+
"@babel/preset-typescript": "7.3.3",
12+
"@types/jest": "24.0.15",
13+
"@types/node": "12.6.8",
14+
"@typescript-eslint/eslint-plugin": "1.12.0",
15+
"@typescript-eslint/parser": "1.12.0",
16+
"babel-jest": "24.8.0",
17+
"eslint": "6.0.1",
18+
"eslint-config-airbnb": "17.1.1",
19+
"eslint-config-airbnb-base": "13.2.0",
20+
"eslint-config-prettier": "6.0.0",
21+
"eslint-plugin-import": "2.18.2"
22+
},
23+
"version": "1.2.3"
24+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+

test/tests/custom package path/stdout.txt

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createTests } from "../../createTests";
2+
3+
createTests(__dirname, {
4+
extraArgs: ["--package", "my-package.json"],
5+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"rules": {
3+
"array-type": [true, "array"],
4+
"arrow-return-shorthand": false,
5+
"completed-docs": false,
6+
"comment-format": false,
7+
"file-name-casing": false,
8+
"linebreak-style": false,
9+
"interface-name": [true, "never-prefix"],
10+
"member-ordering": false,
11+
"newline-before-return": false,
12+
"no-any": false,
13+
"no-bitwise": false,
14+
"no-empty": false,
15+
"no-magic-numbers": false,
16+
"no-import-side-effect": false,
17+
"no-implicit-dependencies": [true, "dev"],
18+
"no-null-keyword": false,
19+
"no-parameter-reassignment": false,
20+
"no-parameter-properties": false,
21+
"no-submodule-imports": false,
22+
"no-unbound-method": false,
23+
"no-unused-variable": false,
24+
"no-use-before-declare": false,
25+
"prefer-conditional-expression": false,
26+
"prefer-method-signature": false,
27+
"prefer-switch": false,
28+
"prefer-template": false,
29+
"promise-function-async": false,
30+
"strict-boolean-expressions": [true, "allow-boolean-or-undefined", "allow-number"],
31+
"switch-default": false,
32+
"switch-final-break": false,
33+
"typedef": false
34+
}
35+
}
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
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-

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+
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
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-


0 commit comments

Comments
 (0)