New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Echo test mocks #708
Echo test mocks #708
Conversation
Codecov Report
@@ Coverage Diff @@
## dev #708 +/- ##
======================================
Coverage ? 96.63%
======================================
Files ? 33
Lines ? 1160
Branches ? 0
======================================
Hits ? 1121
Misses ? 39
Partials ? 0
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems mostly good, just had a few questions inline.
Any reason this needs to be merged onto dev instead of master?
Also, can you add some mention of the mocks in the PR description? I imagine we may use these mocks in other tests, so it's worth mentioning.
test/echo.js
Outdated
t.is(stdout, '555\n'); | ||
t.end(); | ||
}); | ||
test.skip('simple test with silent(true)', t => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason why we're skipping this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think because this is testing with config.silent = true;
, but we already have set shell.config.silent = true
above on line 7, so this test is redundant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, why not delete it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I will delete this test.
test/utils/mocks.js
Outdated
@@ -0,0 +1,73 @@ | |||
/* eslint-disable prefer-rest-params, strict */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we disabling linting?
test/utils/mocks.js
Outdated
|
||
function init() { | ||
resetValues(); | ||
console.log = consoleLog; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to mock console.log
and console.error
? What happens if we just mock process.stdout
& .stderr
objects? That might give us more reliable results overall.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that's probably a better thing to do in this scenario. I actually just copied this over from the shx mocks. I'll reduce this down to just the process.stdout
and process.stderr
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, SGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@freitagbr can you manually confirm that ava still outputs the result of the test (pass/fail)? I want to make sure the mocks don't interfere with test reporting.
test/utils/mocks.js
Outdated
process.exit = oldProcessExit; | ||
} | ||
exports.restore = restore; | ||
function Mocks() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason we're using mocks as a prototype? I like the idea in general, but we're mocking global values. Using prototypes gives the illusion that multiple mocks can be created, which wouldn't work very well (the second mock would be mocking the first one).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good point. I implemented it as a prototype because I though it was a good way to implement this. But, if you have several mocks going at once, the true process.stdout.write
will get swapped around in them, and it may be hard to figure out where it went, unless you restore
in the right order. This seems like it could be problematic, so I will revert this to the global version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might be good to enforce that init()
can only be called once though (since calling it twice will lose the real process.stdout
). Maybe we can just implement this as 2 streams for process.stdout
and process.stdin
, then we don't have to reinvent the stream logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've re-implemented the mocks using globals, and calling init()
or restore()
any number of times will not lose the real process.stdout.write
, nor the mock functions
Yes, the tests still output during these tests, even with the mocks. In fact, that spinny thing is still shown, so I don't think the mocks interfere with anything. |
Also, the reason I merged this into dev instead of master is because of the extra unit tests added in the newline echo branch. I figured it would just be easier to refactor all the tests at once, instead of fixing merge conflicts later on. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* Add stdout/stderr test mocks * Mock stdout/stderr during echo tests * Fix lint issues * Use 'use strict' * Re-implement mocks as a prototype * Implement mocks as a single-instance * Remove redundant test * Create mocked stdout/stderr.write methods once
* Add stdout/stderr test mocks * Mock stdout/stderr during echo tests * Fix lint issues * Use 'use strict' * Re-implement mocks as a prototype * Implement mocks as a single-instance * Remove redundant test * Create mocked stdout/stderr.write methods once
Fixes #622
Adds mocks for
process.stdout.write
andprocess.stderr.write
that allows the written values to be extracted. This way, we can inspect the output ofecho
without actually outputting to stdout.