diff --git a/index.js b/index.js index 25452ce78..394952a5e 100644 --- a/index.js +++ b/index.js @@ -11,6 +11,7 @@ const parser = require("./src/parser"); const printDocToDebug = require("./src/doc-debug").printDocToDebug; const config = require("./src/resolve-config"); const docblock = require("jest-docblock"); +const getStream = require("get-stream"); function guessLineEnding(text) { const index = text.indexOf("\n"); @@ -353,6 +354,7 @@ module.exports = { /* istanbul ignore next */ __debug: { + getStream, parse: function(text, opts) { return parser.parse(text, opts); }, diff --git a/jest.config.js b/jest.config.js index 506ae3af3..bcfffd316 100644 --- a/jest.config.js +++ b/jest.config.js @@ -13,5 +13,6 @@ module.exports = { "/src/doc-debug.js", "/src/clean-ast.js", "/src/deprecated.js" - ] + ], + transform: {} }; diff --git a/src/cli-util.js b/src/cli-util.js index a93eea37c..2fd62ba76 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -4,7 +4,6 @@ const path = require("path"); const camelCase = require("camelcase"); const dashify = require("dashify"); const minimist = require("minimist"); -const getStream = require("get-stream"); const fs = require("fs"); const globby = require("globby"); const ignore = require("ignore"); @@ -208,7 +207,7 @@ function applyConfigPrecedence(argv, options) { } function formatStdin(argv) { - getStream(process.stdin).then(input => { + prettier.__debug.getStream(process.stdin).then(input => { const filepath = argv["stdin-filepath"] ? path.resolve(process.cwd(), argv["stdin-filepath"]) : process.cwd(); diff --git a/tests/cursor/jsfmt.spec.js b/tests/cursor/jsfmt.spec.js index e92b0d4e4..6af7a5d1b 100644 --- a/tests/cursor/jsfmt.spec.js +++ b/tests/cursor/jsfmt.spec.js @@ -1,4 +1,4 @@ -const prettier = require("../.."); +const prettier = require("../../tests_config/require_prettier"); test("translates cursor correctly in basic case", () => { expect(prettier.formatWithCursor(" 1", { cursorOffset: 2 })).toEqual({ diff --git a/tests_config/require_prettier.js b/tests_config/require_prettier.js new file mode 100644 index 000000000..87a7b1b2d --- /dev/null +++ b/tests_config/require_prettier.js @@ -0,0 +1,6 @@ +"use strict"; + +const isProduction = process.env.NODE_ENV === "production"; +const prettier = require(isProduction ? "../dist/" : "../"); + +module.exports = prettier; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 5f8532bc0..5c28a505e 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -1,9 +1,8 @@ "use strict"; -const isProduction = process.env.NODE_ENV === "production"; const fs = require("fs"); const extname = require("path").extname; -const prettier = require(isProduction ? "../dist/" : "../"); +const prettier = require("./require_prettier"); const parser = require("../src/parser"); const massageAST = require("../src/clean-ast.js").massageAST; diff --git a/tests_integration/__tests__/config-resolution.js b/tests_integration/__tests__/config-resolution.js index fff56fbe5..ef6a8a780 100644 --- a/tests_integration/__tests__/config-resolution.js +++ b/tests_integration/__tests__/config-resolution.js @@ -3,7 +3,7 @@ const path = require("path"); const runPrettier = require("../runPrettier"); -const prettier = require("../../"); +const prettier = require("../../tests_config/require_prettier"); expect.addSnapshotSerializer(require("../path-serializer")); diff --git a/tests_integration/__tests__/early-exit.js b/tests_integration/__tests__/early-exit.js index d5b073443..19f73beae 100644 --- a/tests_integration/__tests__/early-exit.js +++ b/tests_integration/__tests__/early-exit.js @@ -1,6 +1,6 @@ "use strict"; -const prettier = require("../.."); +const prettier = require("../../tests_config/require_prettier"); const runPrettier = require("../runPrettier"); const constant = require("../../src/cli-constant"); diff --git a/tests_integration/__tests__/parser-api.js b/tests_integration/__tests__/parser-api.js index c63cee06f..19bdea7d5 100644 --- a/tests_integration/__tests__/parser-api.js +++ b/tests_integration/__tests__/parser-api.js @@ -1,6 +1,6 @@ "use strict"; -const prettier = require("../.."); +const prettier = require("../../tests_config/require_prettier"); const runPrettier = require("../runPrettier"); test("allows custom parser provided as object", () => { diff --git a/tests_integration/runPrettier.js b/tests_integration/runPrettier.js index fd43b8df9..1d2e4da0b 100644 --- a/tests_integration/runPrettier.js +++ b/tests_integration/runPrettier.js @@ -3,6 +3,10 @@ const fs = require("fs"); const path = require("path"); +const isProduction = process.env.NODE_ENV === "production"; +const prettierApi = isProduction ? "../dist/index" : "../index"; +const prettierCli = isProduction ? "../dist/bin/prettier" : "../bin/prettier"; + function runPrettier(dir, args, options) { args = args || []; options = options || {}; @@ -11,32 +15,35 @@ function runPrettier(dir, args, options) { let stdout = ""; let stderr = ""; - const spiedProcessExit = jest.spyOn(process, "exit"); - spiedProcessExit.mockImplementation(exitCode => { + jest.spyOn(process, "exit").mockImplementation(exitCode => { if (status === undefined) { status = exitCode || 0; } }); - const spiedStdoutWrite = jest.spyOn(process.stdout, "write"); - spiedStdoutWrite.mockImplementation(text => appendStdout(text)); + jest + .spyOn(process.stdout, "write") + .mockImplementation(text => appendStdout(text)); - const spiedStderrWrite = jest.spyOn(process.stderr, "write"); - spiedStderrWrite.mockImplementation(text => appendStderr(text)); + jest + .spyOn(process.stderr, "write") + .mockImplementation(text => appendStderr(text)); - const spiedConsoleLog = jest.spyOn(console, "log"); - spiedConsoleLog.mockImplementation(text => appendStdout(text + "\n")); + jest + .spyOn(console, "log") + .mockImplementation(text => appendStdout(text + "\n")); - const spiedConsoleWarn = jest.spyOn(console, "warn"); - spiedConsoleWarn.mockImplementation(text => appendStderr(text + "\n")); + jest + .spyOn(console, "warn") + .mockImplementation(text => appendStderr(text + "\n")); - const spiedConsoleError = jest.spyOn(console, "error"); - spiedConsoleError.mockImplementation(text => appendStderr(text + "\n")); + jest + .spyOn(console, "error") + .mockImplementation(text => appendStderr(text + "\n")); const write = []; - const spiedFsWriteFileSync = jest.spyOn(fs, "writeFileSync"); - spiedFsWriteFileSync.mockImplementation((filename, content) => { + jest.spyOn(fs, "writeFileSync").mockImplementation((filename, content) => { write.push({ filename, content }); }); @@ -50,12 +57,18 @@ function runPrettier(dir, args, options) { process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args); jest.resetModules(); - jest.setMock("get-stream", () => ({ - then: handler => handler(options.input || "") - })); + + // We cannot use `jest.setMock("get-stream", impl)` here, because in the + // production build everything is bundled into one file so there is no + // "get-stream" module to mock. + jest + .spyOn(require(prettierApi).__debug, "getStream") + .mockImplementation(() => ({ + then: handler => handler(options.input || "") + })); try { - require("../bin/prettier"); + require(prettierCli); status = (status === undefined ? process.exitCode : status) || 0; } catch (error) { status = 1;