1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", { value: true });
|
4 | const tslib_1 = require("tslib");
|
5 | const chai = require("chai");
|
6 | const chaiAsPromised = require("chai-as-promised");
|
7 | const _ = require("lodash");
|
8 | const stripAnsi = require("strip-ansi");
|
9 | chai.use(chaiAsPromised);
|
10 | const stdMocks = require('std-mocks');
|
11 | function bufToString(b) {
|
12 | return b.toString('utf8');
|
13 | }
|
14 | exports.output = {
|
15 | get stdout() {
|
16 | return stdMocks
|
17 | .flush({ stderr: false })
|
18 | .stdout
|
19 | .map(bufToString)
|
20 | .map(stripAnsi)
|
21 | .join('');
|
22 | },
|
23 | get stderr() {
|
24 | return stdMocks
|
25 | .flush({ stdout: false })
|
26 | .stderr
|
27 | .map(bufToString)
|
28 | .map(stripAnsi)
|
29 | .join('');
|
30 | },
|
31 | };
|
32 | const env = process.env;
|
33 | function hooks(options) {
|
34 | if (process.env.SHOW_OUTPUT === '1')
|
35 | options.print = true;
|
36 | const mocks = (options.mock || []).map(m => [...m, _.get(m[0], m[1])]);
|
37 | return {
|
38 | before() {
|
39 | mocks.forEach(([object, path, value]) => _.set(object, path, value));
|
40 | if (options.env)
|
41 | process.env = options.env;
|
42 | if (options.stdout || options.stderr) {
|
43 | stdMocks.use({ stdout: !!options.stdout, stderr: !!options.stderr, print: !!options.print });
|
44 | }
|
45 | },
|
46 | after() {
|
47 | mocks.forEach(([object, path, , original]) => _.set(object, path, original));
|
48 | process.env = env;
|
49 | stdMocks.restore();
|
50 | stdMocks.flush();
|
51 | },
|
52 | };
|
53 | }
|
54 | const settings = (builder, opts) => {
|
55 | const prop = (name, value) => {
|
56 | const prop = { enumerable: true };
|
57 | if (typeof value === 'function') {
|
58 | prop.value = (...args) => builder(Object.assign({}, opts, { [name]: value(...args) }));
|
59 | }
|
60 | else {
|
61 | prop.get = () => builder(Object.assign({}, opts, { [name]: value }));
|
62 | }
|
63 | return prop;
|
64 | };
|
65 | const mock = opts.mock = opts.mock || [];
|
66 | return {
|
67 | print: prop('print', true),
|
68 | stdout: prop('stdout', true),
|
69 | stderr: prop('stderr', true),
|
70 | only: prop('only', true),
|
71 | skip: prop('skip', true),
|
72 | env: prop('env', (env = {}) => env),
|
73 | mock: prop('mock', (object, path, value) => mock.concat([[object, path, value]])),
|
74 | };
|
75 | };
|
76 | const __describe = (options = {}) => {
|
77 | return Object.defineProperties((description, cb) => {
|
78 | return ((options.only && describe.only) || (options.skip && describe.skip) || describe)(description, function () {
|
79 | let { before, after } = hooks(options);
|
80 | beforeEach(before);
|
81 | afterEach(after);
|
82 | cb.call(this);
|
83 | });
|
84 | }, settings(__describe, options));
|
85 | };
|
86 | const __it = (options = {}) => {
|
87 | return Object.defineProperties((expectation, cb) => {
|
88 | return ((options.only && it.only) || (options.skip && it.skip) || it)(expectation, async function () {
|
89 | let { before, after } = hooks(options);
|
90 | before();
|
91 | await cb.call(this);
|
92 | after();
|
93 | });
|
94 | }, settings(__it, options));
|
95 | };
|
96 | const _describe = __describe();
|
97 | exports.describe = _describe;
|
98 | const _it = __it();
|
99 | exports.it = _it;
|
100 | const expect = chai.expect;
|
101 | exports.expect = expect;
|
102 | tslib_1.__exportStar(require("./command"), exports);
|
103 | tslib_1.__exportStar(require("./hook"), exports);
|
104 |
|
\ | No newline at end of file |