test: add tests

master
ikatyang 2017-09-09 11:49:22 +08:00
parent 507545510f
commit 6ab8edafd5
3 changed files with 74 additions and 3 deletions

View File

@ -58,6 +58,66 @@ Uncategorized options:
"
`;
exports[`throw error and show usage with something unexpected 1`] = `
"Usage: prettier [opts] [filename ...]
Config options:
--config <path> Path to a prettier configuration file (.prettierrc, package.json, prettier.config.js).
--config-precedence <cli-override|file-override|prefer-file>
Defines how config file should be evaluated in combination of CLI options
cli-override | default config => config file => CLI options
file-override | default config => CLI options => config file
prefer-file | default config => config file (if config file is found) or
default config => CLI options (if no config file is found)
Defaults to cli-override
--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.
--no-config Do not look for a configuration file.
--with-node-modules Process files inside 'node_modules' directory.
Format options:
--jsx-bracket-same-line Put > on the last line instead of at a new line.
--no-bracket-spacing Do not print spaces between brackets.
--no-semi Do not print semicolons, except at the beginning of lines which may need them.
--parser <flow|babylon|typescript|postcss|json|graphql>
Specify which parse to use. Defaults to babylon.
--print-width <int> Specify the length of line that the printer will wrap on. Defaults to 80.
--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.
--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.
--single-quote Use single quotes instead of double quotes.
--tab-width <int> Specify the number of spaces per indentation-level. Defaults to 2.
--trailing-comma <none|es5|all>
Print trailing commas wherever possible when multi-line. Defaults to none.
--use-tabs Indent lines with tabs instead of spaces.
Uncategorized options:
--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
--help or -h Show help.
--list-different or -l Print filenames of files that are different from Prettier formatting.
--no-color Do not colorize error messages.
--stdin Read input from stdin.
--stdin-filepath <path> Path to the file used to read from stdin.
--version or -v Print Prettier version.
--write Edit the file in-place. (Beware!)
"
`;
exports[`throw error and show usage with something unexpected 2`] = `""`;
exports[`throw error with --find-config-path + multiple files 1`] = `
"Cannot use --find-config-path with multiple files
"

View File

@ -30,3 +30,11 @@ test("throw error with --find-config-path + multiple files", () => {
expect(result.stderr).toMatchSnapshot();
expect(result.status).toEqual(1);
});
test("throw error and show usage with something unexpected", () => {
const result = runPrettier("cli", [], { isTTY: true });
expect(result.stdout).toMatchSnapshot();
expect(result.stderr).toMatchSnapshot();
expect(result.status).not.toEqual(0);
});

View File

@ -4,6 +4,9 @@ const fs = require("fs");
const path = require("path");
function runPrettier(dir, args, options) {
args = args || [];
options = options || {};
let status;
let stdout = "";
let stderr = "";
@ -43,12 +46,12 @@ function runPrettier(dir, args, options) {
const originalStdinIsTTY = process.stdin.isTTY;
process.chdir(normalizeDir(dir));
process.stdin.isTTY = false;
process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args || []);
process.stdin.isTTY = !!options.isTTY;
process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args);
jest.resetModules();
jest.setMock("get-stream", () => ({
then: handler => handler((options && options.input) || "")
then: handler => handler(options.input || "")
}));
try {