All files / lib/internal/test_runner utils.js

94.64% Statements 53/56
90% Branches 9/10
100% Functions 4/4
94.64% Lines 53/56

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 5730x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 30x 46x 46x 46x 30x 4x 4x 4x 30x 20x 20x 20x 20x 20x 20x 20x 20x 20x 6x 6x 6x 6x 6x 6x       14x 20x 2x 2x 12x 12x 20x 20x 20x 20x 30x 30x 30x 30x 30x 30x  
'use strict';
const { RegExpPrototypeExec } = primordials;
const { basename } = require('path');
const { createDeferredPromise } = require('internal/util');
const {
  codes: {
    ERR_TEST_FAILURE,
  },
} = 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 };
}
 
module.exports = {
  createDeferredCallback,
  doesPathMatchFilter,
  isSupportedFileType,
};