UNPKG

poku

Version:

🐷 Poku makes testing easy for Node.js, Bun, Deno, and you at the same time.

177 lines (176 loc) 9.87 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.poku = exports.errors = void 0; const node_path_1 = require("path"); const node_process_1 = require("process"); const indentation_js_1 = require("../../configs/indentation.js"); const poku_js_1 = require("../../configs/poku.js"); const assert_js_1 = require("../../parsers/assert.js"); const find_file_from_stack_js_1 = require("../../parsers/find-file-from-stack.js"); const time_js_1 = require("../../parsers/time.js"); const format_js_1 = require("../format.js"); const write_js_1 = require("../write.js"); const regexFile = /file:(\/\/)?/; exports.errors = []; exports.poku = (() => { return { onRunStart() { }, onFileStart() { }, onDescribeAsTitle(title, options) { const { background, icon } = options ?? Object.create(null); const message = `${icon ?? '☰'} ${(0, format_js_1.format)(title).bold()}`; const noBackground = !background; if (noBackground) (0, write_js_1.log)((0, format_js_1.format)(message).bold()); else (0, write_js_1.log)((0, format_js_1.format)(` ${message} `).bg(typeof background === 'string' ? background : 'grey')); }, onDescribeStart({ title }) { indentation_js_1.indentation.hasDescribe = true; (0, write_js_1.log)((0, format_js_1.format)(`◌ ${title}`).bold().dim()); }, onDescribeEnd({ title, duration }) { indentation_js_1.indentation.hasDescribe = false; (0, write_js_1.log)(`${(0, format_js_1.format)(`● ${title}`).success().bold()} ${(0, format_js_1.format)(`› ${duration.toFixed(6)}ms`) .success() .dim()}`); }, onItStart({ title }) { if (title) { indentation_js_1.indentation.hasItOrTest = true; (0, write_js_1.log)(`${indentation_js_1.indentation.hasDescribe ? ' ' : ''}${(0, format_js_1.format)(`◌ ${title}`).dim()}`); } }, onItEnd({ title, duration }) { indentation_js_1.indentation.hasItOrTest = false; (0, write_js_1.log)(`${indentation_js_1.indentation.hasDescribe ? ' ' : ''}${(0, format_js_1.format)(`● ${title}`).success().bold()} ${(0, format_js_1.format)(`› ${duration.toFixed(6)}ms`) .success() .dim()}`); }, onAssertionSuccess({ message }) { let preIdentation = ''; if (indentation_js_1.indentation.hasDescribe) preIdentation += ' '; if (indentation_js_1.indentation.hasItOrTest) preIdentation += ' '; const output = `${preIdentation}${(0, format_js_1.format)(`✔ ${message}`).success().bold()}`; (0, write_js_1.log)(output); }, onAssertionFailure({ assertOptions: options, error }) { const { cwd } = poku_js_1.GLOBAL; let preIdentation = ''; const { code, actual, expected, operator } = error; const absolutePath = (0, find_file_from_stack_js_1.findFile)(error).replace(regexFile, ''); const file = (0, node_path_1.relative)((0, node_path_1.resolve)(cwd), absolutePath); if (indentation_js_1.indentation.hasDescribe) preIdentation += ' '; if (indentation_js_1.indentation.hasItOrTest) preIdentation += ' '; let message = ''; if (typeof options.message === 'string') message = options.message; else if (options.message instanceof Error) message = options.message.message; else if (typeof options.defaultMessage === 'string') message = options.defaultMessage; const output = message?.trim().length > 0 ? (0, format_js_1.format)(`✘ ${message}`).fail().bold() : (0, format_js_1.format)('✘ Assertion Error').fail().bold(); (0, write_js_1.log)(`${preIdentation}${output}`); file && (0, write_js_1.log)(`${(0, format_js_1.format)(`${preIdentation} File`).dim()} ${file}`); (0, write_js_1.log)(`${(0, format_js_1.format)(`${preIdentation} Code`).dim()} ${code}`); (0, write_js_1.log)(`${(0, format_js_1.format)(`${preIdentation} Operator`).dim()} ${operator}\n`); if (!options?.hideDiff) { const splitActual = (0, assert_js_1.parseResultType)(actual).split('\n'); const splitExpected = (0, assert_js_1.parseResultType)(expected).split('\n'); (0, write_js_1.log)((0, format_js_1.format)(`${preIdentation} ${options?.actual ?? 'Actual'}:`).dim()); for (const line of splitActual) (0, write_js_1.log)(`${preIdentation} ${(0, format_js_1.format)(line).fail().bold()}`); (0, write_js_1.log)(`\n${preIdentation} ${(0, format_js_1.format)(`${options?.expected ?? 'Expected'}:`).dim()}`); for (const line of splitExpected) (0, write_js_1.log)(`${preIdentation} ${(0, format_js_1.format)(line).success().bold()}`); preIdentation = ''; } if (options.throw) { console.error(error); (0, write_js_1.hr)(); } }, onSkipFile({ message }) { (0, write_js_1.log)((0, format_js_1.format)(`◯ ${message}`).info().bold()); }, onSkipModifier({ message }) { (0, write_js_1.log)(`${indentation_js_1.indentation.hasDescribe ? ' ' : ''}${(0, format_js_1.format)(`◯ ${message}`).info().bold()}`); }, onTodoModifier({ message }) { (0, write_js_1.log)(`${indentation_js_1.indentation.hasDescribe ? ' ' : ''}${(0, format_js_1.format)(`● ${message}`).cyan().bold()}`); }, onFileResult({ status, path, duration, output }) { node_process_1.stdout.write('\n'); if (status) { (0, write_js_1.log)(`${(0, format_js_1.format)('›').success().bold()} ${(0, format_js_1.format)(path.relative).success().underline()} ${(0, format_js_1.format)(`› ${duration.toFixed(6)}ms`) .success() .dim()}`); if (output) (0, write_js_1.log)(output); } else (0, write_js_1.log)(`${(0, format_js_1.format)('›').fail().bold()} ${(0, format_js_1.format)(path.relative).fail().underline()} ${(0, format_js_1.format)(`› ${duration.toFixed(6)}ms`) .fail() .dim()}`); if (!status) exports.errors.push({ file: path.relative, output, }); }, onRunResult() { if (exports.errors.length === 0) return; (0, write_js_1.hr)(); (0, write_js_1.log)(`${(0, format_js_1.format)(String(exports.errors.length)).fail().bold()} ${(0, format_js_1.format)('test file(s) failed:').bold()}\n`); for (const i in exports.errors) { if (Object.prototype.hasOwnProperty.call(exports.errors, i)) { const { file, output } = exports.errors[i]; const index = +i; index > 0 && node_process_1.stdout.write('\n'); (0, write_js_1.log)(`${(0, format_js_1.format)(`${index + 1})`) .dim() .bold()} ${(0, format_js_1.format)(file).underline()}`); output && (0, write_js_1.log)(`\n${output}`); } } }, onExit({ results, timespan }) { const success = ` PASS › ${results.passed} `; const failure = ` FAIL › ${results.failed} `; const skips = ` SKIP › ${results.skipped} `; const plans = ` TODO › ${results.todo} `; const inline = results.skipped === 0 || results.todo === 0; let message = ''; if (inline) { message += `${(0, format_js_1.format)(success).bg('green')} ${(0, format_js_1.format)(failure).bg(results.failed === 0 ? 'grey' : 'brightRed')}`; if (results.skipped) message += ` ${(0, format_js_1.format)(skips).bg('brightBlue')}`; if (results.todo) message += ` ${(0, format_js_1.format)(plans).bg('brightBlue')}`; } else { message += `${(0, format_js_1.format)(success.trim()).success().bold()}\n`; message += results.failed === 0 ? (0, format_js_1.format)(`${failure.trim()}\n`).bold() : `${(0, format_js_1.format)(failure.trim()).fail().bold()}\n`; message += `${(0, format_js_1.format)(skips.trim()).info().bold()}\n`; message += `${(0, format_js_1.format)(plans.trim()).info().bold()}`; } (0, write_js_1.hr)(); (0, write_js_1.log)(`${(0, format_js_1.format)(`Start at › ${(0, format_js_1.format)(`${(0, time_js_1.parseTime)(timespan.started)}`).bold()}`).dim()}`); (0, write_js_1.log)(`${(0, format_js_1.format)('Duration › ').dim()}${(0, format_js_1.format)(`${timespan.duration.toFixed(6)}ms`) .bold() .dim()} ${(0, format_js_1.format)(`(±${(0, time_js_1.parseTimeToSecs)(timespan.duration)} seconds)`).dim()}`); (0, write_js_1.log)(`${(0, format_js_1.format)(`Files › ${(0, format_js_1.format)(String(results.passed + results.failed)).bold()}`).dim()}`); (0, write_js_1.log)(`\n${message}\n`); }, }; })();