All files / lib/internal/test_runner utils.js

95.16% Statements 59/62
91.66% Branches 11/12
100% Functions 5/5
95.16% Lines 59/62

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 6366x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 66x 54x 54x 54x 66x 4x 4x 4x 66x 31x 31x 31x 31x 25x 25x 25x 25x 25x 6x 6x 6x 6x 6x 6x       19x 25x 3x 3x 16x 16x 31x 31x 31x 31x 66x 86x 86x 86x 66x 66x 66x 66x 66x 66x 66x  
'use strict';
const { RegExpPrototypeExec } = primordials;
const { basename } = require('path');
const { createDeferredPromise } = require('internal/util');
const {
  codes: {
    ERR_TEST_FAILURE,
  },
  kIsNodeError,
} = require('internal/errors');
 
const kMultipleCallbackInvocations = 'multipleCallbackInvocations';
const kSupportedFileExtensions = /\.[cm]?js$/;
const kTestFilePattern = /((^test(-.+)?)|(.+[.\-_]test))\.[cm]?js$/;
 
function doesPathMatchFilter(p) {
  return RegExpPrototypeExec(kTestFilePattern, basename(p)) !== null;
}
 
function isSupportedFileType(p) {
  return RegExpPrototypeExec(kSupportedFileExtensions, p) !== null;
}
 
function createDeferredCallback() {
  let calledCount = 0;
  const { promise, resolve, reject } = createDeferredPromise();
  const cb = (err) => {
    calledCount++;
 
    // If the callback is called a second time, let the user know, but
    // don't let them know more than once.
    if (calledCount > 1) {
      if (calledCount === 2) {
        throw new ERR_TEST_FAILURE(
          'callback invoked multiple times',
          kMultipleCallbackInvocations
        );
      }

      return;
    }
 
    if (err) {
      return reject(err);
    }
 
    resolve();
  };
 
  return { promise, cb };
}
 
function isTestFailureError(err) {
  return err?.code === 'ERR_TEST_FAILURE' && kIsNodeError in err;
}
 
module.exports = {
  createDeferredCallback,
  doesPathMatchFilter,
  isSupportedFileType,
  isTestFailureError,
};