UNPKG

1.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const engine_1 = require("@dxcli/engine");
4const util_1 = require("util");
5const _1 = require(".");
6exports.testHook = (event, hookOpts = {}, opts = {}, fn) => {
7 const description = opts.description || `run hook with opts: ${util_1.inspect(hookOpts)}`;
8 let test = _1.it;
9 if (opts.stdout)
10 test = test.stdout;
11 if (opts.stderr)
12 test = test.stderr;
13 test(description, async () => {
14 const engine = new engine_1.Engine();
15 await engine.load(opts.root || module.parent.parent.filename);
16 const run = () => engine.runHook(event, hookOpts);
17 let err;
18 if (typeof opts.exit === 'number') {
19 try {
20 await run();
21 throw new Error(`Expected hook to exit with code ${opts.exit} but it ran without exiting`);
22 }
23 catch (e) {
24 err = e;
25 if (!err['cli-ux'] || typeof err['cli-ux'].exit !== 'number')
26 throw err;
27 if (err['cli-ux'].exit !== opts.exit) {
28 throw new Error(`Expected hook to exit with ${opts.exit} but exited with ${err['cli-ux'].exit}`);
29 }
30 }
31 }
32 else
33 await run();
34 if (typeof opts.stdout === 'string')
35 _1.expect(_1.output.stdout).to.equal(opts.stdout);
36 if (typeof opts.stderr === 'string')
37 _1.expect(_1.output.stderr).to.equal(opts.stderr);
38 if (!fn)
39 return;
40 let o = {};
41 o.error = err;
42 if (opts.stdout)
43 o.stdout = _1.output.stdout;
44 if (opts.stderr)
45 o.stderr = _1.output.stderr;
46 await fn(o);
47 });
48};
49//# sourceMappingURL=hook.js.map
\No newline at end of file