UNPKG

folio

Version:

A customizable test framework to build your own test frameworks. Foundation for the [Playwright test runner](https://github.com/microsoft/playwright-test).

117 lines 4.01 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); const reporter_1 = require("../reporter"); function toPosixPath(aPath) { return aPath.split(path_1.default.sep).join(path_1.default.posix.sep); } class JSONReporter extends reporter_1.EmptyReporter { constructor() { super(...arguments); this._errors = []; } onBegin(config, suite) { this.config = config; this.suite = suite; } onTimeout() { this.onEnd(); } onError(error) { this._errors.push({ error }); } onEnd() { outputReport({ config: { ...this.config, outputDir: toPosixPath(this.config.outputDir), testDir: toPosixPath(this.config.testDir), }, suites: this.suite.suites.map(suite => this._serializeSuite(suite)).filter(s => s), errors: this._errors }); } _serializeSuite(suite) { if (!suite.findSpec(test => true)) return null; const suites = suite.suites.map(suite => this._serializeSuite(suite)).filter(s => s); return { title: suite.title, file: toPosixPath(path_1.default.relative(this.config.testDir, suite.file)), line: suite.line, column: suite.column, specs: suite.specs.map(test => this._serializeTestSpec(test)), suites: suites.length ? suites : undefined, }; } _serializeTestSpec(spec) { return { title: spec.title, ok: spec.ok(), tests: spec.tests.map(r => this._serializeTest(r)), file: toPosixPath(path_1.default.relative(this.config.testDir, spec.file)), line: spec.line, column: spec.column, }; } _serializeTest(test) { return { slow: test.slow, timeout: test.timeout, annotations: test.annotations, expectedStatus: test.expectedStatus, parameters: test.parameters, // TODO: rename to results. runs: test.results.map(r => this._serializeTestResult(r)) }; } _serializeTestResult(result) { return { workerIndex: result.workerIndex, status: result.status, duration: result.duration, error: result.error, stdout: result.stdout.map(s => stdioEntry(s)), stderr: result.stderr.map(s => stdioEntry(s)), data: result.data, retry: result.retry, }; } } function outputReport(report) { const reportString = JSON.stringify(report, undefined, 2); const outputName = process.env[`FOLIO_JSON_OUTPUT_NAME`]; if (outputName) { fs_1.default.mkdirSync(path_1.default.dirname(outputName), { recursive: true }); fs_1.default.writeFileSync(outputName, reportString); } else { console.log(reportString); } } function stdioEntry(s) { if (typeof s === 'string') return { text: s }; return { buffer: s.toString('base64') }; } exports.default = JSONReporter; //# sourceMappingURL=json.js.map