From 55e620bdc7a8f60e75f2195e68a190bf07556e5e Mon Sep 17 00:00:00 2001 From: Ika Date: Sat, 1 Sep 2018 12:11:19 +0800 Subject: [PATCH] chore: extract jest snapshot serializers (#5034) --- jest.config.js | 4 ++-- package.json | 3 ++- tests_config/ansi-serializer.js | 13 ------------- tests_config/raw-serializer.js | 16 ---------------- tests_config/run_spec.js | 13 +------------ yarn.lock | 23 +++++++++++++++++------ 6 files changed, 22 insertions(+), 50 deletions(-) delete mode 100644 tests_config/ansi-serializer.js delete mode 100644 tests_config/raw-serializer.js diff --git a/jest.config.js b/jest.config.js index dc12fda87..c837041ac 100644 --- a/jest.config.js +++ b/jest.config.js @@ -13,8 +13,8 @@ const isOldNode = semver.parse(process.version).major <= 4; module.exports = { setupFiles: ["/tests_config/run_spec.js"], snapshotSerializers: [ - "/tests_config/raw-serializer.js", - "/tests_config/ansi-serializer.js" + "jest-snapshot-serializer-raw", + "jest-snapshot-serializer-ansi" ], testRegex: "jsfmt\\.spec\\.js$|__tests__/.*\\.js$", testPathIgnorePatterns: ["tests/new_react", "tests/more_react"].concat( diff --git a/package.json b/package.json index 664af0d9e..5b185800b 100644 --- a/package.json +++ b/package.json @@ -81,9 +81,10 @@ "eslint-plugin-prettier": "2.6.0", "eslint-plugin-react": "7.7.0", "execa": "0.10.0", - "has-ansi": "3.0.0", "jest": "23.3.0", "jest-junit": "5.0.0", + "jest-snapshot-serializer-ansi": "1.0.0", + "jest-snapshot-serializer-raw": "1.1.0", "jest-watch-typeahead": "0.1.0", "mkdirp": "0.5.1", "prettier": "1.14.2", diff --git a/tests_config/ansi-serializer.js b/tests_config/ansi-serializer.js deleted file mode 100644 index e807dc5bb..000000000 --- a/tests_config/ansi-serializer.js +++ /dev/null @@ -1,13 +0,0 @@ -"use strict"; - -const hasAnsi = require("has-ansi"); -const stripAnsi = require("strip-ansi"); - -module.exports = { - print(value, serialize) { - return serialize(stripAnsi(value)); - }, - test(value) { - return typeof value === "string" && hasAnsi(value); - } -}; diff --git a/tests_config/raw-serializer.js b/tests_config/raw-serializer.js deleted file mode 100644 index 8e447810c..000000000 --- a/tests_config/raw-serializer.js +++ /dev/null @@ -1,16 +0,0 @@ -"use strict"; - -const RAW = Symbol.for("raw"); - -module.exports = { - print(val) { - return val[RAW]; - }, - test(val) { - return ( - val && - Object.prototype.hasOwnProperty.call(val, RAW) && - typeof val[RAW] === "string" - ); - } -}; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index abc0f8627..9b54d2ee8 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -2,6 +2,7 @@ const fs = require("fs"); const extname = require("path").extname; +const raw = require("jest-snapshot-serializer-raw").wrap; const AST_COMPARE = process.env["AST_COMPARE"]; const TEST_STANDALONE = process.env["TEST_STANDALONE"]; @@ -130,18 +131,6 @@ function skipStandalone(parser) { return new Set(["parse5"]).has(parser); } -/** - * Wraps a string in a marker object that is used by `./raw-serializer.js` to - * directly print that string in a snapshot without escaping all double quotes. - * Backticks will still be escaped. - */ -function raw(string) { - if (typeof string !== "string") { - throw new Error("Raw snapshots have to be strings."); - } - return { [Symbol.for("raw")]: string }; -} - function mergeDefaultOptions(parserConfig) { return Object.assign( { diff --git a/yarn.lock b/yarn.lock index feef2fe6b..34052b7aa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2603,18 +2603,18 @@ har-validator@~5.0.3: ajv "^5.1.0" har-schema "^2.0.0" -has-ansi@3.0.0: - version "3.0.0" - resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-3.0.0.tgz#36077ef1d15f333484aa7fa77a28606f1c655b37" - dependencies: - ansi-regex "^3.0.0" - has-ansi@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-2.0.0.tgz#34f5049ce1ecdf2b0649af3ef24e45ed35416d91" dependencies: ansi-regex "^2.0.0" +has-ansi@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/has-ansi/-/has-ansi-3.0.0.tgz#36077ef1d15f333484aa7fa77a28606f1c655b37" + dependencies: + ansi-regex "^3.0.0" + has-flag@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-1.0.0.tgz#9d9e793165ce017a00f00418c43f942a7b1d11fa" @@ -3471,6 +3471,17 @@ jest-serializer@^23.0.1: version "23.0.1" resolved "https://registry.yarnpkg.com/jest-serializer/-/jest-serializer-23.0.1.tgz#a3776aeb311e90fe83fab9e533e85102bd164165" +jest-snapshot-serializer-ansi@1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/jest-snapshot-serializer-ansi/-/jest-snapshot-serializer-ansi-1.0.0.tgz#7daeefe7bfa81aed44e153fd3451320b61dc2a67" + dependencies: + has-ansi "^3.0.0" + strip-ansi "^4.0.0" + +jest-snapshot-serializer-raw@1.1.0: + version "1.1.0" + resolved "https://registry.yarnpkg.com/jest-snapshot-serializer-raw/-/jest-snapshot-serializer-raw-1.1.0.tgz#1d7f09c02f3dbbc3ae70b5b7598fb2f45e37d6c8" + jest-snapshot@^23.0.1: version "23.3.0" resolved "https://registry.yarnpkg.com/jest-snapshot/-/jest-snapshot-23.3.0.tgz#fc4e9f81e45432d10507e27f50bce60f44d81424"