Search code examples
typescriptmocha.jsmochawesome

Mochawesome-reporter for Cypress with Typescript


I have a Cypress project which uses Typescript and I want to include a reporter.

Problem is that the reporter always shows only the compiled Javascript code.

I tried to google but the only thing I found was an unanswered question on stackoverflow. I also tried like 6 reporter, but every reporter used 'mochawesome-report-generator' as dependency or doesn't work at all.


Solution

  • I fixed it for me with a patch of mochawesome

    1. install patch-package
    2. in ./node_modules/mochawesome/src/mochawesome.js I added at runner.on('end', () => ...) directly after endCalled = true;
    const fileContent = fs.readFileSync(path.resolve(this.runner.suite.file), { encoding: 'utf8' }).split('\n');
    const logLineIndices = fileContent.map((lineContent, lineIndex) => lineContent.match(/^\t+(it\\.only|it|only)/) ? lineIndex : undefined).filter(Boolean);
    
    this.runner.suite.suites.forEach(suite => {
        suite.tests.forEach(test => {
            let testStartIndex = fileContent.findIndex(lineContent => lineContent.includes("'" + test.title + "'"));
            while (!logLineIndices.includes(testStartIndex) || testStartIndex < 0) {
                testStartIndex--;
            }
    
            if (testStartIndex < 0) return;
    
            const endRegex = new RegExp('^\\t{' + fileContent[testStartIndex].search(/\S|$/) + '}}?\\);\\s');
            const testEndIndex = fileContent.findIndex((lineContent, lineIndex) => lineIndex > testStartIndex && endRegex.test(lineContent));
    
            if (testEndIndex > testStartIndex) {
                test.body = fileContent.slice(testStartIndex, testEndIndex + 1).join('\n');
            }
        });
    });
    

    The regex is not the best, but its working in my cases