test(integration): add more tests

master
ikatyang 2017-09-03 21:31:33 +08:00
parent c4e5463514
commit ac9fa43114
19 changed files with 228 additions and 1 deletions

View File

@ -0,0 +1,15 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`throw error with invalid config format 1`] = `"Failed to parse \\"<cwd>/tests_integration/cli/config/invalid/file/.prettierrc\\" as JSON, JS, or YAML."`;
exports[`throw error with invalid config option (int) 1`] = `
"Invalid --tab-width value. Expected an integer, but received: 0.5
"
`;
exports[`throw error with invalid config option (trailingComma) 1`] = `
"Invalid configuration:
"
`;
exports[`throw error with invalid config target (directory) 1`] = `"EISDIR: illegal operation on a directory, read"`;

View File

@ -0,0 +1,54 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`show usage with --help 1`] = `
"Usage: prettier [opts] [filename ...]
Available options:
--write Edit the file in-place. (Beware!)
--list-different or -l Print filenames of files that are different from Prettier formatting.
--config Path to a prettier configuration file (.prettierrc, package.json, prettier.config.js).
--no-config Do not look for a configuration file.
--find-config-path <path>
Finds and prints the path to a configuration file for a given input file.
--ignore-path <path> Path to a file containing patterns that describe files to ignore.
Defaults to ./.prettierignore.
--stdin Read input from stdin.
--stdin-filepath Path to the file used to read from stdin.
--print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.
--tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.
--use-tabs Indent lines with tabs instead of spaces.
--no-semi Do not print semicolons, except at the beginning of lines which may need them.
--single-quote Use single quotes instead of double quotes.
--no-bracket-spacing Do not print spaces between brackets.
--jsx-bracket-same-line Put > on the last line instead of at a new line.
--trailing-comma <none|es5|all>
Print trailing commas wherever possible when multi-line. Defaults to none.
--parser <flow|babylon|typescript|postcss|json|graphql>
Specify which parse to use. Defaults to babylon.
--cursor-offset <int> Print (to stderr) where a cursor at the given position would move to after formatting.
This option cannot be used with --range-start and --range-end
--range-start <int> Format code starting at a given character offset.
The range will extend backwards to the start of the first line containing the selected statement.
This option cannot be used with --cursor-offset.
Defaults to 0.
--range-end <int> Format code ending at a given character offset (exclusive).
The range will extend forwards to the end of the selected statement.
This option cannot be used with --cursor-offset.
Defaults to Infinity.
--no-color Do not colorize error messages.
--with-node-modules Process files inside 'node_modules' directory.
--version or -v Print Prettier version.
"
`;
exports[`throw error with --find-config-path + multiple files 1`] = `
"Cannot use --find-config-path with multiple files
"
`;
exports[`throw error with --write + --debug-check 1`] = `
"Cannot use --write and --debug-check together.
"
`;

View File

@ -0,0 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`throw error with invalid ignore 1`] = `
"Unable to read <cwd>/tests_integration/cli/invalid-ignore/.prettierignore:
"
`;

View File

@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`do not write file with --write + invalid file 1`] = `
"invalid.js: SyntaxError: Unexpected token, expected ; (1:6)
> 1 | this is invalid!
 |  ^
"
`;
exports[`write file with --write + unformated file 1`] = `
Array [
Object {
"content": "var x = 1; // eslint-disable-line
",
"filename": "unformated.js",
},
]
`;

View File

@ -0,0 +1,38 @@
"use strict";
const runPrettier = require("../runPrettier");
expect.addSnapshotSerializer(require("../cwd-serializer"));
test("throw error with invalid config format", () => {
const output = runPrettier("cli/config/invalid", [
"--config",
"file/.prettierrc"
]);
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});
test("throw error with invalid config target (directory)", () => {
const output = runPrettier("cli/config/invalid", [
"--config",
"folder/.prettierrc" // this is a directory
]);
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});
test("throw error with invalid config option (int)", () => {
const output = runPrettier("cli/config/invalid", ["--config", "option/int"]);
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});
test("throw error with invalid config option (trailingComma)", () => {
const output = runPrettier("cli/config/invalid", [
"--config",
"option/trailingComma"
]);
expect(output.stderr).toMatchSnapshot();
expect(output.status).not.toBe(0);
});

