UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2// tslint:disable no-namespace
3Object.defineProperty(exports, "__esModule", { value: true });
4const tslib_1 = require("tslib");
5const chai = require("chai");
6const chaiAsPromised = require("chai-as-promised");
7const _ = require("lodash");
8const stripAnsi = require("strip-ansi");
9chai.use(chaiAsPromised);
10const stdMocks = require('std-mocks');
11function bufToString(b) {
12 return b.toString('utf8');
13}
14exports.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};
32const env = process.env;
33function 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}
54const 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};
76const __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};
86const __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};
96const _describe = __describe();
97exports.describe = _describe;
98const _it = __it();
99exports.it = _it;
100const expect = chai.expect;
101exports.expect = expect;
102tslib_1.__exportStar(require("./command"), exports);
103tslib_1.__exportStar(require("./hook"), exports);
104//# sourceMappingURL=index.js.map
\No newline at end of file