« get me outta code hell

test-lib.js « test - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/test/test-lib.js
blob: a12974cdd04eb7d66335ec5824ae45cd0017d3a4 (plain)
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
import {compositeFrom} from '#composite';

Error.stackTraceLimit = Infinity;

export function quickCheckCompositeOutputs(t, dependencies) {
  return (step, outputDict) => {
    t.same(
      Object.keys(step.toDescription().outputs),
      Object.keys(outputDict));

    const composite = compositeFrom({
      compose: false,
      steps: [
        step,

        {
          dependencies: Object.keys(outputDict),

          // Access all dependencies by their expected keys -
          // the composition runner actually provides a proxy
          // and is checking that *we* access the dependencies
          // we've specified.
          compute: dependencies =>
            Object.fromEntries(
              Object.keys(outputDict)
                .map(key => [key, dependencies[key]])),
        },
      ],
    });

    t.same(
      composite.expose.compute(dependencies),
      outputDict);
  };
}

export function strictlyThrows(t, fn, pattern) {
  const error = catchErrorOrNull(fn);

  t.currentAssert = strictlyThrows;

  if (error === null) {
    t.fail(`expected to throw`);
    return;
  }

  const nameAndMessage = `${pattern.constructor.name} ${pattern.message}`;
  t.match(
    prepareErrorForMatch(error),
    prepareErrorForMatch(pattern),
    (pattern instanceof AggregateError
      ? `expected to throw: ${nameAndMessage} (${pattern.errors.length} error(s))`
      : `expected to throw: ${nameAndMessage}`));
}

function prepareErrorForMatch(error) {
  if (error instanceof RegExp) {
    return {
      message: error,
    };
  }

  if (!(error instanceof Error)) {
    return error;
  }

  const matchable = {
    name: error.constructor.name,
    message: error.message,
  };

  if (error instanceof AggregateError) {
    matchable.errors = error.errors.map(prepareErrorForMatch);
  }

  return matchable;
}

function catchErrorOrNull(fn) {
  try {
    fn();
    return null;
  } catch (error) {
    return error;
  }
}