diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 5f7ae5d59..923c3ffef 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -12,7 +12,9 @@ Here's what you need to know about the tests: - The tests use [Jest snapshots](https://facebook.github.io/jest/docs/en/snapshot-testing.html). - You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR. -- You can run `AST_COMPARE=1 jest` for a more robust test run. That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent. +- You can run `AST_COMPARE=1 DEEP_COMPARE=1 jest` for a more robust test run. + - `AST_COMPARE` That formats each file, re-parses it, and compares the new AST with the original one and makes sure they are semantically equivalent. + - `DEEP_COMPARE` That formats each file, then formats the output again, and checks that the second output is the same as the first. - Each test folder has a `jsfmt.spec.js` that runs the tests. For JavaScript files, generally you can just put `run_spec(__dirname, ["babel", "flow", "typescript"]);` there. This will verify that the output using each parser is the same. You can also pass options as the third argument, like this: `run_spec(__dirname, ["babel"], { trailingComma: "es5" });` - `tests/flow/` contains the Flow test suite, and is not supposed to be edited by hand. To update it, clone the Flow repo next to the Prettier repo and run: `node scripts/sync-flow-tests.js ../flow/tests/`. - If you would like to debug prettier locally, you can either debug it in node or the browser. The easiest way to debug it in the browser is to run the interactive `docs` REPL locally. The easiest way to debug it in node, is to create a local test file with some example code you want formatted and either run it in an editor like VS Code or run it directly via `./bin/prettier.js `. diff --git a/package.json b/package.json index 6b98fbcd2..095b8bd2d 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "angular-html-parser": "1.4.0", "camelcase": "5.3.1", "chalk": "3.0.0", - "ci-info": "2.0.0", + "ci-info": "watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540", "cjk-regex": "2.0.0", "cosmiconfig": "6.0.0", "dashify": "2.0.0", diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 2108094a2..bde58f06c 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -7,6 +7,7 @@ const { isCI } = require("ci-info"); const { TEST_STANDALONE, TEST_CRLF } = process.env; const AST_COMPARE = isCI || process.env.AST_COMPARE; +const DEEP_COMPARE = isCI || process.env.DEEP_COMPARE; const CURSOR_PLACEHOLDER = "<|>"; const RANGE_START_PLACEHOLDER = "<<>>"; @@ -16,6 +17,42 @@ const prettier = !TEST_STANDALONE ? require("prettier/local") : require("prettier/standalone"); +// TODO: these test files need fix +const unstableTests = new Map( + [ + "class_comment/comments.js", + ["comments/dangling_array.js", options => options.semi === false], + ["comments/jsx.js", options => options.semi === false], + "comments/return-statement.js", + "comments/tagged-template-literal.js", + "comments_closure_typecast/iife.js", + "css_atrule/include.css", + "graphql_interface/separator-detection.graphql", + [ + "html_angular/attributes.component.html", + options => options.printWidth === 1 + ], + "html_prettier_ignore/cases.html", + "js_empty/semicolon.js", + "jsx_ignore/jsx_ignore.js", + "markdown_footnoteDefinition/multiline.md", + "markdown_spec/example-234.md", + "markdown_spec/example-235.md", + "multiparser_html_js/script-tag-escaping.html", + [ + "multiparser_js_markdown/codeblock.js", + options => options.proseWrap === "always" + ], + ["no-semi/comments.js", options => options.semi === false], + "yaml_prettier_ignore/document.yml" + ].map(fixture => { + const [file, isUnstable = () => true] = Array.isArray(fixture) + ? fixture + : [fixture]; + return [path.join(__dirname, "../tests/", file), isUnstable]; + }) +); + global.run_spec = (dirname, parsers, options) => { // `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone` const IS_PARSER_INFERENCE_TESTS = dirname.endsWith("parser-inference"); @@ -105,6 +142,26 @@ global.run_spec = (dirname, parsers, options) => { parsersToVerify.push("babel-ts"); } + if ( + DEEP_COMPARE && + typeof rangeStart === "undefined" && + typeof rangeEnd === "undefined" && + typeof cursorOffset === "undefined" && + !TEST_CRLF + ) { + test(`${basename} second format`, () => { + const secondOutput = format(output, filename, mainOptions); + const isUnstable = unstableTests.get(filename); + if (isUnstable && isUnstable(options || {})) { + // To keep eye on failed tests, this assert never supposed to pass, + // if it fails, just remove the file from `unstableTests` + expect(secondOutput).not.toEqual(output); + } else { + expect(secondOutput).toEqual(output); + } + }); + } + for (const parser of parsersToVerify) { const verifyOptions = { ...baseOptions, parser }; @@ -127,7 +184,7 @@ global.run_spec = (dirname, parsers, options) => { } if (AST_COMPARE) { - test(`${filename} parse`, () => { + test(`${basename} parse`, () => { const parseOptions = { ...mainOptions }; delete parseOptions.cursorOffset; diff --git a/yarn.lock b/yarn.lock index 706fc62ea..89a8b9c7d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2112,10 +2112,9 @@ chrome-trace-event@^1.0.2: dependencies: tslib "^1.9.0" -ci-info@2.0.0, ci-info@^2.0.0: +ci-info@^2.0.0, ci-info@watson/ci-info#f43f6a1cefff47fb361c88cf4b943fdbcaafe540: version "2.0.0" - resolved "https://registry.yarnpkg.com/ci-info/-/ci-info-2.0.0.tgz#67a9e964be31a51e15e5010d58e6f12834002f46" - integrity sha512-5tK7EtrZ0N+OLFMthtqOj4fI2Jeb88C4CAZPu25LDVUgXJ0A3Js4PMGqrn0JU1W0Mh1/Z8wZzYPxqUrXeBboCQ== + resolved "https://codeload.github.com/watson/ci-info/tar.gz/f43f6a1cefff47fb361c88cf4b943fdbcaafe540" cipher-base@^1.0.0, cipher-base@^1.0.1, cipher-base@^1.0.3: version "1.0.4"