View File

@ -0,0 +1,32 @@
"use strict";
const prettier = require("../..");
const runPrettier = require("../runPrettier");
test("show version with --version", () => {
const result = runPrettier("cli/with-shebang", ["--version"]);
expect(result.stdout).toBe(prettier.version + "\n");
expect(result.status).toEqual(0);
});
test("show usage with --help", () => {
const result = runPrettier("cli", ["--help"]);
expect(result.stdout).toMatchSnapshot();
expect(result.status).toEqual(0);
});
test("throw error with --write + --debug-check", () => {
const result = runPrettier("cli", ["--write", "--debug-check"]);
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(1);
});
test("throw error with --find-config-path + multiple files", () => {
const result = runPrettier("cli", ["--find-config-path", "abc.js", "def.js"]);
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(1);
});

View File

@ -0,0 +1,12 @@
"use strict";
const runPrettier = require("../runPrettier");
expect.addSnapshotSerializer(require("../cwd-serializer"));
test("throw error with invalid ignore", () => {
const result = runPrettier("cli/invalid-ignore", ["something.js"]);
expect(result.stderr).toMatchSnapshot();
expect(result.status).not.toEqual(0);
});

View File

@ -0,0 +1,25 @@
"use strict";
const runPrettier = require("../runPrettier");
test("write file with --write + unformated file", () => {
const result = runPrettier("cli/write", ["--write", "unformated.js"]);
expect(result.write).toMatchSnapshot();
expect(result.status).toEqual(0);
});
test("do not write file with --write + formated file", () => {
const result = runPrettier("cli/write", ["--write", "formated.js"]);
expect(result.write).toHaveLength(0);
expect(result.status).toEqual(0);
});
test("do not write file with --write + invalid file", () => {
const result = runPrettier("cli/write", ["--write", "invalid.js"]);
expect(result.stderr).toMatchSnapshot();
expect(result.write).toHaveLength(0);
expect(result.status).not.toEqual(0);
});

View File

@ -0,0 +1 @@
--invalid--

View File

@ -0,0 +1,3 @@
{
"tabWidth": 0.5
}

View File

@ -0,0 +1,3 @@
{
"trailing-comma": "wow"
}

View File

@ -0,0 +1 @@
var x = 1; // eslint-disable-line

View File

@ -0,0 +1 @@
this is invalid!

View File

@ -0,0 +1 @@
var x = 1; // eslint-disable-line

View File

@ -0,0 +1,8 @@
"use strict";
module.exports = {
test: value =>
typeof value === "string" && value.indexOf(process.cwd()) !== -1,
print: (value, serializer) =>
serializer(value.replace(process.cwd(), "<cwd>"))
};

View File

@ -1,6 +1,7 @@
"use strict";
const path = require("path");
const fs = require("fs");
function runPrettier(dir, args, options) {
let status;
@ -49,6 +50,13 @@ function runPrettier(dir, args, options) {
}
});
const write = [];
const spiedFsWriteFileSync = jest.spyOn(fs, "writeFileSync");
spiedFsWriteFileSync.mockImplementation((filename, content) => {
write.push({ filename, content });
});
const originalCwd = process.cwd();
const originalIsTTY = process.stdin.isTTY;
const originalArgv = process.argv;
@ -81,9 +89,10 @@ function runPrettier(dir, args, options) {
spiedConsoleLog.mockRestore();
spiedConsoleWarn.mockRestore();
spiedConsoleError.mockRestore();
spiedFsWriteFileSync.mockRestore();
}
return { status, stdout, stderr };
return { status, stdout, stderr, write };
}
function normalizeDir(dir) {