-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit adds two test cases to test the `--watch` option. We check that touching a test file reruns the tests and we test that touching a file that has a correct extensions reruns the test. This commit adds `fs-extra` as a new dev dependency.
- Loading branch information
Showing
5 changed files
with
222 additions
and
57 deletions.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,56 +1,173 @@ | ||
'use strict'; | ||
|
||
var helpers = require('../helpers'); | ||
var runMochaJSONRaw = helpers.runMochaJSONRaw; | ||
const fs = require('fs-extra'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const helpers = require('../helpers'); | ||
const runMochaJSONRawAsync = helpers.runMochaJSONRawAsync; | ||
|
||
const sigintExitCode = 130; | ||
|
||
describe('--watch', function() { | ||
var args = []; | ||
describe('when enabled', function() { | ||
this.timeout(10 * 1000); | ||
this.slow(3000); | ||
|
||
before(function() { | ||
args = ['--watch']; | ||
}); | ||
beforeEach(function() { | ||
this.tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'mocha-')); | ||
|
||
describe('when enabled', function() { | ||
before(function() { | ||
const fixtureSource = helpers.DEFAULT_FIXTURE; | ||
|
||
this.testFile = path.join(this.tempDir, 'test.js'); | ||
fs.copySync(fixtureSource, this.testFile); | ||
}); | ||
|
||
afterEach(function() { | ||
if (this.tempDir) { | ||
return fs.remove(this.tempDir); | ||
} | ||
}); | ||
|
||
it('should show the cursor and signal correct exit code, when watch process is terminated', function() { | ||
// Feature works but SIMULATING the signal (ctrl+c) via child process | ||
// does not work due to lack of POSIX signal compliance on Windows. | ||
if (process.platform === 'win32') { | ||
this.skip(); | ||
} | ||
}); | ||
|
||
it('should show the cursor and signal correct exit code, when watch process is terminated', function(done) { | ||
this.timeout(0); | ||
this.slow(3000); | ||
|
||
var fixture = 'exit.fixture.js'; | ||
var spawnOpts = {stdio: 'pipe'}; | ||
var mocha = runMochaJSONRaw( | ||
fixture, | ||
args, | ||
function postmortem(err, data) { | ||
if (err) { | ||
return done(err); | ||
} | ||
|
||
var expectedCloseCursor = '\u001b[?25h'; | ||
const [mocha, resultPromise] = runMochaJSONRawAsync([ | ||
helpers.DEFAULT_FIXTURE, | ||
'--watch' | ||
]); | ||
|
||
return sleep(1000) | ||
.then(() => { | ||
mocha.kill('SIGINT'); | ||
return resultPromise; | ||
}) | ||
.then(data => { | ||
const expectedCloseCursor = '\u001b[?25h'; | ||
expect(data.output, 'to contain', expectedCloseCursor); | ||
|
||
function exitStatusBySignal(sig) { | ||
return 128 + sig; | ||
} | ||
expect(data.code, 'to be', sigintExitCode); | ||
}); | ||
}); | ||
|
||
it('reruns test when watched test file is touched', function() { | ||
const [mocha, outputPromise] = runMochaJSONWatchAsync([this.testFile], { | ||
cwd: this.tempDir | ||
}); | ||
|
||
var sigint = 2; | ||
expect(data.code, 'to be', exitStatusBySignal(sigint)); | ||
done(); | ||
}, | ||
spawnOpts | ||
return expect( | ||
sleep(1000) | ||
.then(() => { | ||
touchFile(this.testFile); | ||
return sleep(1000); | ||
}) | ||
.then(() => { | ||
mocha.kill('SIGINT'); | ||
return outputPromise; | ||
}), | ||
'when fulfilled', | ||
'to have length', | ||
2 | ||
); | ||
}); | ||
|
||
setTimeout(function() { | ||
// Kill the child process | ||
mocha.kill('SIGINT'); | ||
}, 1000); | ||
it('reruns test when file matching extension is touched', function() { | ||
const watchedFile = path.join(this.tempDir, 'file.xyz'); | ||
touchFile(watchedFile); | ||
const [mocha, outputPromise] = runMochaJSONWatchAsync( | ||
[this.testFile, '--extension', 'xyz,js'], | ||
{ | ||
cwd: this.tempDir | ||
} | ||
); | ||
|
||
return expect( | ||
sleep(1000) | ||
.then(() => { | ||
touchFile(watchedFile); | ||
return sleep(1000); | ||
}) | ||
.then(() => { | ||
mocha.kill('SIGINT'); | ||
return outputPromise; | ||
}), | ||
'when fulfilled', | ||
'to have length', | ||
2 | ||
); | ||
}); | ||
|
||
it('ignores files in "node_modules" and ".git"', function() { | ||
const nodeModulesFile = path.join( | ||
this.tempDir, | ||
'node_modules', | ||
'file.xyz' | ||
); | ||
const gitFile = path.join(this.tempDir, '.git', 'file.xyz'); | ||
|
||
touchFile(gitFile); | ||
touchFile(nodeModulesFile); | ||
|
||
const [mocha, outputPromise] = runMochaJSONWatchAsync( | ||
[this.testFile, '--extension', 'xyz,js'], | ||
{ | ||
cwd: this.tempDir | ||
} | ||
); | ||
|
||
return expect( | ||
sleep(1000) | ||
.then(() => { | ||
touchFile(gitFile); | ||
touchFile(nodeModulesFile); | ||
}) | ||
.then(() => sleep(1000)) | ||
.then(() => { | ||
mocha.kill('SIGINT'); | ||
return outputPromise; | ||
}), | ||
'when fulfilled', | ||
'to have length', | ||
1 | ||
); | ||
}); | ||
}); | ||
}); | ||
|
||
/** | ||
* Invokes the mocha binary with the `--watch` argument for the given fixture. | ||
* | ||
* Returns child process and a promise for the test results. The test results | ||
* are an array of JSON objects generated by the JSON reporter. | ||
*/ | ||
function runMochaJSONWatchAsync(args, spawnOpts) { | ||
args = [...args, '--watch']; | ||
const [mocha, mochaDone] = runMochaJSONRawAsync(args, spawnOpts); | ||
const testResults = mochaDone.then(data => { | ||
const testResults = data.output | ||
// eslint-disable-next-line no-control-regex | ||
.replace(/\u001b\[\?25./g, '') | ||
.split('\u001b[2K') | ||
.map(x => JSON.parse(x)); | ||
return testResults; | ||
}); | ||
return [mocha, testResults]; | ||
} | ||
|
||
/** | ||
* Synchronously touch a file by appending a space to the end. Creates | ||
* the file and all its parent directories if necessary. | ||
*/ | ||
function touchFile(file) { | ||
fs.ensureDirSync(path.dirname(file)); | ||
fs.appendFileSync(file, ' '); | ||
} | ||
|
||
function sleep(time) { | ||
return new Promise(resolve => { | ||
setTimeout(resolve, time); | ||
}); | ||
} |