src/addContext.js
function (...args) {
  // Check args to see if we should bother continuing
  if ((args.length !== 2) || !isObject(args[0])) {
    log(ERRORS.INVALID_ARGS, 'error');
    return;
  }

  const ctx = args[1];

  // Ensure that context meets the requirements
  if (!_isValidContext(ctx)) {
    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`
   */
  const test = args[0].currentTest || args[0].test;

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

  /* 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) {
    test.context = ctx;
  } else if (Array.isArray(test.context)) {
    // Test has context and context is an array -> push new context
    test.context.push(ctx);
  } else {
    // Test has context and it is not an array -> make it an array, then push new context
    test.context = [ test.context ];
    test.context.push(ctx);
  }
}
function (...args) {
  // Check args to see if we should bother continuing
  if ((args.length !== 2) || !isObject(args[0])) {
    log(ERRORS.INVALID_ARGS, 'error');
    return;
  }

  const ctx = args[1];

  // Ensure that context meets the requirements
  if (!_isValidContext(ctx)) {
    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`
   */
  const test = args[0].currentTest || args[0].test;

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

  /* 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) {
    test.context = ctx;
  } else if (Array.isArray(test.context)) {
    // Test has context and context is an array -> push new context
    test.context.push(ctx);
  } else {
    // Test has context and it is not an array -> make it an array, then push new context
    test.context = [ test.context ];
    test.context.push(ctx);
  }
}
function ( ... args ) { // Check args to see if we should bother continuing if ( ( args . length !== 2 ) || ! isObject ( args [ 0 ] ) ) { log ( ERRORS . INVALID_ARGS , 'error' ) ; return ; } const ctx = args [ 1 ] ; // Ensure that context meets the requirements if ( ! _isValidContext ( ctx ) ) { 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`
   */ const test = args [ 0 ] . currentTest || args [ 0 ] . test ; if ( ! test ) { log ( ERRORS . INVALID_TEST , 'error' ) ; return ; } /* 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 ) { test . context = ctx ; } else if ( Array . isArray ( test . context ) ) { // Test has context and context is an array -> push new context test . context . push ( ctx ) ; } else { // Test has context and it is not an array -> make it an array, then push new context test . context = [ test . context ] ; test . context . push ( ctx ) ; } }
Add context to the test object so it can
be displayed in the mochawesome report

@param {Object} test object
@param {String|Object} context to add
If context is an object, it must have the shape:
{
title: string that is used as context title in the report
value: the context that is to be added
}

Usage:

it('should test something', function () {
someFunctionThatTestsCode();

addContext(this, 'some context to add');

addContext(this, {
title: 'Expected number of something'
value: 42
});

assert('something');
});
Add context to the test object so it can be displayed in the mochawesome report