Skip to content

Instantly share code, notes, and snippets.

@ncuillery
Created September 5, 2017 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ncuillery/ab5b252e247dbcc9bbf72413382fe0c9 to your computer and use it in GitHub Desktop.
Save ncuillery/ab5b252e247dbcc9bbf72413382fe0c9 to your computer and use it in GitHub Desktop.
Full code snippets relative to the Medium story
#!/usr/bin/env bash
echo "Run custom test frameworks"
cd ..
# Create the directory expected by buddybuild
mkdir -p buddybuild_artifacts/Jest
mkdir -p buddybuild_artifacts/ESLint
# Run the ESLint command then the output transformation script
eslint src --format json > buddybuild_artifacts/ESLint/eslint.json
node eslint-output-adapter.js || exit 1
# Run the Jest command
jest --outputFile=buddybuild_artifacts/Jest/jest.json --json || exit 1
/*
* This file rewrites the eslint JSON output to make it similar
* to the Jest JSON output. So that buddybuild can understand it
* and display a proper and relevant ESLint tab
*/
const util = require('util');
const fs = require('fs');
// Node 8 is needed
const readFile = util.promisify(fs.readFile);
const writeFile = util.promisify(fs.writeFile);
let errorCount = 0;
readFile('buddybuild_artifacts/ESLint/eslint.json')
.then(JSON.parse)
.then(eslintReport => {
// 1. Create a single test suite report
const output = {
testResults: [
{
assertionResults: [],
},
],
};
// 2. Populate with a test case for each file in the ESLint report
eslintReport.forEach(entry => {
errorCount += entry.errorCount ? 1 : 0; // Count only 1 error in case of multiple errors in the same file
output.testResults[0].assertionResults.push({
status: entry.errorCount ? 'failed' : 'passed',
title: entry.filePath,
failureMessages: entry.messages.map(
// Transform the object-based ESLint message into a Jest-style string:
({ ruleId, message, line, column }) => `Error ${ruleId} at ${line}${column}: ${message}`,
),
});
});
// 3. Add Jest-style metadata
// Global metadata
output.numFailedTestSuites = errorCount ? 1 : 0;
output.numFailedTests = errorCount;
output.numPassedTestSuites = errorCount ? 0 : 1;
output.numPassedTests = eslintReport.length - errorCount;
output.numTotalTestSuites = 1;
output.numTotalTests = eslintReport.length;
output.success = !errorCount;
// Test suite metadata
output.testResults[0].status = errorCount ? 'failed' : 'passed';
output.testResults[0].message = '';
output.testResults[0].name = '';
output.testResults[0].summary = '';
return output;
})
.then(JSON.stringify)
.then(output => writeFile('buddybuild_artifacts/ESLint/eslint.json', output))
.then(() => {
console.log('Eslint output transformation done');
// Exit with an error code in case of ESLint error
process.exit(errorCount ? 1 : 0);
})
.catch(err => {
console.error('Error during Eslint output transformation', err);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment