storybook/cypress/report-teamcity-metadata.ts

74 lines
2.3 KiB
TypeScript
Raw Normal View History

import path from 'path';
import fs from 'fs-extra';
import { testMetadata } from 'teamcity-service-messages';
import { findSuitesAndTests } from 'mocha-list-tests';
const testsDir = path.join(__dirname, 'integration');
const videosDir = path.join(__dirname, 'videos');
const screensDir = path.join(__dirname, 'screenshots');
2020-02-23 01:34:50 +01:00
let prevFoundTests: string[] = [];
function getTests(fileName: string) {
const { tests } = findSuitesAndTests(path.join(testsDir, fileName));
const newTests = tests.filter((test: string) => !prevFoundTests.includes(test));
prevFoundTests = tests;
return newTests.map((test: string) => test.split(/\./));
}
2020-02-23 01:34:50 +01:00
const fullTestName = (suite: string, testName: string) => `${suite}: ${testName}`;
async function report() {
2020-04-30 18:00:35 +02:00
const hookFailures: { [file: string]: [string, string][] } = {};
2020-02-23 01:34:50 +01:00
const reports: any[] = [];
2020-02-23 23:52:12 +01:00
try {
const testFiles = await fs.readdir(screensDir);
await Promise.all(
2020-03-27 20:04:50 +01:00
testFiles.map(async (testFile) => {
2020-02-23 23:52:12 +01:00
const files = await fs.readdir(path.join(screensDir, testFile));
2020-03-27 20:04:50 +01:00
files.forEach((file) => {
2020-02-23 23:52:12 +01:00
const match = file.match(/^(.*) \(failed\).png$/);
if (match == null) {
return;
}
2020-02-23 01:34:50 +01:00
2020-02-23 23:52:12 +01:00
const [suite, test, hookPart] = match[1].split(' -- ');
let testName = test;
const hook = hookPart?.match(/^(.*) hook$/)?.[1];
if (hook != null) {
testName = `"${hook}" hook for "${test}"`;
hookFailures[testFile] = hookFailures[testFile] || [];
hookFailures[testFile].push([suite, testName]);
}
reports.push({
name: 'Screenshot',
testName: fullTestName(suite, testName),
type: 'image',
value: `screenshots.tar.gz!${testFile}/${file}`,
});
2020-02-23 01:34:50 +01:00
});
2020-02-23 23:52:12 +01:00
})
);
} catch (e) {
// ignore
}
2020-02-23 01:34:50 +01:00
const videoFiles = await fs.readdir(videosDir);
2020-03-27 20:04:50 +01:00
videoFiles.forEach((videoFile) => {
2020-02-23 01:34:50 +01:00
const testFile = videoFile.replace(/\.mp4$/, '');
const tests = [...getTests(testFile), ...(hookFailures[testFile] || [])];
tests.forEach(([suite, testName]) =>
reports.unshift({
name: 'Video',
testName: fullTestName(suite, testName),
type: 'video',
value: `videos.tar.gz!${videoFile}`,
})
);
});
2020-02-23 01:34:50 +01:00
reports.forEach(testMetadata);
}
2020-02-23 01:34:50 +01:00
report();