Extract out jasmine reporters

This commit is contained in:
Evan Morikawa 2016-10-14 16:33:27 -04:00
parent 52e38d8dd0
commit 8e786956df
8 changed files with 123 additions and 70 deletions

31
spec/console-reporter.es6 Normal file
View file

@ -0,0 +1,31 @@
const originalLog = console.log;
const originalWarn = console.warn;
const originalError = console.error;
export default class ConsoleReporter {
reportSpecStarting(spec) {
const withContext = (log) => {
return (...args) => {
if (args[0] === '.') {
return log(...args);
}
return log(`[${spec.getFullName()}] ${args[0]}`, ...args.slice(1, -1 + 1));
}
}
console.log = withContext(originalLog);
console.warn = withContext(originalWarn);
console.error = withContext(originalError);
}
reportSpecResults() {
if (console.log !== originalLog) {
console.log = originalLog;
}
if (console.warn !== originalWarn) {
console.warn = originalWarn;
}
if (console.error !== originalError) {
console.error = originalError;
}
}
}

View file

View file

@ -1,50 +1,31 @@
/* eslint global-require:0 */
import { remote } from 'electron';
import jasmineExports from './jasmine';
import {TerminalReporter} from 'jasmine-tagged';
import TimeReporter from './time-reporter'
import N1SpecReporter from './n1-spec-reporter';
import N1GuiReporter from './n1-gui-reporter';
import ConsoleReporter from './console-reporter'
import N1TerminalReporter from './terminal-reporter'
export function runSpecSuite(specSuite) {
for (const key of Object.keys(jasmineExports)) {
window[key] = jasmineExports[key]
}
const timeReporter = new TimeReporter();
const log = (str) => {
return remote.process.stdout.write(str);
};
let reporter = new TerminalReporter({
color: true,
print(str) {
return log(str);
},
onComplete(runner) {
if (runner.results().failedCount > 0) {
return NylasEnv.exit(1);
}
return NylasEnv.exit(0);
},
});
if (NylasEnv.getLoadSettings().showSpecsInWindow) {
reporter = N1SpecReporter
}
NylasEnv.initialize();
require(specSuite);
const consoleReporter = new ConsoleReporter();
const terminalReporter = new N1TerminalReporter();
const jasmineEnv = jasmineExports.jasmine.getEnv();
jasmineEnv.addReporter(reporter);
if (NylasEnv.getLoadSettings().showSpecsInWindow) {
jasmineEnv.addReporter(N1GuiReporter);
} else {
jasmineEnv.addReporter(terminalReporter);
}
jasmineEnv.addReporter(timeReporter);
jasmineEnv.addReporter(consoleReporter);
const div = document.createElement('div');
div.id = 'jasmine-content';
document.body.appendChild(div);
require(specSuite);
return jasmineEnv.execute();
}

View file

@ -58,7 +58,7 @@ suiteString: (spec) ->
descriptions.join("\n")
class N1SpecReporter extends React.Component
class N1GuiReporter extends React.Component
constructor: (@props) ->
render: ->
@ -262,7 +262,7 @@ deprecations = []
plainTextOutput = ""
update = =>
component = <N1SpecReporter
component = <N1GuiReporter
startedAt={startedAt}
specs={specs}
deprecations={deprecations}

View file

@ -3,7 +3,26 @@
global.Promise = require('bluebird');
Promise.longStackTraces();
import jasmineExports from './jasmine';
import NylasEnvConstructor from '../src/nylas-env';
window.NylasEnv = NylasEnvConstructor.loadOrCreate();
import { runSpecSuite } from './jasmine-helper';
Object.assign(window, {
NylasEnv: NylasEnvConstructor.loadOrCreate(),
jasmine: jasmineExports.jasmine,
it: jasmineExports.it,
xit: jasmineExports.xit,
runs: jasmineExports.runs,
waits: jasmineExports.waits,
spyOn: jasmineExports.spyOn,
expect: jasmineExports.expect,
waitsFor: jasmineExports.waitsFor,
describe: jasmineExports.describe,
xdescribe: jasmineExports.xdescribe,
afterEach: jasmineExports.afterEach,
beforeEach: jasmineExports.beforeEach,
})
import {runSpecSuite} from './jasmine-helper';
NylasEnv.initialize();
runSpecSuite('./spec-suite');

View file

@ -54,38 +54,38 @@ jasmine.getEnv().addEqualityTester(_.isEqual) # Use underscore's definition of e
# Custom log reporter to pointpoint console statements
# ----------------------------------------------------
util = require('util')
console.inspect = (args...) ->
arg = args
if (args.length is 1)
arg = args[0]
console.log(util.inspect(arg))
original_log = console.log
original_warn = console.warn
original_error = console.error
class JasmineConsoleReporter
reportSpecStarting: (spec) ->
withContext = (log) ->
return ->
if arguments[0] is '.'
log(arguments...)
else
log("[#{spec.getFullName()}] #{arguments[0]}", Array(arguments)[1..-1]...)
console.log = withContext(original_log)
console.warn = withContext(original_warn)
console.error = withContext(original_error)
reportSpecResults: (result) ->
if console.log isnt original_log
console.log = original_log
if console.warn isnt original_warn
console.warn = original_warn
if console.error isnt original_error
console.error = original_error
jasmine.getEnv().addReporter(new JasmineConsoleReporter())
# util = require('util')
# console.inspect = (args...) ->
# arg = args
# if (args.length is 1)
# arg = args[0]
# console.log(util.inspect(arg))
#
# original_log = console.log
# original_warn = console.warn
# original_error = console.error
#
# class JasmineConsoleReporter
# reportSpecStarting: (spec) ->
# withContext = (log) ->
# return ->
# if arguments[0] is '.'
# log(arguments...)
# else
# log("[#{spec.getFullName()}] #{arguments[0]}", Array(arguments)[1..-1]...)
# console.log = withContext(original_log)
# console.warn = withContext(original_warn)
# console.error = withContext(original_error)
#
# reportSpecResults: (result) ->
# if console.log isnt original_log
# console.log = original_log
# if console.warn isnt original_warn
# console.warn = original_warn
# if console.error isnt original_error
# console.error = original_error
#
# jasmine.getEnv().addReporter(new JasmineConsoleReporter())
#
#
@ -273,7 +273,8 @@ afterEach ->
NylasEnv.themes.removeStylesheet('global-editor-styles')
delete NylasEnv.state?.packageStates
if NylasEnv.state
delete NylasEnv.state.packageStates
unless window.debugContent
document.getElementById('jasmine-content').innerHTML = ''

View file

@ -0,0 +1,20 @@
import { remote } from 'electron';
import {TerminalReporter} from 'jasmine-tagged';
export default class N1TerminalReporter extends TerminalReporter {
constructor(opts = {}) {
const options = Object.assign(opts, {
color: true,
print(str) {
return remote.process.stdout.write(str);
},
onComplete(runner) {
if (runner.results().failedCount > 0) {
return NylasEnv.exit(1);
}
return NylasEnv.exit(0);
},
});
super(options)
}
}

View file

@ -1,5 +1,6 @@
_ = require 'underscore'
_str = require 'underscore.string'
{jasmine} = require('./jasmine')
module.exports =
class TimeReporter extends jasmine.Reporter