Console output
If you’re writing a custom reporter, there’s a good chance
you’ll want to print output to the console, whether that’s the browser’s
developer console or the stdout
stream of a server-side process. In order to
make sure text-based reporters work on every supported platform, jstest
includes an API for writing to the console, no matter what environment your
tests are running on. If you use this API, your reporter will work on any
platform that jstest
supports.
The console API is found on an object called JS.Console
in the browser, or
require('jstest').Console
on CommonJS platforms. It provides several sets
of methods for performing various tasks.
Text output
Console.puts(string)
writes a string to the console, followed by a newline. This will use the developer console in web browsers, theruntime.trace()
method on Adobe AIR, or the stdout stream on sever-side platforms.Console.print(string)
writes the given string to the console, without a trailing newline if the platform supports this. If not, the output is buffered and will be flushed on the nextputs()
call.
Text formatting
The Console
object supports the following methods for formatting text
output. Each method adds the named format to the current state, and all text
printed after calling the command will have that format applied.
In environments that do not support color formatting, these commands are silently ignored.
reset()
resets all formatting attributesbold()
,normal()
set the weight of the textitalic()
,noitalic()
switch between italic and roman textunderline()
,noline()
apply and remove underliningblink()
,noblink()
apply and remove blinking textblack()
,red()
,green()
,yellow()
,blue()
,magenta()
,cyan()
,white()
,nocolor()
set the current text color- Color methods can be prefixed with
bg
, for examplebgyellow()
, to set the current background color
Cursor motion
The following methods can be used in some environments to move the cursor within the terminal window.
cursorUp(n)
,cursorDown(n)
,cursorForward(n)
,cursorBack(n)
move the cursorn
places in the given directioncursorNextLine(n)
,cursorPrevLine(n)
move the cursor byn
linescursorColumn(x)
moves the cursor to columnx
cursorPosition(x,y)
moves the cursor to columnx
, rowy
cursorHide()
,cursorShow()
hides and shows the cursoreraseScreen()
clears the entire terminal screeneraseScreenForward()
,eraseScreenBack()
clears everything either after or before the cursor positioneraseLine()
clears the whole of the current lineeraseLineForward()
,eraseLineBack()
clears the current line either to the right or the left of the cursor
Exit status
On platforms that allow scripts to exit the current process, this command will end the process with the given status number:
Console.exit(status)
The status
parameter is optional and defaults to 0
.
Environment variables
The Console
object also provides a uniform interface for accessing
environment variables on all supported platforms. To read an environment
variable, use this method:
// Returns the value of the FORMAT environment variable var format = Console.envvar('FORMAT');