I want the PublishTestResults@2 task to run ONLY if the previous task script (which runs unit tests) actually ran.
condition: succeededOrFailed() then PublishTestResults@2 runs even if the previous step did not run - I thought this was what condition: always() was for.always() and succeededOrFailed()?Reference
# This step only runs if the previous step was successful - OK
- script: |
cd $(System.DefaultWorkingDirectory)/application/src
yarn test:unit --silent --ci --reporters=jest-junit
displayName: 'Jest Unit Tests'
env:
JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'
# This step ALWAYS runs - NO
# This step should ONLY run if the previous step RAN (success OR fail)
- task: PublishTestResults@2
displayName: 'Frontend Test results'
condition: succeededOrFailed()
inputs:
testResultsFormat: JUnit
searchFolder: $(System.DefaultWorkingDirectory)/testresults
testResultsFiles: 'jest-junit.xml'
testRunTitle: 'Frontend Test Results'
mergeTestResults: false
failTaskOnFailedTests: true
UPDATE: I suspect the Publish step "Frontend Test results" is running because the 2 previous steps were NOT run but the one before that was successful:

succeededOrFailed for a setp is equivalent to in(variables['Agent.JobStatus'], 'Succeeded', 'SucceededWithIssues', 'Failed') and this is why your Frontend Test results ran.
If you want to publish only when Jest Unit Tests was executed you may use logging commands to set variable and then use this variable in condition:
# This step only runs if the previous step was successful - OK
- script: |
echo "##vso[task.setvariable variable=doThing;isOutput=true]Yes" #set variable doThing to Yes
cd $(System.DefaultWorkingDirectory)/application/src
yarn test:unit --silent --ci --reporters=jest-junit
displayName: 'Jest Unit Tests'
name: JestUnitTests
env:
JEST_JUNIT_OUTPUT_DIR: $(System.DefaultWorkingDirectory)/testresults
JEST_JUNIT_OUTPUT_NAME: 'jest-junit.xml'
# This step ALWAYS runs - NO
# This step should ONLY run if the previous step RAN (success OR fail)
- task: PublishTestResults@2
displayName: 'Frontend Test results'
condition: and(succeededOrFailed(), eq(variables['JestUnitTests.doThing'], 'Yes'))
inputs:
testResultsFormat: JUnit
searchFolder: $(System.DefaultWorkingDirectory)/testresults
testResultsFiles: 'jest-junit.xml'
testRunTitle: 'Frontend Test Results'
mergeTestResults: false
failTaskOnFailedTests: true
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With