UNPKG

@helpscout/cyan

Version:

Cypress-like Testing for React + JSDOM

92 lines (71 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.inspect = exports.openBrowser = void 0; var _rimraf = _interopRequireDefault(require("rimraf")); var _path = _interopRequireDefault(require("path")); var _child_process = require("child_process"); var _gadget = _interopRequireDefault(require("./gadget")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* global jasmine */ var baseDir = _path.default.join(process.cwd(), '/.cyan'); // Caching the native Promise implementation, in case it gets stubbed. var _Promise = Promise; var cachedProcess; /** * Caching the existing Jasmine timeout to be modified * and restored before/after every inspect call */ var _defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; /** * Setting up our inspector. * * The Jasmine timers have to be cached and reset here. * The inspector forces the timer to be super long, in order * to accomodate the debugging/inspect experience. * * We need to restore the timer to the original state to * ensure subsequent tests run as expected. */ beforeEach(function () { _defaultTimeoutInterval = jasmine.DEFAULT_TIMEOUT_INTERVAL; }); afterEach(function () { jasmine.DEFAULT_TIMEOUT_INTERVAL = _defaultTimeoutInterval; }); var cleanUp = function () { _rimraf.default.sync(baseDir); }; var inspector = function (process) { return new _Promise(function (resolve) { process.on('exit', function () { cleanUp(); resolve(); }); }); }; var openBrowser = function () { (0, _child_process.spawn)('open', ['http://localhost:3000'], { stdio: 'inherit' }); }; exports.openBrowser = openBrowser; var inspect = function () { jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000000; if (cachedProcess && cachedProcess.exit) { cachedProcess.exit(0); cachedProcess = null; } (0, _gadget.default)(); var scriptpath = _path.default.join(__dirname, 'brain'); var brainProcess = (0, _child_process.fork)(scriptpath); cachedProcess = brainProcess; openBrowser(); return inspector(brainProcess); }; exports.inspect = inspect; process.on('SIGINT', function () { cleanUp(); }); var _default = inspect; exports.default = _default;