Skip to content

Commit 6e958f1

Browse files
jan-molakaurelien-reevesaslakhellesoy
authored
fix(formatter): Enable calling parseTestCaseAttempt on test cases that haven't completed (#1531)
* fix(formatter): Enable calling parseTestCaseAttempt on test cases that haven't completed yet * Instanciate a proper TestStepResult when parsing TestCaseAttempt * Add unit tests * Refactor testCaseAttemptParser unit tests Co-authored-by: Aurélien Reeves <[email protected]> Co-authored-by: Aslak Hellesøy <[email protected]>
1 parent 0fbb0fb commit 6e958f1

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

src/formatter/helpers/test_case_attempt_parser.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { ISupportCodeLibrary } from '../../support_code_library_builder/types'
1111
import { doesHaveValue, valueOrDefault } from '../../value_checker'
1212
import TestCaseHookDefinition from '../../models/test_case_hook_definition'
1313
import { ILineAndUri } from '../../types'
14+
import { TestStepResult } from '@cucumber/messages'
1415

1516
export interface IParsedTestStep {
1617
actionLocation?: ILineAndUri
@@ -150,9 +151,13 @@ export function parseTestCaseAttempt({
150151
const parsedTestSteps: IParsedTestStep[] = []
151152
let isBeforeHook = true
152153
let previousKeywordType = KeywordType.Precondition
154+
153155
testCase.testSteps.forEach((testStep) => {
154-
const testStepResult = testCaseAttempt.stepResults[testStep.id]
156+
const testStepResult =
157+
testCaseAttempt.stepResults[testStep.id] || new TestStepResult()
158+
155159
isBeforeHook = isBeforeHook && doesHaveValue(testStep.hookId)
160+
156161
let keyword, keywordType, pickleStep
157162
if (doesHaveValue(testStep.pickleStepId)) {
158163
pickleStep = pickleStepMap[testStep.pickleStepId]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import { describe, it } from 'mocha'
2+
import { expect } from 'chai'
3+
import * as messages from '@cucumber/messages'
4+
import { parseTestCaseAttempt } from '.'
5+
import { getBaseSupportCodeLibrary } from '../../../test/fixtures/steps'
6+
import StepDefinitionSnippetBuilder from '../step_definition_snippet_builder'
7+
import { ParameterTypeRegistry } from '@cucumber/cucumber-expressions'
8+
import { reindent } from 'reindent-template-literals'
9+
import { getTestCaseAttempts } from '../../../test/formatter_helpers'
10+
11+
describe('TestCaseAttemptParser', () => {
12+
describe('parseTestCaseAttempt', () => {
13+
const cwd = ''
14+
const supportCodeLibrary = getBaseSupportCodeLibrary()
15+
const snippetSyntax = {
16+
build: () => 'snippet',
17+
}
18+
19+
const snippetBuilder = new StepDefinitionSnippetBuilder({
20+
snippetSyntax,
21+
parameterTypeRegistry: new ParameterTypeRegistry(),
22+
})
23+
24+
const source = {
25+
data: reindent(`
26+
Feature: my feature
27+
Scenario: my scenario
28+
Given a passing step
29+
`),
30+
uri: 'a.feature',
31+
}
32+
33+
describe('with no test step result', () => {
34+
it('initialize step result with status UNKNOWN', async () => {
35+
// Arrange
36+
const [testCaseAttempt] = await getTestCaseAttempts({
37+
sources: [source],
38+
supportCodeLibrary,
39+
})
40+
41+
testCaseAttempt.stepResults = {}
42+
43+
// Act
44+
const output = parseTestCaseAttempt({
45+
cwd,
46+
testCaseAttempt,
47+
snippetBuilder,
48+
supportCodeLibrary,
49+
})
50+
51+
// Assert
52+
expect(output.testSteps[0].result.status).to.eq(
53+
messages.TestStepResultStatus.UNKNOWN
54+
)
55+
})
56+
})
57+
58+
describe('with test step result', () => {
59+
it('uses the parsed step result', async () => {
60+
// Arrange
61+
const [testCaseAttempt] = await getTestCaseAttempts({
62+
sources: [source],
63+
supportCodeLibrary,
64+
})
65+
66+
// Act
67+
const output = parseTestCaseAttempt({
68+
cwd,
69+
testCaseAttempt,
70+
snippetBuilder,
71+
supportCodeLibrary,
72+
})
73+
74+
// Assert
75+
expect(output.testSteps[0].result.status).to.eq(
76+
messages.TestStepResultStatus.PASSED
77+
)
78+
})
79+
})
80+
})
81+
})

0 commit comments

Comments
 (0)