Skip to content

Instantly share code, notes, and snippets.

@acdcjunior
Last active September 9, 2020 13:17
Show Gist options
  • Save acdcjunior/c1c7d7ec95060452dd9caff52813d319 to your computer and use it in GitHub Desktop.
Save acdcjunior/c1c7d7ec95060452dd9caff52813d319 to your computer and use it in GitHub Desktop.
const formatter = function (options) {
const cucumber = require(options.cucumberLibPath);
const common = require('./cucumberjs_formatter_common.js');
//Need to create an instance to see the summary in the console output
const formatter = new cucumber.Formatter(options);
const summaryFormatter = new cucumber.SummaryFormatter(options);
let currentFeature = '';
let featureIndex = 0;
/**
* test-run == Feature == testSuite
* test-case == Scenario == testSuite
* test-step == Step == test
*/
options.eventBroadcaster.on('test-run-started', logFeatureStarted.bind())
options.eventBroadcaster.on('test-run-finished', logFeatureFinished.bind())
options.eventBroadcaster.on('test-case-started', logTestCaseStarted.bind())
options.eventBroadcaster.on('test-case-finished', logTestCaseFinished.bind())
options.eventBroadcaster.on('test-step-started', logStepStarted.bind())
options.eventBroadcaster.on('test-step-finished', logStepFinished.bind())
function logStepStarted(event) {
const data = formatter.eventDataCollector.getTestCaseData(event.testCase.sourceLocation);
if (data.pickle.steps[event.index] == null) return;
const stepName = data.pickle.steps[event.index].text;
const stepLine = data.pickle.steps[event.index].locations[0].line;
console.log(
`##teamcity[testStarted name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' captureStandardOutput = 'true' locationHint='file:///${getUri() +
':' +
stepLine}']`);
}
function logStepFinished(event) {
const data = formatter.eventDataCollector.getTestCaseData(event.testCase.sourceLocation);
if (data.pickle.steps[event.index] == null) return;
const stepName = data.pickle.steps[event.index].text;
const result = event.result.status;
let duration = event.result.duration
if (duration === undefined) duration = 0
switch (result) {
case 'undefined':
console.log(`##teamcity[testFailed name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' message='' ]`);
break;
case 'ambiguous':
case 'failed':
console.log(`##teamcity[testFailed name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' details='${common.escape(
event.result.exception.stack)}' message='' ]`);
break;
case 'skipped':
console.log(`##teamcity[testIgnored name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}']`);
break;
}
console.log(`##teamcity[testFinished name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' duration='${duration}']`);
}
function getFeatureName() {
const length = Object.keys(formatter.eventDataCollector.gherkinDocumentMap).length
const featureFileName = Object.keys(formatter.eventDataCollector.gherkinDocumentMap)[featureIndex]
if (formatter.eventDataCollector.gherkinDocumentMap[featureFileName].feature === undefined) return null
return formatter.eventDataCollector.gherkinDocumentMap[featureFileName].feature.name
}
function getUri() {
return formatter.cwd + '/' + getFeatureFile()
}
function getFeatureFile() {
return common.escape(Object.keys(formatter.eventDataCollector.gherkinDocumentMap)[featureIndex]);
}
function logFeatureStarted() {
//For cases when feature file exists but contains no content
if (getFeatureName() == null) return;
if (currentFeature === '') {
currentFeature = getFeatureName()
console.log(`##teamcity[enteredTheMatrix timestamp='${common.getCurrentDate()}']\n`)
console.log(`##teamcity[customProgressStatus testsCategory = 'Scenarios' count = '0' timestamp='${common.getCurrentDate()}']`)
}
console.log(
`##teamcity[testSuiteStarted name='Feature: ${common.escape(currentFeature)}' timestamp='${common.getCurrentDate()}' locationHint='file:///${getUri()}']`);
}
function logFeatureFinished() {
console.log(`##teamcity[testSuiteFinished name='Feature: ${common.escape(currentFeature)}' timestamp='${common.getCurrentDate()}']`);
console.log(`##teamcity[customProgressStatus testsCategory = '' count = '0' timestamp = '${common.getCurrentDate()}']`)
featureIndex++;
}
function logTestCaseStarted({sourceLocation}) {
const data = formatter.eventDataCollector.getTestCaseData(sourceLocation);
const featureName = data.gherkinDocument.feature.name
if (featureName !== currentFeature) {
logFeatureFinished()
currentFeature = featureName
logFeatureStarted()
}
console.log(`##teamcity[customProgressStatus type = 'testStarted' timestamp='${common.getCurrentDate()}']`);
console.log(`##teamcity[testSuiteStarted name='Scenario: ${common.escape(
data.pickle.name)}' timestamp='${common.getCurrentDate()}' locationHint='file:///${getUri() +
':' +
sourceLocation.line}']`);
}
function logTestCaseFinished({sourceLocation}) {
const data = formatter.eventDataCollector.getTestCaseData(sourceLocation);
const result = data.testCase.result.status;
switch (result) {
case 'passed':
console.log(`##teamcity[customProgressStatus type = 'testFinished' timestamp='${common.getCurrentDate()}']`)
break;
case 'ambiguous':
case 'failed':
console.log(`##teamcity[customProgressStatus type = 'testFailed' timestamp='${common.getCurrentDate()}']`)
break;
case 'skipped':
console.log(`##teamcity[customProgressStatus type = 'testSkipped' timestamp='${common.getCurrentDate()}']`)
break;
}
console.log(`##teamcity[testSuiteFinished name='Scenario: ${common.escape(data.pickle.name)}' timestamp='${common.getCurrentDate()}']`);
}
};
module.exports = formatter
const formatter = function (options) {
const cucumber = require(options.cucumberLibPath);
const common = require('./cucumberjs_formatter_common.js');
//Need to create an instance to see the summary in the console output
const formatter = new cucumber.Formatter(options);
const summaryFormatter = new cucumber.SummaryFormatter(options);
let currentFeature = '';
let featureIndex = 0;
/**
* test-run == Feature == testSuite
* test-case == Scenario == testSuite
* test-step == Step == test
*/
options.eventBroadcaster.on('test-run-started', logFeatureStarted.bind())
options.eventBroadcaster.on('test-run-finished', logFeatureFinished.bind())
options.eventBroadcaster.on('test-case-started', logTestCaseStarted.bind())
options.eventBroadcaster.on('test-case-finished', logTestCaseFinished.bind())
options.eventBroadcaster.on('test-step-started', logStepStarted.bind())
options.eventBroadcaster.on('test-step-finished', logStepFinished.bind())
function logStepStarted(event) {
const testCaseAttempt = formatter.eventDataCollector.getTestCaseAttempt(event.testCase || event)
if (testCaseAttempt.pickle.steps[event.index] == null) return;
const stepName = testCaseAttempt.pickle.steps[event.index].text;
const stepLine = testCaseAttempt.pickle.steps[event.index].locations[0].line;
console.log(
`##teamcity[testStarted name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' captureStandardOutput = 'true' locationHint='file:///${getUri() +
':' +
stepLine}']`);
}
function logStepFinished(event) {
const testCaseAttempt = formatter.eventDataCollector.getTestCaseAttempt(event.testCase || event)
if (testCaseAttempt.pickle.steps[event.index] == null) return;
const stepName = testCaseAttempt.pickle.steps[event.index].text;
const result = event.result.status;
let duration = event.result.duration
if (duration === undefined) duration = 0
switch (result) {
case 'undefined':
console.log(`##teamcity[testFailed name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' message='' ]`);
break;
case 'ambiguous':
case 'failed':
console.log(`##teamcity[testFailed name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' details='${common.escape(
event.result.exception.stack)}' message='' ]`);
break;
case 'skipped':
console.log(`##teamcity[testIgnored name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}']`);
break;
}
console.log(`##teamcity[testFinished name = 'Step: ${common.escape(stepName)}' timestamp='${common.getCurrentDate()}' duration='${duration}']`);
}
function getFeatureName() {
const length = Object.keys(formatter.eventDataCollector.gherkinDocumentMap).length
const featureFileName = Object.keys(formatter.eventDataCollector.gherkinDocumentMap)[featureIndex]
if (formatter.eventDataCollector.gherkinDocumentMap[featureFileName].feature === undefined) return null
return formatter.eventDataCollector.gherkinDocumentMap[featureFileName].feature.name
}
function getUri() {
return formatter.cwd + '/' + getFeatureFile()
}
function getFeatureFile() {
return common.escape(Object.keys(formatter.eventDataCollector.gherkinDocumentMap)[featureIndex]);
}
function logFeatureStarted() {
//For cases when feature file exists but contains no content
if (getFeatureName() == null) return;
if (currentFeature === '') {
currentFeature = getFeatureName()
console.log(`##teamcity[enteredTheMatrix timestamp='${common.getCurrentDate()}']\n`)
console.log(`##teamcity[customProgressStatus testsCategory = 'Scenarios' count = '0' timestamp='${common.getCurrentDate()}']`)
}
console.log(
`##teamcity[testSuiteStarted name='Feature: ${common.escape(currentFeature)}' timestamp='${common.getCurrentDate()}' locationHint='file:///${getUri()}']`);
}
function logFeatureFinished() {
console.log(`##teamcity[testSuiteFinished name='Feature: ${common.escape(currentFeature)}' timestamp='${common.getCurrentDate()}']`);
console.log(`##teamcity[customProgressStatus testsCategory = '' count = '0' timestamp = '${common.getCurrentDate()}']`)
featureIndex++;
}
function logTestCaseStarted(event) {
const testCaseAttempt = formatter.eventDataCollector.getTestCaseAttempt(event);
const featureName = testCaseAttempt.gherkinDocument.feature.name
if (featureName !== currentFeature) {
logFeatureFinished()
currentFeature = featureName
logFeatureStarted()
}
console.log(`##teamcity[customProgressStatus type = 'testStarted' timestamp='${common.getCurrentDate()}']`);
console.log(`##teamcity[testSuiteStarted name='Scenario: ${common.escape(
testCaseAttempt.pickle.name)}' timestamp='${common.getCurrentDate()}' locationHint='file:///${getUri() +
':' +
event.sourceLocation.line}']`);
}
function logTestCaseFinished(event) {
const testCaseAttempt = formatter.eventDataCollector.getTestCaseAttempt(event.testCase || event)
const result = testCaseAttempt.result.status;
switch (result) {
case 'passed':
console.log(`##teamcity[customProgressStatus type = 'testFinished' timestamp='${common.getCurrentDate()}']`)
break;
case 'ambiguous':
case 'failed':
console.log(`##teamcity[customProgressStatus type = 'testFailed' timestamp='${common.getCurrentDate()}']`)
break;
case 'skipped':
console.log(`##teamcity[customProgressStatus type = 'testSkipped' timestamp='${common.getCurrentDate()}']`)
break;
}
console.log(`##teamcity[testSuiteFinished name='Scenario: ${common.escape(testCaseAttempt.pickle.name)}' timestamp='${common.getCurrentDate()}']`);
}
};
module.exports = formatter
@acdcjunior
Copy link
Author

