Advertisement
mvsil

Untitled

Jan 12th, 2022
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const mocha = require("mocha");
  2. const TestomatClient = require("../client");
  3. const TRConstants = require("../constants");
  4. const { parseTest } = require("../util");
  5. const fs = require('fs');
  6. const path = require('path');
  7.  
  8. const cwd = process.cwd()
  9.  
  10. const getTestVideoPath = (originalFilePath) => {
  11.   const VIDEOS_PATH = `${cwd}/cypress/videos`
  12.   const VIDEO_FORMAT = '.mp4'
  13.  
  14.   const lastPart = originalFilePath.replace('cypress/integration/', '') + VIDEO_FORMAT;
  15.  
  16.   const videoPath = path.join(VIDEOS_PATH, lastPart);
  17.  
  18.   console.log({ videoPath })
  19.  
  20.   return videoPath;
  21.  
  22.   // return fs.access(videoPath, (err) => {
  23.   //   if (err) {
  24.   //     console.warn('Video file not found')
  25.   //     return null;
  26.   //   }
  27.  
  28.   //   return videoPath;
  29.   // })
  30. }
  31.  
  32. // const getTestScreenshotsPath = (originalFilePath) => {
  33. //   const SCREENSHOTS_PATH = `${cwd}/cypress/temp-screenshots`
  34.  
  35. //   const lastPart = originalFilePath.replace('cypress/integration/', '');
  36.  
  37. //   const screenshotsPath = path.join(SCREENSHOTS_PATH, lastPart);
  38.  
  39. //   console.log({ screenshotsPath })
  40.  
  41. //   // check if ss path exists
  42.  
  43. //   const ls = fs.readdirSync(screenshotsPath);
  44.  
  45. //   // if yes:
  46.  
  47. //   // for (const file of ls) { prepare paths then return }
  48.  
  49. //   console.log({ ls })
  50.  
  51. //   // return fs.access(screenshotsPath, (err) => {
  52. //   //   if (err) {
  53. //   //     console.warn('Video file not found')
  54. //   //     return null;
  55. //   //   }
  56.  
  57. //   //   return screenshotsPath;
  58. //   // })
  59. // }
  60.  
  61. function MochaReporter(runner, opts) {
  62.   mocha.reporters.Base.call(this, runner);
  63.   let passes = 0;
  64.   let failures = 0;
  65.  
  66.   const { apiKey } = opts.reporterOptions;
  67.   if (!apiKey) {
  68.     console.log("TESTOMATIO key is empty, ignoring reports");
  69.     return;
  70.   }
  71.   const client = new TestomatClient({ apiKey });
  72.  
  73.   runner.on("start", () => {
  74.     client.createRun();
  75.   });
  76.  
  77.   runner.on("pass", (test) => {
  78.     console.log(test)
  79.     const originalFile = test.parent.invocationDetails.originalFile;
  80.     const testVideoPath = getTestVideoPath(originalFile);
  81.     const testScreenshotsPath = getTestScreenshotsPath(originalFile);
  82.  
  83.     const files = testVideoPath ? [testVideoPath] : []
  84.  
  85.     console.log({ files })
  86.    
  87.     passes += 1;
  88.     console.log("pass: %s", test.fullTitle());
  89.     const testId = parseTest(test.title);
  90.     client.addTestRun(testId, TRConstants.PASSED, {
  91.       title: test.title,
  92.       time: test.duration,
  93.       files
  94.     });
  95.   });
  96.  
  97.   runner.on("fail", (test, err) => {
  98.     failures += 1;
  99.     console.log("fail: %s -- error: %s", test.fullTitle(), err.message);
  100.     const testId = parseTest(test.title);
  101.     client.addTestRun(testId, TRConstants.FAILED, {
  102.       error: err,
  103.       title: test.title,
  104.       time: test.duration,
  105.     });
  106.   });
  107.  
  108.   runner.on("end", () => {
  109.     console.log("end: %d/%d", passes, passes + failures);
  110.     const status = failures === 0 ? TRConstants.PASSED : TRConstants.FAILED;
  111.     client.updateRunStatus(status);
  112.   });
  113. }
  114.  
  115. // To have this reporter "extend" a built-in reporter uncomment the following line:
  116. mocha.utils.inherits(MochaReporter, mocha.reporters.Spec);
  117.  
  118. module.exports = MochaReporter;
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement