Skip to content

Commit

Permalink
update docs/dist [ci skip]
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam Gruber committed May 9, 2017
1 parent c96e4ad commit 60d7c9c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mochawesome

Mochawesome is a custom reporter for use with the Javascript testing framework, [mocha][]. It runs on Node.js (>=4) and generates a full fledged HTML/CSS report that helps visualize your test suites.

## :tada: New in 2.1.0
## :tada: Latest Changes
- Use `addContext` in `beforeEach` and `afterEach` test hooks
- New [options](#options): `overwrite` and `timestamp`

See the [CHANGELOG][] for up-to-date changes.
Expand Down
37 changes: 30 additions & 7 deletions dist/addContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@ var stringify = require('json-stringify-safe');
var errorPrefix = 'Error adding context:';
var ERRORS = {
INVALID_ARGS: errorPrefix + ' Invalid arguments.',
INVALID_CONTEXT: errorPrefix + ' Expected a string or an object of shape { title: string, value: any } but saw:'
INVALID_TEST: errorPrefix + ' Invalid test object.',
INVALID_CONTEXT: function INVALID_CONTEXT(ctx) {
var expected = 'Expected a string or an object of shape { title: string, value: any } but saw:';
return errorPrefix + ' ' + expected + '\n' + stringify(ctx, function (key, val) {
return val === undefined ? 'undefined' : val;
}, 2);
}
};

/**
Expand All @@ -35,9 +41,10 @@ function _isValidContext(ctx) {
/*
* Context is valid if any of the following are true:
* 1. Type is string and it is not empty
* 2. Type is object and it has properties 'title' and 'value'
* 2. Type is object and it has properties `title` and `value` and `title` is not empty
*/
return typeof ctx === 'string' && !isEmpty(ctx) || Object.hasOwnProperty.call(ctx, 'title') && Object.hasOwnProperty.call(ctx, 'value');
if (!ctx) return false;
return typeof ctx === 'string' && !isEmpty(ctx) || Object.hasOwnProperty.call(ctx, 'title') && !isEmpty(ctx.title) && Object.hasOwnProperty.call(ctx, 'value');
}

/**
Expand Down Expand Up @@ -75,7 +82,7 @@ var addContext = function addContext() {
}

// Check args to see if we should bother continuing
if (args.length !== 2 || !isObject(args[0]) || !args[0].test) {
if (args.length !== 2 || !isObject(args[0])) {
log(ERRORS.INVALID_ARGS, 'error');
return;
}
Expand All @@ -84,12 +91,28 @@ var addContext = function addContext() {

// Ensure that context meets the requirements
if (!_isValidContext(ctx)) {
log(ERRORS.INVALID_CONTEXT + '\n' + stringify(ctx, null, 2), 'error');
log(ERRORS.INVALID_CONTEXT(ctx), 'error');
return;
}

/* Context is valid, now get the test object
* If `addContext` is called from inside a `beforeEach` or `afterEach`
* the test object will be `.currentTest`, otherwise just `.test`
*/
var test = args[0].currentTest || args[0].test;

if (!test) {
log(ERRORS.INVALID_TEST, 'error');
return;
}

// Context is valid we can proceed
var test = args[0].test;
/* If context is an object, and value is `undefined`
* change it to 'undefined' so it can be displayed
* correctly in the report
*/
if (ctx.title && ctx.value === undefined) {
ctx.value = 'undefined';
}

// Test doesn't already have context -> set it
if (!test.context) {
Expand Down
21 changes: 18 additions & 3 deletions dist/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,26 @@ function cleanTest(test) {
err: err,
isRoot: test.parent && test.parent.root,
uuid: test.uuid || /* istanbul ignore next: default */uuid.v4(),
parentUUID: test.parent && test.parent.uuid
parentUUID: test.parent && test.parent.uuid,
isHook: test.type === 'hook'
};

cleaned.skipped = !cleaned.pass && !cleaned.fail && !cleaned.pending;

return cleaned;
}

/**
* Filters all failed hooks from suite
* And concatenates them to a single array
*
* @param {Object} suite
*/
function getFailedHooks(suite) {
var failedHooks = [].concat(suite._afterAll, suite._afterEach, suite._beforeAll, suite._beforeEach);
return _.filter(failedHooks, { state: 'failed' });
}

/**
* Mutates the suite object to add properties needed to render
* the template and remove unused properties.
Expand All @@ -171,7 +183,7 @@ function cleanTest(test) {
*/
function cleanSuite(suite, totalTestsRegistered) {
suite.uuid = uuid.v4();

var failedHooks = _.map(getFailedHooks(suite), cleanTest);
var cleanTests = _.map(suite.tests, cleanTest);
var passingTests = _.filter(cleanTests, { state: 'passed' });
var failingTests = _.filter(cleanTests, { state: 'failed' });
Expand All @@ -186,13 +198,15 @@ function cleanSuite(suite, totalTestsRegistered) {
totalTestsRegistered.total += suite.tests.length;

suite.tests = cleanTests;
suite.failedHooks = failedHooks;
suite.fullFile = suite.file || '';
suite.file = suite.file ? suite.file.replace(process.cwd(), '') : '';
suite.passes = passingTests;
suite.failures = failingTests;
suite.pending = pendingTests;
suite.skipped = skippedTests;
suite.hasTests = suite.tests.length > 0;
suite.hasFailedHooks = suite.failedHooks.length > 0;
suite.hasSuites = suite.suites.length > 0;
suite.totalTests = suite.tests.length;
suite.totalPasses = passingTests.length;
Expand All @@ -206,7 +220,7 @@ function cleanSuite(suite, totalTestsRegistered) {
suite.duration = duration;
suite.rootEmpty = suite.root && suite.totalTests === 0;

removeAllPropsFromObjExcept(suite, ['title', 'fullFile', 'file', 'tests', 'suites', 'passes', 'failures', 'pending', 'skipped', 'hasTests', 'hasSuites', 'totalTests', 'totalPasses', 'totalFailures', 'totalPending', 'totalSkipped', 'hasPasses', 'hasFailures', 'hasPending', 'hasSkipped', 'root', 'uuid', 'duration', 'rootEmpty', '_timeout']);
removeAllPropsFromObjExcept(suite, ['title', 'fullFile', 'file', 'tests', 'failedHooks', 'suites', 'passes', 'failures', 'pending', 'skipped', 'hasTests', 'hasFailedHooks', 'hasSuites', 'totalTests', 'totalPasses', 'totalFailures', 'totalPending', 'totalSkipped', 'hasPasses', 'hasFailures', 'hasPending', 'hasSkipped', 'root', 'uuid', 'duration', 'rootEmpty', '_timeout']);
}

/**
Expand Down Expand Up @@ -241,5 +255,6 @@ module.exports = {
cleanCode: cleanCode,
cleanTest: cleanTest,
cleanSuite: cleanSuite,
getFailedHooks: getFailedHooks,
traverseSuites: traverseSuites
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mochawesome",
"version": "2.1.0",
"version": "2.2.0",
"description": "A Gorgeous HTML/CSS Reporter for Mocha.js",
"scripts": {
"lint": "eslint src test",
Expand Down

0 comments on commit 60d7c9c

Please sign in to comment.