All files / lib/internal/test_runner harness.js

98.95% Statements 190/192
95.45% Branches 42/44
100% Functions 12/12
98.95% Lines 190/192

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 19365x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x 124x 124x 17x 17x 17x 17x     17x 17x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 4x 4x 4x 124x 124x 65x 348x 348x 286x 286x 62x 62x 14943x 559x 559x 559x 14384x 14384x 14384x 14943x 5583x 5583x 62x 62x 6128x 6128x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 324x 324x 324x 324x 324x 29x 324x 10x 295x 14x 284x 59x 271x 212x 212x 324x 62x 62x 62x 62x 13x 13x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 62x 20x 20x 62x 62x 62x 2x 2x 62x 62x 62x 62x 62x 62x 348x 8x 8x 8x 62x 62x 62x 62x 62x 62x 348x 65x 251x 251x 251x 251x 65x 130x 130x 233x 233x 233x 97x 97x 233x 130x 130x 228x 130x 130x 130x 260x 5x 260x 130x 130x 130x 65x 260x 260x 12x 12x 260x 260x 65x 65x 65x 65x 65x 65x 65x 65x 65x 65x  
'use strict';
const {
  ArrayPrototypeForEach,
  FunctionPrototypeBind,
  SafeMap,
} = primordials;
const {
  createHook,
  executionAsyncId,
} = require('async_hooks');
const {
  codes: {
    ERR_TEST_FAILURE,
  },
} = require('internal/errors');
const { getOptionValue } = require('internal/options');
const { Test, ItTest, Suite } = require('internal/test_runner/test');
 
const isTestRunner = getOptionValue('--test');
const testResources = new SafeMap();
const root = new Test({ __proto__: null, name: '<root>' });
let wasRootSetup = false;
 
function createProcessEventHandler(eventName, rootTest) {
  return (err) => {
    // Check if this error is coming from a test. If it is, fail the test.
    const test = testResources.get(executionAsyncId());
 
    if (!test) {
      throw err;
    }
 
    if (test.finished) {
      // If the test is already finished, report this as a top level
      // diagnostic since this is a malformed test.
      const msg = `Warning: Test "${test.name}" generated asynchronous ` +
        'activity after the test ended. This activity created the error ' +
        `"${err}" and would have caused the test to fail, but instead ` +
        `triggered an ${eventName} event.`;
 
      rootTest.diagnostic(msg);
      return;
    }
 
    test.fail(new ERR_TEST_FAILURE(err, eventName));
    test.postRun();
  };
}
 
function setup(root) {
  if (wasRootSetup) {
    return root;
  }
  const hook = createHook({
    init(asyncId, type, triggerAsyncId, resource) {
      if (resource instanceof Test) {
        testResources.set(asyncId, resource);
        return;
      }
 
      const parent = testResources.get(triggerAsyncId);
 
      if (parent !== undefined) {
        testResources.set(asyncId, parent);
      }
    },
    destroy(asyncId) {
      testResources.delete(asyncId);
    }
  });
 
  hook.enable();
 
  const exceptionHandler =
    createProcessEventHandler('uncaughtException', root);
  const rejectionHandler =
    createProcessEventHandler('unhandledRejection', root);
 
  const exitHandler = () => {
    root.postRun();
 
    let passCount = 0;
    let failCount = 0;
    let skipCount = 0;
    let todoCount = 0;
    let cancelledCount = 0;
 
    for (let i = 0; i < root.subtests.length; i++) {
      const test = root.subtests[i];
 
      // Check SKIP and TODO tests first, as those should not be counted as
      // failures.
      if (test.skipped) {
        skipCount++;
      } else if (test.isTodo) {
        todoCount++;
      } else if (test.cancelled) {
        cancelledCount++;
      } else if (!test.passed) {
        failCount++;
      } else {
        passCount++;
      }
    }
 
    root.reporter.plan(root.indent, root.subtests.length);
 
    for (let i = 0; i < root.diagnostics.length; i++) {
      root.reporter.diagnostic(root.indent, root.diagnostics[i]);
    }
 
    root.reporter.diagnostic(root.indent, `tests ${root.subtests.length}`);
    root.reporter.diagnostic(root.indent, `pass ${passCount}`);
    root.reporter.diagnostic(root.indent, `fail ${failCount}`);
    root.reporter.diagnostic(root.indent, `cancelled ${cancelledCount}`);
    root.reporter.diagnostic(root.indent, `skipped ${skipCount}`);
    root.reporter.diagnostic(root.indent, `todo ${todoCount}`);
    root.reporter.diagnostic(root.indent, `duration_ms ${process.uptime()}`);
 
    root.reporter.push(null);
    hook.disable();
    process.removeListener('unhandledRejection', rejectionHandler);
    process.removeListener('uncaughtException', exceptionHandler);
 
    if (failCount > 0 || cancelledCount > 0) {
      process.exitCode = 1;
    }
  };
 
  const terminationHandler = () => {
    exitHandler();
    process.exit();
  };
 
  process.on('uncaughtException', exceptionHandler);
  process.on('unhandledRejection', rejectionHandler);
  process.on('beforeExit', exitHandler);
  // TODO(MoLow): Make it configurable to hook when isTestRunner === false.
  if (isTestRunner) {
    process.on('SIGINT', terminationHandler);
    process.on('SIGTERM', terminationHandler);
  }
 
  root.reporter.pipe(process.stdout);
  root.reporter.version();
 
  wasRootSetup = true;
  return root;
}
 
function test(name, options, fn) {
  const subtest = setup(root).createSubtest(Test, name, options, fn);
  return subtest.start();
}
 
function runInParentContext(Factory) {
  function run(name, options, fn, overrides) {
    const parent = testResources.get(executionAsyncId()) || setup(root);
    const subtest = parent.createSubtest(Factory, name, options, fn, overrides);
    if (parent === root) {
      subtest.start();
    }
  }
 
  const cb = (name, options, fn) => {
    run(name, options, fn);
  };
 
  ArrayPrototypeForEach(['skip', 'todo'], (keyword) => {
    cb[keyword] = (name, options, fn) => {
      run(name, options, fn, { [keyword]: true });
    };
  });
  return cb;
}
 
function hook(hook) {
  return (fn, options) => {
    const parent = testResources.get(executionAsyncId()) || setup(root);
    parent.createHook(hook, fn, options);
  };
}
 
module.exports = {
  test: FunctionPrototypeBind(test, root),
  describe: runInParentContext(Suite),
  it: runInParentContext(ItTest),
  before: hook('before'),
  after: hook('after'),
  beforeEach: hook('beforeEach'),
  afterEach: hook('afterEach'),
};