• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RomiC / ya-disk / 24871543156

24 Apr 2026 04:02AM UTC coverage: 98.12% (-1.9%) from 100.0%
24871543156

Pull #618

github

web-flow
Merge 06e8e3711 into 8ef4d0e04
Pull Request #618: feat(#612): migrate test runner from Jest to node:test

188 of 206 branches covered (91.26%)

Branch coverage included in aggregate %.

180 of 206 new or added lines in 13 files covered. (87.38%)

2109 of 2135 relevant lines covered (98.78%)

5.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.74
/tests/node-test-setup.cjs
1
const fs = require('node:fs');
26✔
2
const path = require('node:path');
26✔
3
const Module = require('node:module');
26✔
4
const nodeTest = require('node:test');
26✔
5
const { ModuleMocker } = require('jest-mock');
26✔
6

26✔
7
const expectExport = require('expect');
26✔
8
const expect = expectExport.expect || expectExport.default || expectExport;
26!
9

26✔
10
const moduleMocker = new ModuleMocker(global);
26✔
11
const moduleMocks = new Map();
26✔
12
const originalLoad = Module._load;
26✔
13

26✔
14
const getCallerFile = () => {
26✔
15
  const err = new Error();
26✔
16
  const stack = String(err.stack || '').split('\n').slice(2);
26!
17

26✔
18
  for (const line of stack) {
26✔
19
    const match = line.match(/\((.*?):\d+:\d+\)$/) || line.match(/at (.*?):\d+:\d+$/);
52!
20
    if (!match) {
52!
NEW
21
      continue;
×
NEW
22
    }
×
23

52✔
24
    const filePath = match[1];
52✔
25
    if (
52✔
26
      filePath &&
52✔
27
      !filePath.endsWith('node-test-setup.cjs') &&
52✔
28
      filePath.includes(path.sep)
26✔
29
    ) {
52✔
30
      return filePath;
26✔
31
    }
26✔
32
  }
52✔
NEW
33

×
NEW
34
  return null;
×
35
};
26✔
36

26✔
37
const resolveMockExport = (request, parentModule) => {
26✔
38
  if (!request.startsWith('.') && !request.startsWith('/')) {
26✔
39
    const coreMockPath = path.join(process.cwd(), 'tests', '__mocks__', `${request}.js`);
4✔
40
    if (fs.existsSync(coreMockPath)) {
4✔
41
      return require(coreMockPath);
4✔
42
    }
4✔
NEW
43

×
NEW
44
    return null;
×
NEW
45
  }
×
46

22✔
47
  const resolved = Module._resolveFilename(request, parentModule);
22✔
48
  const mockPath = path.join(
22✔
49
    path.dirname(resolved),
22✔
50
    '__mocks__',
22✔
51
    path.basename(resolved)
22✔
52
  );
22✔
53

22✔
54
  if (fs.existsSync(mockPath)) {
22✔
55
    return require(mockPath);
22✔
56
  }
22✔
NEW
57

×
NEW
58
  return null;
×
59
};
26✔
60

26✔
61
Module._load = function patchedLoad(request, parent, isMain) {
26✔
62
  const resolved = Module._resolveFilename(request, parent, isMain);
279✔
63
  if (moduleMocks.has(resolved)) {
279✔
64
    return moduleMocks.get(resolved);
48✔
65
  }
48✔
66

231✔
67
  return originalLoad.apply(this, arguments);
231✔
68
};
26✔
69

26✔
70
const wrapDoneCallback = (fn) => async () =>
26✔
71
  await new Promise((resolve, reject) => {
8✔
72
    let doneCalled = false;
8✔
73
    const done = (err) => {
8✔
74
      if (doneCalled) {
8!
NEW
75
        return;
×
NEW
76
      }
×
77
      doneCalled = true;
8✔
78

8✔
79
      if (err) {
8!
NEW
80
        reject(err);
×
81
      } else {
8✔
82
        resolve();
8✔
83
      }
8✔
84
    };
8✔
85

8✔
86
    try {
8✔
87
      fn(done);
8✔
88
    } catch (error) {
8!
NEW
89
      reject(error);
×
NEW
90
    }
×
91
  });
26✔
92

26✔
93
const wrapHook = (fn) => {
26✔
94
  if (typeof fn !== 'function') {
42!
NEW
95
    return fn;
×
NEW
96
  }
×
97

42✔
98
  if (fn.length > 0) {
42!
NEW
99
    return wrapDoneCallback(fn);
×
NEW
100
  }
×
101

42✔
102
  return fn;
42✔
103
};
26✔
104

26✔
105
const wrapTest = (fn) => {
26✔
106
  if (typeof fn !== 'function') {
98!
NEW
107
    return fn;
×
NEW
108
  }
×
109

98✔
110
  if (fn.length > 0) {
98✔
111
    return wrapDoneCallback(fn);
8✔
112
  }
8✔
113

90✔
114
  return fn;
90✔
115
};
26✔
116

26✔
117
global.expect = expect;
26✔
118
global.test = (name, options, fn) => {
26✔
119
  if (typeof options === 'function') {
98✔
120
    return nodeTest.test(name, wrapTest(options));
98✔
121
  }
98✔
NEW
122
  return nodeTest.test(name, options, wrapTest(fn));
×
123
};
26✔
124
global.it = global.test;
26✔
125
global.describe = (name, options, fn) => {
26✔
126
  if (typeof options === 'function') {
36✔
127
    return nodeTest.describe(name, wrapHook(options));
36✔
128
  }
36✔
NEW
129
  return nodeTest.describe(name, options, wrapHook(fn));
×
130
};
26✔
131
global.beforeEach = (fn, options) => nodeTest.beforeEach(wrapHook(fn), options);
26✔
132
global.afterEach = (fn, options) => nodeTest.afterEach(wrapHook(fn), options);
26✔
133
global.beforeAll = (fn, options) => nodeTest.before(wrapHook(fn), options);
26✔
134
global.afterAll = (fn, options) => nodeTest.after(wrapHook(fn), options);
26✔
135

26✔
136
nodeTest.beforeEach(() => moduleMocker.clearAllMocks());
26✔
137

26✔
138
global.jest = {
26✔
139
  fn: moduleMocker.fn.bind(moduleMocker),
26✔
140
  clearAllMocks: moduleMocker.clearAllMocks.bind(moduleMocker),
26✔
141
  mock(request) {
26✔
142
    const caller = getCallerFile();
26✔
143
    if (!caller) {
26!
NEW
144
      throw new Error(`Unable to resolve caller for jest.mock('${request}')`);
×
NEW
145
    }
×
146

26✔
147
    const parentModule = {
26✔
148
      id: caller,
26✔
149
      filename: caller,
26✔
150
      paths: Module._nodeModulePaths(path.dirname(caller))
26✔
151
    };
26✔
152

26✔
153
    const mockExport = resolveMockExport(request, parentModule);
26✔
154
    if (!mockExport) {
26!
NEW
155
      throw new Error(`Unable to resolve manual mock for '${request}'`);
×
NEW
156
    }
×
157

26✔
158
    const resolved = Module._resolveFilename(request, parentModule);
26✔
159
    moduleMocks.set(resolved, mockExport);
26✔
160
    delete require.cache[resolved];
26✔
161
  }
26✔
162
};
26✔
163

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc