Repository navigation
Conversation
|
Almost all of the failing are of the type “Expected |
|
It seems caused by this mock, I have no idea how to mock it without touching its source code... prettier/tests_integration/runPrettier.js Line 53 in 9d448fb |
|
@ikatyang Nice find! I guess we could make a |
|
To mock, we have to get its reference first, but since they're bundled, how can we get it from outside? |
|
Or we can expose it as something like |
|
Can’t we make module.exports = () => Promise.resolve(whatEverWeWant) |
|
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). |
|
Ah, sorry, I completely forgot that there are no modules in the bundled version -.- |
|
What about mocking |
|
I pushed a commit where I tried to mock process.stdin. It doesn't work, though. Any ideas? |
|
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 |
|
I've tried to replace it using I think the easiest way here is to expose the reference of |
|
@ikatyang Hehe, I just figured out that |
|
|
|
@azz I went with another approach. Check the commit above this comment. Thoughts? Down to only two failures now. This time, |
|
Ah, clever. Better than leaking a global. |
|
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; |
|
Surely someone has found an elegant way to solve this one before? |
|
@ikatyang Can you make a PR with your version, to make it easier to compare? |
|
Sure, #2941. |
|
I think I prefer this one as it doesn't expose another thing on the main prettier API. (no-one is requiring |
|
The babel thing is weird, how did it get there?! |
|
I think Jest runs babel on all code by default. |
|
Remaining TODOs:
|
The good: This catches the errors that #2930 fixes!
The bad: Other tests fail :(
Production test summary:
Details
With #2930 applied:
Details
I have not figured out what causes the test failures yet. Help appreciated! :)