-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show progress while running tests
- Loading branch information
Showing
7 changed files
with
214 additions
and
41 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
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,39 @@ | ||
const Logger = require('./Logger'); | ||
|
||
/** | ||
* This logger can buffer multiple calls to .log() and then | ||
* flush them all at once to STDOUT. Useful for printing test | ||
* suite output all at once for some terminals. | ||
*/ | ||
class LoggerBufferable extends Logger { | ||
constructor (...args) { | ||
super(...args); | ||
|
||
this.isBuffering = false; | ||
this.queue = ''; | ||
} | ||
|
||
buffer () { | ||
this.isBuffering = true; | ||
} | ||
|
||
flush () { | ||
this.isBuffering = false; | ||
super.write(this.queue); | ||
this.queue = ''; | ||
} | ||
|
||
push (data) { | ||
this.queue += data; | ||
} | ||
|
||
write (data) { | ||
if (this.isBuffering) { | ||
this.push(data); | ||
} else { | ||
super.write(data); | ||
} | ||
} | ||
} | ||
|
||
module.exports = LoggerBufferable; |
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,70 @@ | ||
const LoggerBufferable = require('./LoggerBufferable'); | ||
|
||
const getLineCount = (str) => { | ||
let count = 1; | ||
let pos = -1; | ||
|
||
while ((pos = str.indexOf('\n', pos + 1)) > -1) { | ||
count++; | ||
} | ||
|
||
return count; | ||
}; | ||
|
||
/** | ||
* This logger allows to print temporary output at the very bottom | ||
* that will be erased and overwritten on the next call to .log(). | ||
* Used when showing temporary progress when streaming test suite results. | ||
*/ | ||
class LoggerTemporal extends LoggerBufferable { | ||
constructor (...args) { | ||
super(...args); | ||
|
||
this.linesToErase = 0; | ||
this.isTemporary = false; | ||
this.queueTemporary = ''; | ||
} | ||
|
||
temporary () { | ||
if (!this.isBuffering) { | ||
throw new Error('You need to call .buffer() before .temporary().'); | ||
} | ||
this.isTemporary = true; | ||
} | ||
|
||
push (data) { | ||
if (this.isTemporary) { | ||
this.queueTemporary += data; | ||
} else { | ||
super.push(data); | ||
} | ||
} | ||
|
||
flush () { | ||
super.flush(); | ||
|
||
this.write(this.queueTemporary); | ||
this.linesToErase = getLineCount(this.queueTemporary) - 1; | ||
|
||
this.queueTemporary = ''; | ||
this.isTemporary = false; | ||
} | ||
|
||
write (data) { | ||
const {stream} = this; | ||
|
||
if (this.linesToErase && stream.moveCursor) { | ||
for (let index = 0; index < this.linesToErase; index++) { | ||
stream.cursorTo(0); | ||
stream.clearLine(); | ||
stream.moveCursor(0, -1); | ||
} | ||
|
||
this.linesToErase = 0; | ||
} | ||
|
||
super.write(data); | ||
} | ||
} | ||
|
||
module.exports = LoggerTemporal; |
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,3 @@ | ||
const chalk = require('chalk'); | ||
|
||
module.exports = (line) => chalk`{hidden #} ${line}`; |
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,3 @@ | ||
const {format} = require('util'); | ||
|
||
module.exports = (...args) => format(...args); |