All files / src/__tests__ util.ts

96% Statements 24/25
66.66% Branches 6/9
100% Functions 6/6
94.44% Lines 17/18

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 4447x             47x   4x 10x 10x       47x 292x 292x     47x 55x     47x 17x   17x       17x     47x   47x           47x  
import { createFsFromVolume, Volume } from '..';
import { Link, Node } from '../node';
 
// Turn the done callback into an incremental one that will only fire after being called
// `times` times, failing with the first reported error if such exists.
// Useful for testing callback-style functions with several different fixtures without
// having to clutter the test suite with a multitude of individual tests (like it.each would).
export const multitest = (_done: (err?: Error) => void, times: number) => {
  let err;
  return function done(_err?: Error) {
    err ??= _err;
    if (!--times) _done(_err);
  };
};
 
export const create = (json: { [s: string]: string } = { '/foo': 'bar' }) => {
  const vol = Volume.fromJSON(json);
  return vol;
};
 
export const createFs = (json?) => {
  return createFsFromVolume(create(json));
};
 
export const tryGetChild = (link: Link, name: string): Link => {
  const child = link.getChild(name);
 
  Iif (!child) {
    throw new Error(`expected link to have a child named "${name}"`);
  }
 
  return child;
};
 
export const tryGetChildNode = (link: Link, name: string): Node => tryGetChild(link, name).getNode();
 
const nodeMajorVersion = +process.version.split('.')[0].slice(1);
 
/**
 * The `File` global is available only starting in Node v20. Hence we run the
 * tests only in those versions.
 */
export const onlyOnNode20 = nodeMajorVersion >= 20 ? describe : describe.skip;