Error:

Testing started at 19:50 ...
...\node_modules\cucumber\bin\cucumber-js ui\tests\feat.feature --name "stuff" -p default --format C:\Users\...\AppData\Roaming\JetBrains\IntelliJIdea2020.1\plugins\CucumberJavaScript\lib\cucumberjs_formatter_v3.js --format-options "{\"cucumberLibPath\": \"D:/.../ui/node_modules/cucumber/lib\"}" --format-options "{\"colorsEnabled\": true}" --require D:/.../ui/tests/feat

TypeError: formatter.eventDataCollector.getTestCaseData is not a function
    at logTestCaseStarted (C:\Users\....\AppData\Roaming\JetBrains\IntelliJIdea2020.1\plugins\CucumberJavaScript\lib\cucumberjs_formatter_v3.js:89:47)
    at EventEmitter.emit (events.js:317:22)

Process finished with exit code 1

@ddemydenko
Copy link

thanks

@byjs-dev
Copy link

byjs-dev commented Sep 3, 2020

Perfect, thank you!

@Roomysy
Copy link

Roomysy commented Sep 9, 2020

Hi all
I have the same problem while running tests

Testing started at 3:21 PM ...
"C:\Program Files\nodejs\node.exe" C:\Workspace\projectName\apps\web\src\main\webapp\loe\tests\node_modules\cucumber\bin\cucumber-js C:\Workspace\projectName\apps\web\src\main\webapp\loe\tests\features\login.feature --format C:\Users\userName\AppData\Roaming\JetBrains\IntelliJIdea2020.1\plugins\CucumberJavaScript\lib\cucumberjs_formatter_v3.js --format-options "{"cucumberLibPath": "C:/Workspace/cxs-accelerators-portal/apps/web/src/main/webapp/loe/tests/node_modules/cucumber/lib"}" --format-options "{"colorsEnabled": true}" --require C:/Workspace/projectName/apps/web/src/main/webapp/loe/tests/features
TypeError: formatter.eventDataCollector.getTestCaseData is not a function
at logTestCaseStarted (C:\Users\userName\AppData\Roaming\JetBrains\IntelliJIdea2020.1\plugins\CucumberJavaScript\lib\cucumberjs_formatter_v3.js:89:47)
at EventEmitter.emit (events.js:327:22)

What is the resolution here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment