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
Extract watch mode #2362
Merged
Merged
Extract watch mode #2362
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
95aa7f8
Add watch option to HastMapOptions
rogeliog 4161822
wip
rogeliog c17bc41
Extract runJest and other utility functions
rogeliog 5ce07f1
Start to extract watch to its own module
rogeliog b8d1ea0
Fix flow and lint errors
rogeliog 0032bf7
Add tests to getMaxWorkers and setWatchMode
rogeliog c9e77fc
Use createHasteContext instead of custom method
rogeliog f89cbdb
Keep haste context up to date FS add/remove
rogeliog 63ec6b0
Fix flow and eslint errors
rogeliog 51918c2
Fix caps in require
rogeliog e79d1b7
Extract createHasteContext and logDebugMessages
rogeliog 90a452c
Alphabetize requires and remove extra graceful-fs requires
rogeliog e7c0d09
Fix test interrupution issue with --bail
rogeliog 0504011
Fixes for new watch mode.
cpojer 16b2780
Pass the raw module map from the test runner to the test worker in wa…
cpojer 4a91dd6
Add test for injecting rawModuleMap to workers
rogeliog 7909aeb
Add tests for watch mode and extract KEYS to contstants module
rogeliog File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @emails oncall+jsinfra | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const chalk = require('chalk'); | ||
const TestWatcher = require('../TestWatcher'); | ||
const {KEYS} = require('../constants'); | ||
|
||
const runJestMock = jest.fn(); | ||
|
||
jest.mock('jest-util', () => ({clearLine: () => {}})); | ||
jest.doMock('chalk', () => new chalk.constructor({enabled: false})); | ||
jest.doMock('../constants', () => ({CLEAR: '', KEYS})); | ||
jest.doMock('../runJest', () => (...args) => { | ||
runJestMock(...args); | ||
|
||
// Call the callback | ||
args[args.length - 1]({snapshot: {}}); | ||
|
||
return Promise.resolve(); | ||
}); | ||
|
||
const watch = require('../watch'); | ||
|
||
const USAGE_MESSAGE = ` | ||
Watch Usage | ||
› Press o to only run tests related to changed files. | ||
› Press p to filter by a filename regex pattern. | ||
› Press q to quit watch mode. | ||
› Press Enter to trigger a test run.`; | ||
|
||
afterEach(runJestMock.mockReset); | ||
|
||
describe('Watch mode flows', () => { | ||
let pipe; | ||
let hasteMap; | ||
let argv; | ||
let hasteContext; | ||
let config; | ||
let stdin; | ||
|
||
beforeEach(() => { | ||
pipe = {write: jest.fn()}; | ||
hasteMap = {on: () => {}}; | ||
argv = {}; | ||
hasteContext = {}; | ||
config = {}; | ||
stdin = new MockStdin(); | ||
}); | ||
|
||
it('Runs Jest once by default and shows usage', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
expect(runJestMock).toBeCalledWith(hasteContext, config, argv, pipe, | ||
new TestWatcher({isWatchMode: true}), jasmine.any(Function)); | ||
expect(pipe.write).toBeCalledWith(USAGE_MESSAGE); | ||
}); | ||
|
||
it('Pressing "o" runs test in "only changed files" mode', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
runJestMock.mockReset(); | ||
|
||
stdin.emit(KEYS.O); | ||
|
||
expect(runJestMock).toBeCalled(); | ||
expect(argv).toEqual({ | ||
'_': '', | ||
onlyChanged: true, | ||
watch: true, | ||
watchAll: false, | ||
}); | ||
}); | ||
|
||
it('Pressing "a" runs test in "watch all" mode', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
runJestMock.mockReset(); | ||
|
||
stdin.emit(KEYS.A); | ||
|
||
expect(runJestMock).toBeCalled(); | ||
expect(argv).toEqual({ | ||
'_': '', | ||
onlyChanged: false, | ||
watch: false, | ||
watchAll: true, | ||
}); | ||
}); | ||
|
||
it('Pressing "P" enters pattern mode', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
|
||
// Write a enter pattern mode | ||
stdin.emit(KEYS.P); | ||
expect(pipe.write).toBeCalledWith(' pattern › '); | ||
|
||
// Write a pattern | ||
stdin.emit(KEYS.P); | ||
stdin.emit(KEYS.O); | ||
stdin.emit(KEYS.A); | ||
expect(pipe.write).toBeCalledWith(' pattern › poa'); | ||
|
||
//Runs Jest again | ||
runJestMock.mockReset(); | ||
stdin.emit(KEYS.ENTER); | ||
expect(runJestMock).toBeCalled(); | ||
|
||
//Argv is updated with the current pattern | ||
expect(argv).toEqual({ | ||
'_': ['poa'], | ||
onlyChanged: false, | ||
watch: true, | ||
watchAll: false, | ||
}); | ||
}); | ||
|
||
it('Pressing "ENTER" reruns the tests', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
expect(runJestMock).toHaveBeenCalledTimes(1); | ||
stdin.emit(KEYS.ENTER); | ||
expect(runJestMock).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('Pressing "u" reruns the tests in "update snapshot" mode', () => { | ||
watch(config, pipe, argv, hasteMap, hasteContext, stdin); | ||
runJestMock.mockReset(); | ||
|
||
stdin.emit(KEYS.U); | ||
|
||
expect(runJestMock.mock.calls[0][1]).toEqual({updateSnapshot: true}); | ||
}); | ||
}); | ||
|
||
class MockStdin { | ||
constructor() { | ||
this._callbacks = []; | ||
} | ||
|
||
setRawMode() {} | ||
|
||
resume() {} | ||
|
||
setEncoding() {} | ||
|
||
on(evt, callback) { | ||
this._callbacks.push(callback); | ||
} | ||
|
||
emit(key) { | ||
this._callbacks.forEach(cb => cb(key)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright (c) 2014-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
* | ||
* @flow | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const CLEAR = process.platform === 'win32' ? '\x1Bc' : '\x1B[2J\x1B[3J\x1B[H'; | ||
|
||
const KEYS = { | ||
A: '61', | ||
ARROW_DOWN: '1b5b42', | ||
ARROW_LEFT: '1b5b44', | ||
ARROW_RIGHT: '1b5b43', | ||
ARROW_UP: '1b5b41', | ||
BACKSPACE: process.platform === 'win32' ? '08' : '7f', | ||
CONTROL_C: '03', | ||
CONTROL_D: '04', | ||
ENTER: '0d', | ||
ESCAPE: '1b', | ||
O: '6f', | ||
P: '70', | ||
Q: '71', | ||
QUESTION_MARK: '3f', | ||
U: '75', | ||
}; | ||
|
||
module.exports = {CLEAR, KEYS}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 decided to tests
_createParallelTestRun
instead ofrunTests
even though it is a private method.I did this because the current implementation of
runTests
is a harder to test, mainly because of all of the extra logic that the inner callbacks have. If you prefer I can also spend some time refactoring some of it to makerunTests
easier to test :)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.
Yeah I was thinking that as well. We could do that as a follow-up. TestRunner is one of the oldest parts of Jest and has been through a lot. I'm open to any ideas on how to improve the code to make it more testable. Feel free to create an issues to discuss or a PR to fix.
One thing that is untested as well is the performance test cache and that Jest runs failed tests first on a re-run. This all may break at any time. I'm really grateful you are adding such extensive unit tests for the watch mode, as it was entirely untested previously.