UNPKG

2.13 kBJavaScriptView Raw
1function setup() {
2 try { require('anux-common'); } catch (error) { /* do nothing */ }
3 let enableReact = false;
4 try { enableReact = !!require('react'); } catch (error) { /* do nothing */ }
5 const chai = require('chai');
6 const spies = require('chai-spies');
7 const fuzzy = require('chai-fuzzy');
8 const asPromised = require('chai-as-promised');
9
10 chai.use(spies);
11 chai.use(fuzzy);
12 chai.use(asPromised);
13
14 global['chai'] = chai;
15 global['expect'] = chai.expect;
16
17 if (enableReact) {
18 const { JSDOM } = require('jsdom');
19 const { window } = new JSDOM('<!doctype html><html><body></body></html>', {
20 pretendToBeVisual: false,
21 userAgent: 'mocha',
22 });
23
24 const React = require('react');
25 const enzyme = require('enzyme');
26 const Adapter = require('@wojtekmaj/enzyme-adapter-react-17');
27
28 global['React'] = React;
29 global['window'] = window;
30 global['document'] = window.document;
31 global['navigator'] = window.navigator;
32 global['enzyme'] = enzyme;
33
34 const readWriteNumericProperty = {
35 value: 0,
36 enumerable: true,
37 configurable: true,
38 writable: true,
39 };
40
41 // set HTMLElement properties to be read-write
42 Object.defineProperties(window.HTMLElement.prototype, {
43 clientWidth: readWriteNumericProperty,
44 clientHeight: readWriteNumericProperty,
45 scrollWidth: readWriteNumericProperty,
46 scrollHeight: readWriteNumericProperty,
47 });
48
49 // mock the resize observer
50 window['ResizeObserver'] = function (delegate) {
51 window['resizeObserver'] = this;
52 this.observe = () => void 0;
53 this.unobserve = () => void 0;
54 this.triggerWith = entries => delegate(entries);
55 };
56
57 enzyme.configure({ adapter: new Adapter() });
58 }
59}
60
61// mocha can't call the function so we have to do it for it
62// const config = process.env['test-config'] && JSON.parse(process.env['test-config']);
63// eslint-disable-next-line mocha/no-top-level-hooks, mocha/no-hooks-for-single-case
64if (process.env['is-mocha']) { setup(); }
65
66module.exports = setup;