UNPKG

dts-jest

Version:

A preprocessor for Jest to snapshot test TypeScript declaration (.d.ts) files

73 lines (72 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Runtime = void 0; var pretty_format = require("pretty-format"); var definitions_1 = require("./definitions"); var get_display_line_1 = require("./utils/get-display-line"); var get_trigger_line_1 = require("./utils/get-trigger-line"); var indent_1 = require("./utils/indent"); var Runtime = /** @class */ (function () { function Runtime(filename, config, triggers, snapshots) { var _this = this; this._data_map = new Map(); this._filename = filename; this._config = config; triggers.forEach(function (trigger) { var body_line = (0, get_trigger_line_1.get_trigger_body_line)(trigger.header.line); _this._data_map.set(body_line, { trigger: trigger }); }); snapshots.forEach(function (snapshot) { var data = _this._data_map.get(snapshot.line); data.snapshot = snapshot; }); } Runtime.prototype.get_type_inference_or_diagnostic = function (body_line) { var data = this._data_map.get(body_line); var snapshot = data.snapshot; return snapshot.diagnostic !== undefined ? snapshot.diagnostic : snapshot.inference; }; Runtime.prototype.get_type_inference_or_throw_diagnostic = function (body_line) { var data = this._data_map.get(body_line); var snapshot = data.snapshot; if (snapshot.diagnostic !== undefined) { throw new Error(snapshot.diagnostic); } return snapshot.inference; }; Runtime.prototype.get_type_report = function (body_line) { var trigger = this._data_map.get(body_line).trigger; try { return this._create_report('type', trigger, 'Inferred', 'to be', this.get_type_inference_or_throw_diagnostic(body_line)); } catch (error) { return this._create_report('type', trigger, 'Inferring', 'but throw', error.message); } }; Runtime.prototype.get_value_report = function (body_line, getter) { var trigger = this._data_map.get(body_line).trigger; try { return this._create_report('value', trigger, 'Evaluated', 'to be', pretty_format(getter())); } catch (error) { return this._create_report('value', trigger, 'Evaluating', 'but throw', error.message); } }; Runtime.prototype._create_report = function (kind, trigger, title1, title2, value) { var header = trigger.header, body = trigger.body; var description = header.description === undefined ? '' : "\n".concat(header.description, "\n"); var indented_expression = (0, indent_1.indent)(body.text, definitions_1.runtime_indent_spaces); var indented_value = (0, indent_1.indent)(value, definitions_1.runtime_indent_spaces); var line = (0, get_display_line_1.get_display_line)(kind === 'type' ? (0, get_trigger_line_1.get_trigger_body_line)(header.line) : trigger.footer.line); var line_info = this._config.transpile ? "\n(".concat(this._filename, ":").concat(line, ")\n") : ''; return "".concat(line_info).concat(description, "\n").concat(title1, "\n\n").concat(indented_expression, "\n\n").concat(title2, "\n\n").concat(indented_value, "\n"); }; return Runtime; }()); exports.Runtime = Runtime;