Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mocha = require("mocha");
- const TestomatClient = require("../client");
- const TRConstants = require("../constants");
- const { parseTest } = require("../util");
- const fs = require('fs');
- const path = require('path');
- const cwd = process.cwd()
- const getTestVideoPath = (originalFilePath) => {
- const VIDEOS_PATH = `${cwd}/cypress/videos`
- const VIDEO_FORMAT = '.mp4'
- const lastPart = originalFilePath.replace('cypress/integration/', '') + VIDEO_FORMAT;
- const videoPath = path.join(VIDEOS_PATH, lastPart);
- console.log({ videoPath })
- return videoPath;
- // return fs.access(videoPath, (err) => {
- // if (err) {
- // console.warn('Video file not found')
- // return null;
- // }
- // return videoPath;
- // })
- }
- // const getTestScreenshotsPath = (originalFilePath) => {
- // const SCREENSHOTS_PATH = `${cwd}/cypress/temp-screenshots`
- // const lastPart = originalFilePath.replace('cypress/integration/', '');
- // const screenshotsPath = path.join(SCREENSHOTS_PATH, lastPart);
- // console.log({ screenshotsPath })
- // // check if ss path exists
- // const ls = fs.readdirSync(screenshotsPath);
- // // if yes:
- // // for (const file of ls) { prepare paths then return }
- // console.log({ ls })
- // // return fs.access(screenshotsPath, (err) => {
- // // if (err) {
- // // console.warn('Video file not found')
- // // return null;
- // // }
- // // return screenshotsPath;
- // // })
- // }
- function MochaReporter(runner, opts) {
- mocha.reporters.Base.call(this, runner);
- let passes = 0;
- let failures = 0;
- const { apiKey } = opts.reporterOptions;
- if (!apiKey) {
- console.log("TESTOMATIO key is empty, ignoring reports");
- return;
- }
- const client = new TestomatClient({ apiKey });
- runner.on("start", () => {
- client.createRun();
- });
- runner.on("pass", (test) => {
- console.log(test)
- const originalFile = test.parent.invocationDetails.originalFile;
- const testVideoPath = getTestVideoPath(originalFile);
- const testScreenshotsPath = getTestScreenshotsPath(originalFile);
- const files = testVideoPath ? [testVideoPath] : []
- console.log({ files })
- passes += 1;
- console.log("pass: %s", test.fullTitle());
- const testId = parseTest(test.title);
- client.addTestRun(testId, TRConstants.PASSED, {
- title: test.title,
- time: test.duration,
- files
- });
- });
- runner.on("fail", (test, err) => {
- failures += 1;
- console.log("fail: %s -- error: %s", test.fullTitle(), err.message);
- const testId = parseTest(test.title);
- client.addTestRun(testId, TRConstants.FAILED, {
- error: err,
- title: test.title,
- time: test.duration,
- });
- });
- runner.on("end", () => {
- console.log("end: %d/%d", passes, passes + failures);
- const status = failures === 0 ? TRConstants.PASSED : TRConstants.FAILED;
- client.updateRunStatus(status);
- });
- }
- // To have this reporter "extend" a built-in reporter uncomment the following line:
- mocha.utils.inherits(MochaReporter, mocha.reporters.Spec);
- module.exports = MochaReporter;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement