Skip to content

Added options for CLI flags to end-to-end tests #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions test/createTestArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ 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")];

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

return flags.map(flag => `"${flag}"`).join(" ");
return [...flags.map(flag => `"${flag}"`).join(" "), ...extraArgs];
};
59 changes: 42 additions & 17 deletions test/createTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReturnType<typeof exec>>;
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);
});
});
Expand Down
31 changes: 31 additions & 0 deletions test/tests/custom eslint path/eslintrc.js
Original file line number Diff line number Diff line change
@@ -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",
],
},
},
],
},
};
31 changes: 31 additions & 0 deletions test/tests/custom eslint path/expected.json
Original file line number Diff line number Diff line change
@@ -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",
],
},
},
],
},
};
4 changes: 4 additions & 0 deletions test/tests/custom eslint path/stderr.txt
Original file line number Diff line number Diff line change
@@ -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.

Empty file.
8 changes: 8 additions & 0 deletions test/tests/custom eslint path/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { createTests } from "../../createTests";

const eslint = "./eslintrc.js";

createTests(__dirname, {
eslint,
extraArgs: ["--eslint", eslint],
});
35 changes: 35 additions & 0 deletions test/tests/custom eslint path/tslint.json
Original file line number Diff line number Diff line change
@@ -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
}
}
31 changes: 31 additions & 0 deletions test/tests/custom package path/expected.json
Original file line number Diff line number Diff line change
@@ -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",
],
},
},
],
},
};
24 changes: 24 additions & 0 deletions test/tests/custom package path/my-package.json
Original file line number Diff line number Diff line change
@@ -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"
}
4 changes: 4 additions & 0 deletions test/tests/custom package path/stderr.txt
Original file line number Diff line number Diff line change
@@ -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.

Empty file.
5 changes: 5 additions & 0 deletions test/tests/custom package path/test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createTests } from "../../createTests";

createTests(__dirname, {
extraArgs: ["--package", "my-package.json"],
});
35 changes: 35 additions & 0 deletions test/tests/custom package path/tslint.json
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 4 additions & 5 deletions test/tests/missing tslint.json/stderr.txt
Original file line number Diff line number Diff line change
@@ -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.


❌ 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.

4 changes: 4 additions & 0 deletions test/tests/standalone tslint.json/stderr.txt
Original file line number Diff line number Diff line change
@@ -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.

4 changes: 0 additions & 4 deletions test/tests/standalone tslint.json/stdout.txt
Original file line number Diff line number Diff line change
@@ -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! ✅