|
| 1 | +const { Formatter } = require('@cucumber/cucumber'); |
| 2 | +class JsonFormatter extends Formatter { |
| 3 | + |
| 4 | + constructor(options) { |
| 5 | + super(options); |
| 6 | + options.eventBroadcaster.on('envelope', this.processEnvelope.bind(this)); |
| 7 | + this.rpConfig = options.parsedArgvOptions.rpConfig; |
| 8 | + this.json = []; |
| 9 | + this.hooks = {}; |
| 10 | + } |
| 11 | + |
| 12 | + processEnvelope(envelope) { |
| 13 | + if (envelope.testCaseFinished) { |
| 14 | + this.finishTest(envelope); |
| 15 | + } else if (envelope.testRunFinished) { |
| 16 | + this.finishLaunch(); |
| 17 | + } else if (envelope.hook) { |
| 18 | + this.hooks[envelope.hook.id] = envelope.hook; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + finishTest(envelope) { |
| 23 | + const testCase = this.eventDataCollector.getTestCaseAttempt(envelope.testCaseFinished.testCaseStartedId); |
| 24 | + let feature = this.json.find(feature => feature.name === testCase.gherkinDocument.feature.name); |
| 25 | + if (!feature) { |
| 26 | + feature = { |
| 27 | + description: testCase.gherkinDocument.feature.description, |
| 28 | + id: 'feature' + testCase.pickle.id, |
| 29 | + line: testCase.gherkinDocument.feature.location.line, |
| 30 | + keyword: testCase.gherkinDocument.feature.keyword, |
| 31 | + name: testCase.gherkinDocument.feature.name, |
| 32 | + uri: testCase.gherkinDocument.uri, |
| 33 | + elements: [], |
| 34 | + tags: this.formatTags(testCase.gherkinDocument.feature.tags) |
| 35 | + }; |
| 36 | + this.json.push(feature); |
| 37 | + } |
| 38 | + const steps = testCase.testCase.testSteps; |
| 39 | + for (const step of steps) { |
| 40 | + const pickle = testCase.pickle.steps.find(pickle => step.pickleStepId === pickle.id); |
| 41 | + step.name = pickle ? pickle.text : this.hookText(steps, step); |
| 42 | + const result= testCase.stepResults[step.id]; |
| 43 | + step.result = { |
| 44 | + status: result.status.toLowerCase(), |
| 45 | + duration: result.duration.seconds * 1_000_000 + result.duration.nanos |
| 46 | + }; |
| 47 | + step.embeddings = testCase.stepAttachments[step.id]?.map(attachment => ({ |
| 48 | + ...attachment, |
| 49 | + data: attachment.body, |
| 50 | + mime_type: attachment.mediaType |
| 51 | + })); |
| 52 | + } |
| 53 | + const scenario = { |
| 54 | + steps, |
| 55 | + name: testCase.pickle.name, |
| 56 | + id: testCase.pickle.id, |
| 57 | + keyword: 'Scenario', |
| 58 | + tags: this.formatTags(testCase.pickle.tags), |
| 59 | + type: 'scenario' |
| 60 | + } |
| 61 | + feature.elements.push(scenario); |
| 62 | + } |
| 63 | + |
| 64 | + finishLaunch() { |
| 65 | + this.log(JSON.stringify(this.json, null, 2)) |
| 66 | + } |
| 67 | + |
| 68 | + formatTags(tags) { |
| 69 | + return tags.map(tag => ({ name: tag.name, line: tag.location?.line })) |
| 70 | + } |
| 71 | + |
| 72 | + hookText(steps, step) { |
| 73 | + const hook = this.hooks[step.hookId]; |
| 74 | + if (hook?.name) return hook.name; |
| 75 | + const stepsBefore = steps.slice(0, steps.findIndex((element) => element === step)); |
| 76 | + return stepsBefore.every(element => element.pickleStepId === undefined) ? 'Before' : 'After' |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = JsonFormatter; |
0 commit comments