xref: /third_party/node/deps/npm/test/fixtures/tmock.js (revision 1cb0ef41)
1const path = require('path')
2
3const ROOT = path.resolve(__dirname, '../..')
4const BIN = path.join(ROOT, 'bin')
5const LIB = path.join(ROOT, 'lib')
6
7// since mock npm changes directories it can be hard to figure out the
8// correct path to mock something with tap since the directory will change
9// before/after npm is loaded. This helper replaces {BIN} and {LIB} with
10// the absolute path to those directories
11const replace = (s) => {
12  if (/^[./{]/.test(s)) {
13    return s
14      .replace(/^\{BIN\}/, BIN)
15      .replace(/^\{LIB\}/, LIB)
16      .replace(/^\{ROOT\}/, ROOT)
17  } else {
18    return require.resolve(s)
19  }
20}
21
22const tmock = (t, p, mocks = {}) => {
23  const entries = Object.entries(mocks).map(([k, v]) => [replace(k), v])
24  return t.mock(replace(p), Object.fromEntries(entries))
25}
26
27module.exports = tmock
28