Skip to content
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

Optional jasmine 2 support #330

Closed
wants to merge 31 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
64ce193
update jasmine to 2.2.x
akhomchenko Apr 14, 2015
3cf19a8
Merge remote-tracking branch 'origin/master' into update_jasmine
akhomchenko Apr 14, 2015
816583a
fix JasmineReporter tests
akhomchenko Apr 14, 2015
b43c1d7
move to jasmine2TestRunner and enable testRunner config
tomv564 Apr 19, 2015
4d9e1a9
restore original jasmineTestRunner, deps and tests
tomv564 Apr 19, 2015
1d9306a
Merge pull request #1 from tomv564/update_jasmine
akhomchenko May 20, 2015
b515a5e
change jasmine-pit dependency
akhomchenko May 20, 2015
6b8bb74
Merge remote-tracking branch 'upstream/master' into update_jasmine
akhomchenko May 20, 2015
135c477
Populate ancestorTitles from suiteStarted and suiteDone
tomv564 May 24, 2015
af3e354
add --noHightLight
tomv564 May 25, 2015
5a8b418
Refactor jasmine2 JasmineReporter to have private instance methods
tomv564 May 25, 2015
ff4bb8a
Pass jest runtime to test setup scripts for jasmine 2
tomv564 May 25, 2015
4bc03f7
fix jasmine2 reporter suite display, make normalizeConfig test jasmin…
tomv564 May 25, 2015
00e9544
resolve test results on jasmineDone, improve test compatibility
tomv564 May 25, 2015
b3c1c54
fix jasmine2 matchers and add tests
tomv564 May 25, 2015
84e6d1c
move runners & reporters, extra common reporter logic
tomv564 May 25, 2015
0d84d5a
extract common runner logic
tomv564 May 25, 2015
e63ef2a
clean up old test fix
tomv564 May 25, 2015
ebd2500
Merge pull request #2 from tomv564/update_jasmine
akhomchenko Jun 2, 2015
d944326
remove messages from custom matchers in jasmine 2 because confusing o…
tomv564 Jun 4, 2015
17f4dc6
clean stack traces, jasmine2 isNot hack for diffable matchers
tomv564 Jun 4, 2015
8afeb13
add documentation for config.testRunner
tomv564 Jun 4, 2015
554847d
fix stack trace cleaning when installed under jest-cli
tomv564 Jun 4, 2015
30bc8cb
Merge pull request #3 from tomv564/update_jasmine
akhomchenko Jun 5, 2015
706c7d4
update to latest Jasmine 2.x
tomv564 Jul 24, 2015
912a29a
forgot jasmine itself
tomv564 Jul 24, 2015
71ed085
remove old jasmine
tomv564 Jul 24, 2015
f375790
update jasminereporter, too
tomv564 Jul 24, 2015
b034d98
Merge branch 'master' into update_jasmine
tomv564 Jul 24, 2015
508b315
Merge pull request #4 from tomv564/update_jasmine
akhomchenko Jul 25, 2015
a7cb87d
Merge remote-tracking branch 'upstream/master' into update_jasmine
akhomchenko Nov 15, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Check out the [Getting Started](http://facebook.github.io/jest/docs/getting-star
- [`config.testPathDirs` [array<string>]](http://facebook.github.io/jest/docs/api.html#config-testpathdirs-array-string)
- [`config.testPathIgnorePatterns` [array<string>]](http://facebook.github.io/jest/docs/api.html#config-testpathignorepatterns-array-string)
- [`config.testPathPattern` [string]](http://facebook.github.io/jest/docs/api.html#config-testpathpattern-string)
- [`config.testRunner` [string]](http://facebook.github.io/jest/docs/api.html#config-testrunner-string)
- [`config.unmockedModulePathPatterns` [array<string>]](http://facebook.github.io/jest/docs/api.html#config-unmockedmodulepathpatterns-array-string)
- [`config.verbose` [boolean]](http://facebook.github.io/jest/docs/api.html#config-verbose-boolean)

Expand Down
6 changes: 6 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ permalink: docs/api.html
- [`config.testPathDirs` [array<string>]](#config-testpathdirs-array-string)
- [`config.testPathIgnorePatterns` [array<string>]](#config-testpathignorepatterns-array-string)
- [`config.testPathPattern` [string]](http://facebook.github.io/jest/docs/api.html#config-testpathpattern-string)
- [`config.testRunner` [string]](http://facebook.github.io/jest/docs/api.html#config-testrunner-string)
- [`config.unmockedModulePathPatterns` [array<string>]](#config-unmockedmodulepathpatterns-array-string)
- [`config.verbose` [boolean]](#config-verbose-boolean)

Expand Down Expand Up @@ -414,6 +415,11 @@ A regexp pattern string that is matched against all test paths before executing

This is useful if you need to override the default. If you are testing one file at a time the default will be set to `/.*/`, however if you pass a blob rather than a single file the default will then be the absolute path of each test file. The override may be needed on windows machines where, for example, the test full path would be `C:/myproject/__tests__/mystest.jsx.jest` and the default pattern would be set as `/C:\myproject\__tests__\mystest.jsx.jest/`.

### `config.testRunner` [string]
(default: `./jasmineTestRunner/jasmineTestRunner`)

Allows overriding the default jasmine test runner with the bundled jasmine 2.2 test runner: `./jasmineTestRunner/jasmine2TestRunner`.

### `config.unmockedModulePathPatterns` [array<string>]
(default: `[]`)

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"jasmine-pit": "^2.0.2",
"jsdom": "7.0.2",
"json-stable-stringify": "^1.0.0",
"lodash.template": "^3.6.2",
"jasmine-pit": "gagoman/jasmine-pit#jasmine_2_support",
"mkdirp": "^0.5.1",
"node-haste": "^1.2.8",
"object-assign": "^4.0.1",
Expand Down
2 changes: 1 addition & 1 deletion src/DefaultTestReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DefaultTestReporter {
? (testResult.perfStats.end - testResult.perfStats.start) / 1000
: null;

let testDetail = [];
const testDetail = [];
if (testRunTime !== null) {
testDetail.push(
testRunTime > 2.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('HasteModuleLoader', function() {
return buildLoader().then(function(loader) {
expect(function() {
loader.requireModule(null, 'DoesntExist');
}).toThrow('Cannot find module \'DoesntExist\' from \'.\'');
}).toThrow(new Error('Cannot find module \'DoesntExist\' from \'.\''));
});
});

Expand Down Expand Up @@ -95,9 +95,9 @@ describe('HasteModuleLoader', function() {
return buildLoader().then(function(loader) {
expect(function() {
loader.requireModule(__filename, './DoesntExist');
}).toThrow(
}).toThrow(new Error(
'Cannot find module \'./DoesntExist\' from \'' + __filename + '\''
);
));
});
});

Expand Down
88 changes: 88 additions & 0 deletions src/jasmineTestRunner/Jasmine2Reporter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2014, 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.
*/
'use strict';

var jasmineRequire = require('../../vendor/jasmine/jasmine-2.3.4.js');
var jasmine = jasmineRequire.core(jasmineRequire);
var JasmineFormatter = require('./jasmineFormatter');

function Jasmine2Reporter(config) {
this._config = config || {};
this._formatter = new JasmineFormatter(jasmine, config);
this._resultsDeferred = Promise.defer();
this._testResults = [];
this._currentSuites = [];
}

Jasmine2Reporter.prototype.specDone = function(result) {
this._testResults.push(this._extractSpecResults(result,
this._currentSuites.slice(0)));
};

Jasmine2Reporter.prototype.suiteStarted = function(suite) {
this._currentSuites.push(suite.description);
};

Jasmine2Reporter.prototype.suiteDone = function() {
this._currentSuites.pop();
};

Jasmine2Reporter.prototype.jasmineDone = function() {

var numFailingTests = 0;
var numPassingTests = 0;

this._testResults.forEach(function(testResult) {
if (testResult.failureMessages.length > 0) {
numFailingTests++;
} else {
numPassingTests++;
}
});

this._resultsDeferred.resolve({
numFailingTests: numFailingTests,
numPassingTests: numPassingTests,
testResults: this._testResults,
});
};

Jasmine2Reporter.prototype.getResults = function() {
return this._resultsDeferred.promise;
};

Jasmine2Reporter.prototype._extractSpecResults =
function(specResult, currentSuites) {
var results = {
title: 'it ' + specResult.description,
ancestorTitles: currentSuites,
failureMessages: [],
logMessages: [], // Jasmine 2 does not have a logging interface
numPassingAsserts: 0, // Jasmine 2 only returns an array of failed asserts.
};

specResult.failedExpectations.forEach(function(failed) {
if (!failed.matcherName) { // exception

results.failureMessages.push(
this._formatter.formatException(failed.stack)
);

} else { // match failure

results.failureMessages.push(
this._formatter.formatMatchFailure(failed)
);

}
}.bind(this));

return results;
};

module.exports = Jasmine2Reporter;
151 changes: 13 additions & 138 deletions src/jasmineTestRunner/JasmineReporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,12 @@
*/
'use strict';

var colors = require('../lib/colors');
var diff = require('diff');
var formatMsg = require('../lib/utils').formatMsg;
var jasmine = require('../../vendor/jasmine/jasmine-1.3.0').jasmine;

var ERROR_TITLE_COLOR = colors.RED + colors.BOLD + colors.UNDERLINE;
var DIFFABLE_MATCHERS = {
toBe: true,
toNotBe: true,
toEqual: true,
toNotEqual: true,
};
var LINEBREAK_REGEX = /[\r\n]/;
var JasmineFormatter = require('./jasmineFormatter');

function JasmineReporter(config) {
jasmine.Reporter.call(this);
this._formatter = new JasmineFormatter(jasmine, config);
this._config = config || {};
this._logs = [];
this._resultsPromise = new Promise(resolve => this._resolve = resolve);
Expand Down Expand Up @@ -93,57 +83,22 @@ function(container, ancestorTitles, spec) {
switch (result.type) {
case 'expect':
if (result.passed()) {

results.numPassingAsserts++;

// Exception thrown
} else if (!result.matcherName && result.trace.stack) {
// jasmine doesn't give us access to the actual Error object, so we
// have to regexp out the message from the stack string in order to
// colorize the `message` value
result.trace.stack = result.trace.stack.replace(
/(^(.|\n)*?(?=\n\s*at\s))/,
this._formatMsg('$1', ERROR_TITLE_COLOR)
);

result.trace.stack = this._config.noStackTrace
? result.trace.stack.split('\n').slice(0, 2).join('\n')
: result.trace.stack;

results.failureMessages.push(result.trace.stack);

results.failureMessages.push(
this._formatter.formatException(result.trace.stack)
);

// Matcher failed
} else {
var message;
if (DIFFABLE_MATCHERS[result.matcherName]) {
var ppActual = this._prettyPrint(result.actual);
var ppExpected = this._prettyPrint(result.expected);
var colorDiff = this._highlightDifferences(ppActual, ppExpected);

var matcherName = (result.isNot ? 'NOT ' : '') + result.matcherName;

message =
this._formatMsg('Expected:', ERROR_TITLE_COLOR) +
' ' + colorDiff.a +
' ' + this._formatMsg(matcherName + ':', ERROR_TITLE_COLOR) +
' ' + colorDiff.b;
} else {
message = this._formatMsg(result.message, ERROR_TITLE_COLOR);
}

if (result.trace.stack) {
// Replace the error message with a colorized version of the error
message = result.trace.stack.replace(result.trace.message, message);

// Remove the 'Error: ' prefix from the stack trace
message = message.replace(/^.*Error:\s*/, '');

// Remove jasmine jonx from the stack trace
message = message.split('\n').filter(function(line) {
return !/vendor\/jasmine\//.test(line);
});
message = this._config.noStackTrace ? message.slice(0, 2) : message;
message = message.join('\n');
}

results.failureMessages.push(message);

results.failureMessages.push(
this._formatter.formatMatchFailure(result)
);
}
break;
default:
Expand All @@ -156,84 +111,4 @@ function(container, ancestorTitles, spec) {
container.push(results);
};

JasmineReporter.prototype._highlightDifferences = function(a, b) {
var differ;
if (a.match(LINEBREAK_REGEX) || b.match(LINEBREAK_REGEX)) {
// `diff` uses the Myers LCS diff algorithm which runs in O(n+d^2) time
// (where "d" is the edit distance) and can get very slow for large edit
// distances. Mitigate the cost by switching to a lower-resolution diff
// whenever linebreaks are involved.
differ = diff.diffLines;
} else {
differ = diff.diffChars;
}
var changes = differ(a, b);
var ret = {a: '', b: ''};
var change;
for (var i = 0, il = changes.length; i < il; i++) {
change = changes[i];
if (change.added) {
ret.b += this._formatMsg(change.value, colors.RED_BG);
} else if (change.removed) {
ret.a += this._formatMsg(change.value, colors.RED_BG);
} else {
ret.a += change.value;
ret.b += change.value;
}
}
return ret;
};

JasmineReporter.prototype._prettyPrint = function(obj, indent, cycleWeakMap) {
if (!indent) {
indent = '';
}

if (typeof obj === 'object' && obj !== null) {
if (jasmine.isDomNode(obj)) {
var attrStr = '';
Array.prototype.forEach.call(obj.attributes, function(attr) {
var attrName = attr.nodeName.trim();
var attrValue = attr.nodeValue.trim();
attrStr += ' ' + attrName + '="' + attrValue + '"';
});
return 'HTMLNode(' +
'<' + obj.tagName + attrStr + '>[...]</' + obj.tagName + '>' +
')';
}

if (!cycleWeakMap) {
cycleWeakMap = new WeakMap();
}

if (cycleWeakMap.get(obj) === true) {
return '<circular reference>';
}
cycleWeakMap.set(obj, true);

var orderedKeys = Object.keys(obj).sort();
var value;
var keysOutput = [];
var keyIndent = this._formatMsg('|', colors.GRAY) + ' ';
for (var i = 0; i < orderedKeys.length; i++) {
if (orderedKeys[i] === '__jstest_pp_cycle__') {
continue;
}
value = obj[orderedKeys[i]];
keysOutput.push(
indent + keyIndent + orderedKeys[i] + ': ' +
this._prettyPrint(value, indent + keyIndent, cycleWeakMap)
);
}
delete obj.__jstest_pp_cycle__;
return '{\n' + keysOutput.join(',\n') + '\n' + indent + '}';
} else {
return jasmine.pp(obj);
}
};

JasmineReporter.prototype._formatMsg = function(msg, color) {
return formatMsg(msg, color, this._config);
};

module.exports = JasmineReporter;
Loading