Skip to content

WIP: Make production tests always run against dist/ - #2932

Closed
lydell wants to merge 6 commits into
prettier:masterfrom
lydell:fix-production-tests
Closed

lydell wants to merge 6 commits into
prettier:masterfrom
lydell:fix-production-tests

Conversation

@lydell

@lydell lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member

The good: This catches the errors that #2930 fixes!
The bad: Other tests fail :(

Production test summary:

Test Suites: 9 failed, 735 passed, 744 total
Tests:       23 failed, 2507 passed, 2530 total
Snapshots:   18 failed, 2025 passed, 2043 total
Details
Summary of all failing tests
 FAIL  tests_integration/__tests__/with-config-precedence.js
  ● CLI overrides take precedence without --config-precedence
expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- Snapshot
+ Received

@@ -5,46 +5,6 @@
    "jest/Component.js should not have semi"
  )
  console.log(
    "jest/Component.test.js should have semi"
  );
- function js() {
-         console.log(
-                 "js/file.js should have tab width 8"
-         );
- }
- "use strict";
- 
- module.exports = {
-         tabWidth: 8
- };
- function noConfigJs() {
-   console.log(
-     "no-config/file.js should have no semicolons"
-   )
- }
- function packageJs() {
-    console.log(
-       "package/file.js should have tab width 3"
-    );
- }
- function rcJson() {
-   console.log.apply(
-     null,
-     [
-       'rc-json/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
- function rcYaml() {
-   console.log.apply(
-     null,
-     [
-       'rc-yaml/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
  "
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:7:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI overrides take precedence with --config-precedence cli-override

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- Snapshot
+ Received

@@ -5,46 +5,6 @@
    "jest/Component.js should not have semi"
  )
  console.log(
    "jest/Component.test.js should have semi"
  );
- function js() {
-         console.log(
-                 "js/file.js should have tab width 8"
-         );
- }
- "use strict";
- 
- module.exports = {
-         tabWidth: 8
- };
- function noConfigJs() {
-   console.log(
-     "no-config/file.js should have no semicolons"
-   )
- }
- function packageJs() {
-    console.log(
-       "package/file.js should have tab width 3"
-    );
- }
- function rcJson() {
-   console.log.apply(
-     null,
-     [
-       'rc-json/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
- function rcYaml() {
-   console.log.apply(
-     null,
-     [
-       'rc-yaml/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
  "
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:19:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI overrides take lower precedence with --config-precedence file-override

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "function js() {
        console.log("js/file.js should have tab width 8");
}
"use strict";

module.exports = {
        tabWidth: 8
};
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:31:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI overrides gets ignored when config exists with --config-precedence prefer-file

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "function js() {
        console.log("js/file.js should have tab width 8");
}
"use strict";

module.exports = {
        tabWidth: 8
};
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:57:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI validate options with --config-precedence cli-override

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:81:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI validate options with --config-precedence file-override

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:90:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI validate options with --config-precedence prefer-file

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:99:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/config-invalid.js
● throw error with invalid config format

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Error: Invalid configuration file.
Failed to parse "<cwd>/tests_integration/cli/config/invalid/file/.prettierrc" as JSON, JS, or YAML.
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:12:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config target (directory)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Error: Invalid configuration file.
EISDIR: illegal operation on a directory, read
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:21:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config option (int)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid tabWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:27:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config option (trailingComma)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid option for trailingComma.
Expected "none", "es5" or "all", but received: "wow""
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:36:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● show warning with unknown option

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Ignored unknown option: hello
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:54:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/stdin-filepath.js
● format correctly if stdin content compatible with stdin-filepath

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- ".name {
  display: none;
}
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/stdin-filepath.js:12:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error if stdin content incompatible with stdin-filepath

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 2.

- "stdin: SyntaxError: Unexpected token (1:1)
> 1 | .name { display: none; }
    | ^
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/stdin-filepath.js:25:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/debug-check.js
● checks stdin with --debug-check

expect(received).toEqual(expected)

Expected value to equal:
  "(stdin)
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/debug-check.js:21:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/debug-print-doc.js
● prints doc with --debug-print-doc

expect(received).toEqual(expected)

Expected value to equal:
  "[\"0\", \";\", hardline, breakParent];
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/debug-print-doc.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/cursot-offset.js
● write cursorOffset to stderr with --cursor-offset

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "1;
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/cursot-offset.js:8:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● write cursorOffset to stderr with --cursor-offset

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 2.

- "1
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/cursot-offset.js:9:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/list-different.js
● checks stdin with --list-different

expect(received).toEqual(expected)

Expected value to equal:
  "(stdin)
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/list-different.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/syntax-error.js
● exits with non-zero code when input has a syntax error

expect(received).toEqual(expected)

Expected value to equal:
  2
Received:
  0
  
  at Object.<anonymous>.test (tests_integration/__tests__/syntax-error.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/config-resolution.js (27.864s)
● resolves configuration from external files

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- Snapshot
+ Received

  "console.log("jest/__best-tests__/file.js should have semi");
  console.log("jest/Component.js should not have semi")
  console.log("jest/Component.test.js should have semi");
- function js() {
-         console.log("js/file.js should have tab width 8");
- }
- "use strict";
- 
- module.exports = {
-         tabWidth: 8
- };
- function noConfigJs() {
-   console.log("no-config/file.js should have no semicolons")
- }
- function packageJs() {
-    console.log("package/file.js should have tab width 3");
- }
- function rcJson() {
-   console.log.apply(null, [
-     'rc-json/file.js',
-     'should have trailing comma',
-     'and single quotes',
-   ]);
- }
- function rcYaml() {
-   console.log.apply(null, [
-     'rc-yaml/file.js',
-     'should have trailing comma',
-     'and single quotes',
-   ]);
- }
  "
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-resolution.js:12:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI overrides take precedence

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- Snapshot
+ Received

@@ -5,46 +5,6 @@
    "jest/Component.js should not have semi"
  )
  console.log(
    "jest/Component.test.js should have semi"
  );
- function js() {
-         console.log(
-                 "js/file.js should have tab width 8"
-         );
- }
- "use strict";
- 
- module.exports = {
-         tabWidth: 8
- };
- function noConfigJs() {
-   console.log(
-     "no-config/file.js should have no semicolons"
-   )
- }
- function packageJs() {
-    console.log(
-       "package/file.js should have tab width 3"
-    );
- }
- function rcJson() {
-   console.log.apply(
-     null,
-     [
-       'rc-json/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
- function rcYaml() {
-   console.log.apply(
-     null,
-     [
-       'rc-yaml/file.js',
-       'should have trailing comma',
-       'and single quotes',
-     ],
-   );
- }
  "
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-resolution.js:67:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● API resolveConfig with file arg

TypeError: Cannot read property 'children' of undefined
  
  at requireFromString (dist/index.js:19737:7)
  at parseJsFile (dist/index.js:19883:8)
      at <anonymous>

● API resolveConfig.sync with file arg

TypeError: Cannot read property 'children' of undefined
  
  at requireFromString (dist/index.js:19737:7)
  at parseJsFile (dist/index.js:19883:8)
  at loadJs (dist/index.js:19890:1)
  at result (dist/index.js:20179:8)
  at chainFuncsSync (dist/index.js:19745:37)
      at Array.reduce (<anonymous>)
  at funcRunner (dist/index.js:19758:14)
  at searchDirectory (dist/index.js:20161:14)
  at load (dist/index.js:20151:1)
  at Function.Object.<anonymous>.resolveConfig.sync (dist/index.js:21536:14)
  at Object.<anonymous>.test (tests_integration/__tests__/config-resolution.js:92:33)
      at Promise (<anonymous>)
      at <anonymous>

With #2930 applied:

Test Suites: 8 failed, 736 passed, 744 total
Tests:       15 failed, 2515 passed, 2530 total
Snapshots:   12 failed, 2031 passed, 2043 total
Details
Summary of all failing tests
 FAIL  tests_integration/__tests__/with-config-precedence.js
  ● CLI validate options with --config-precedence cli-override
expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:81:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI validate options with --config-precedence file-override

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:90:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● CLI validate options with --config-precedence prefer-file

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid printWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/with-config-precedence.js:99:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/stdin-filepath.js
● format correctly if stdin content compatible with stdin-filepath

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- ".name {
  display: none;
}
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/stdin-filepath.js:12:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error if stdin content incompatible with stdin-filepath

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 2.

- "stdin: SyntaxError: Unexpected token (1:1)
> 1 | .name { display: none; }
    | ^
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/stdin-filepath.js:25:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/debug-check.js
● checks stdin with --debug-check

expect(received).toEqual(expected)

Expected value to equal:
  "(stdin)
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/debug-check.js:21:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/config-invalid.js
● throw error with invalid config format

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Error: Invalid configuration file.
Failed to parse "<cwd>/tests_integration/cli/config/invalid/file/.prettierrc" as JSON, JS, or YAML.
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:12:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config target (directory)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Error: Invalid configuration file.
EISDIR: illegal operation on a directory, read
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:21:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config option (int)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid tabWidth value.
Expected an integer, but received: 0.5"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:27:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● throw error with invalid config option (trailingComma)

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Invalid option for trailingComma.
Expected "none", "es5" or "all", but received: "wow""
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:36:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● show warning with unknown option

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "Ignored unknown option: hello
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/config-invalid.js:54:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/syntax-error.js
● exits with non-zero code when input has a syntax error

expect(received).toEqual(expected)

Expected value to equal:
  2
Received:
  0
  
  at Object.<anonymous>.test (tests_integration/__tests__/syntax-error.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/cursot-offset.js
● write cursorOffset to stderr with --cursor-offset

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 1.

- "1;
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/cursot-offset.js:8:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

● write cursorOffset to stderr with --cursor-offset

expect(value).toMatchSnapshot()

Received value does not match stored snapshot 2.

- "1
"
+ ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/cursot-offset.js:9:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/list-different.js
● checks stdin with --list-different

expect(received).toEqual(expected)

Expected value to equal:
  "(stdin)
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/list-different.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

FAIL tests_integration/tests/debug-print-doc.js
● prints doc with --debug-print-doc

expect(received).toEqual(expected)

Expected value to equal:
  "[\"0\", \";\", hardline, breakParent];
"
Received:
  ""
  
  at Object.<anonymous>.test (tests_integration/__tests__/debug-print-doc.js:10:25)
      at Promise (<anonymous>)
      at <anonymous>
  at process._tickCallback (internal/process/next_tick.js:188:7)

I have not figured out what causes the test failures yet. Help appreciated! :)

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

Almost all of the failing are of the type “Expected "...." but received "" (empty string)”. It sounds as if the mocking in runPrettier.js doesn’t work when testing against the production build.

@ikatyang

Copy link
Copy Markdown
Member

It seems caused by this mock, I have no idea how to mock it without touching its source code...

jest.setMock("get-stream", () => ({

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

@ikatyang Nice find! I guess we could make a src/cli-get-stdin.js (which uses get-stream) and we can mock that instead, or something?

@ikatyang

Copy link
Copy Markdown
Member

To mock, we have to get its reference first, but since they're bundled, how can we get it from outside?

@ikatyang

ikatyang commented Sep 29, 2017 •

Copy link
Copy Markdown
Member

Or we can expose it as something like _debug_get_stream and then mock it.

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

Can’t we make src/cli-get-stdin.js be mocked as if it was something like this?

module.exports = () => Promise.resolve(whatEverWeWant)

@ikatyang

Copy link
Copy Markdown
Member

The question is, how can we tell jest it is the one we want to mock without its reference (we have to expose it to get the reference, otherwise I have no idea how to get it) or module name (there is no module in bundled version).

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

Ah, sorry, I completely forgot that there are no modules in the bundled version -.-

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

What about mocking process.stdin instead of get-stream?

@lydell

lydell commented Sep 29, 2017

Copy link
Copy Markdown
Member Author

I pushed a commit where I tried to mock process.stdin. It doesn't work, though. Any ideas?

@suchipi

suchipi commented Sep 29, 2017

Copy link
Copy Markdown
Member

We might be able to use something like rollup-plugin-alias to mock out the package in just the dist build. But we'd want to make sure it wouldn't break the dist build itself

@ikatyang

Copy link
Copy Markdown
Member

process.stdin is a readonly property.

I've tried to replace it using Object.defineProperty, the replacement did work, but it still can't catch outputs from stderr and get a lot of strange errors.

I think the easiest way here is to expose the reference of get-stream and then mock it.

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

@ikatyang Hehe, I just figured out that process.stdin isn’t writable as well, and my attempts with Object.defineProperty haven’t been successful either. I’ll try exposing the reference to get-stream.

@azz

azz commented Sep 30, 2017

Copy link
Copy Markdown
Member

global.__SECRET_GET_STREAM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED__ = require("get-stream")?

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

@azz I went with another approach. Check the commit above this comment. Thoughts?

Down to only two failures now. This time, --no-color does not seem to work. Investigating.

@azz

azz commented Sep 30, 2017

Copy link
Copy Markdown
Member

Ah, clever. Better than leaking a global.

@ikatyang

Copy link
Copy Markdown
Member

Another another approach: (it works fine to me)

diff --git a/index.js b/index.js
index 25452ce7..394952a5 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/src/cli-util.js b/src/cli-util.js
index 7e570a68..5222ed91 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");
@@ -205,7 +204,7 @@ function applyConfigPrecedence(argv, options) {
 }
 
 function formatStdin(argv) {
-  getStream(process.stdin).then(input => {
+  prettier.__debug.getStream(process.stdin).then(input => {
     const options = getOptionsForFile(argv, process.cwd());
 
     if (listDifferent(argv, input, options, "(stdin)")) {
diff --git a/tests_integration/runPrettier.js b/tests_integration/runPrettier.js
index 42a061e9..e5f8d7fe 100644
--- a/tests_integration/runPrettier.js
+++ b/tests_integration/runPrettier.js
@@ -2,9 +2,10 @@
 
 const fs = require("fs");
 const path = require("path");
-const stream = require("stream");
 
 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 || [];
@@ -46,17 +47,21 @@ function runPrettier(dir, args, options) {
   const originalCwd = process.cwd();
   const originalArgv = process.argv;
   const originalExitCode = process.exitCode;
-  const originalStdin = process.stdin;
+  const originalStdinIsTTY = process.stdin.isTTY;
 
   process.chdir(normalizeDir(dir));
-  process.stdin = new SimpleReadableStream(options.input || "");
   process.stdin.isTTY = !!options.isTTY;
   process.argv = ["path/to/node", "path/to/prettier/bin"].concat(args);
 
   jest.resetModules();
+  jest
+    .spyOn(require(prettierApi).__debug, "getStream")
+    .mockImplementation(() => ({
+      then: handler => handler(options.input || "")
+    }));
 
   try {
-    require(isProduction ? "../dist/bin/prettier" : "../bin/prettier");
+    require(prettierCli);
     status = (status === undefined ? process.exitCode : status) || 0;
   } catch (error) {
     status = 1;
@@ -65,7 +70,7 @@ function runPrettier(dir, args, options) {
     process.chdir(originalCwd);
     process.argv = originalArgv;
     process.exitCode = originalExitCode;
-    process.stdin = originalStdin;
+    process.stdin.isTTY = originalStdinIsTTY;
     jest.restoreAllMocks();
   }
 
@@ -88,18 +93,4 @@ function normalizeDir(dir) {
   return isRelative ? path.resolve(__dirname, dir) : dir;
 }
 
-class SimpleReadableStream extends stream.Readable {
-  constructor(input) {
-    super();
-    this._input = Buffer.from(input);
-  }
-
-  _read() {
-    setImmediate(() => {
-      this.push(this._input);
-      this._input = null;
-    });
-  }
-}
-
 module.exports = runPrettier;

@azz

azz commented Sep 30, 2017

Copy link
Copy Markdown
Member

Surely someone has found an elegant way to solve this one before?

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

@ikatyang Can you make a PR with your version, to make it easier to compare?

@ikatyang

Copy link
Copy Markdown
Member

Sure, #2941.

@azz

azz commented Sep 30, 2017

Copy link
Copy Markdown
Member

I think I prefer this one as it doesn't expose another thing on the main prettier API. (no-one is requiring prettier/bin/prettier AFAIK).

@ikatyang

Copy link
Copy Markdown
Member

The babel thing is weird, how did it get there?!

FAIL  tests_integration/__tests__/stdin-filepath.js
  ● format correctly if stdin content compatible with stdin-filepath
    expect(value).toMatchSnapshot()
    
    Received value does not match stored snapshot 2.
    
    - ""
    + "[BABEL] Note: The code generator has deoptimised the styling of "/home/travis/build/prettier/prettier/dist/parser-postcss.js" as it exceeds the max of "500KB".
    "
      
      at Object.<anonymous>.test (tests_integration/__tests__/stdin-filepath.js:13:25)
          at Promise (<anonymous>)
          at <anonymous>
 › 1 snapshot test failed.

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

I think Jest runs babel on all code by default.

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

Remaining TODOs:

  • Fix the test that fails because of the Babel warning message.
  • Then all the tests pass ... but the production build is broken! The CLI doesn’t work, because the require.main === module check doesn’t work after running rollup :( I’ll either have to solve that somehow, or switch to @ikatyang’s approach (in the other PR).

@lydell

lydell commented Sep 30, 2017

Copy link
Copy Markdown
Member Author

Closing in favor of #2942. While this PR might be nicer in that it doesn’t expose anything, #2942 is better because it actually works.

@lydell lydell closed this Sep 30, 2017
@lock lock Bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jan 19, 2019
@lock lock Bot locked as resolved and limited conversation to collaborators Jan 19, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants