From 3caf401b79b25f89060ec581cfc37bec0f08a2a8 Mon Sep 17 00:00:00 2001 From: Kevin Hudemann Date: Thu, 3 Sep 2020 13:12:27 +0200 Subject: [PATCH] Add npmExecuteScripts to piperPipelineStageAdditionalUnitTests (#1978) This change adds the support for running frontend unit tests using the npmExecuteScripts step in the piperPipelineStageAdditionalUnitTests. --- .../docs/pipelines/cloud-sdk/build-tools.md | 4 +--- .../pipeline/cloudSdkStageDefaults.yml | 3 +-- ...erPipelineStageAdditionalUnitTestsTest.groovy | 16 +++++++++++++++- .../piperPipelineStageAdditionalUnitTests.groovy | 10 ++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/documentation/docs/pipelines/cloud-sdk/build-tools.md b/documentation/docs/pipelines/cloud-sdk/build-tools.md index 141d754c4..0fa8012a7 100644 --- a/documentation/docs/pipelines/cloud-sdk/build-tools.md +++ b/documentation/docs/pipelines/cloud-sdk/build-tools.md @@ -81,7 +81,7 @@ Please pick the corresponding version for your deployment target and rename it p #### Frontend Unit Tests -The command `npm run ci-frontend-unit-test` will be executed in this stage. +For each `package.json` where the script `ci-frontend-unit-test` is defined the command `npm run ci-frontend-unit-test` will be executed in this stage. Furthermore, the test results have to be stored in the folder `./s4hana_pipeline/reports/frontend-unit` in the root directory. The required format of the test result report is the JUnit format as an `.xml` file. The code coverage report can be published as html report and in the cobertura format. @@ -89,8 +89,6 @@ The cobertura report as html report has to be stored in the directory `./s4hana_ These coverage reports will then be published in Jenkins. Furthermore, if configured in the `.pipeline/config.yml`, the pipeline ensures the configured level of code coverage. -In MTA projects Frontend Unit Tests are executed for every module of type `html5`. - #### Frontend Integration Tests The command `npm run ci-it-frontend` will be executed in this stage and has to be defined in the `package.json` in the root. diff --git a/resources/com.sap.piper/pipeline/cloudSdkStageDefaults.yml b/resources/com.sap.piper/pipeline/cloudSdkStageDefaults.yml index 96063491b..586fb54a4 100644 --- a/resources/com.sap.piper/pipeline/cloudSdkStageDefaults.yml +++ b/resources/com.sap.piper/pipeline/cloudSdkStageDefaults.yml @@ -26,9 +26,8 @@ stages: - 'ci-it-frontend' frontendUnitTests: stepConditions: - frontendUnitTests: + npmExecuteScripts: npmScripts: - - 'ci-test' - 'ci-frontend-unit-test' npmAudit: stepConditions: diff --git a/test/groovy/templates/PiperPipelineStageAdditionalUnitTestsTest.groovy b/test/groovy/templates/PiperPipelineStageAdditionalUnitTestsTest.groovy index 7deebf18f..6f4f10df1 100644 --- a/test/groovy/templates/PiperPipelineStageAdditionalUnitTestsTest.groovy +++ b/test/groovy/templates/PiperPipelineStageAdditionalUnitTestsTest.groovy @@ -49,6 +49,10 @@ class PiperPipelineStageAdditionalUnitTestsTest extends BasePiperTest { stepsCalled.add('karmaExecuteTests') }) + helper.registerAllowedMethod('npmExecuteScripts', [Map.class], {m -> + stepsCalled.add('npmExecuteScripts') + }) + helper.registerAllowedMethod('testsPublishResults', [Map.class], {m -> stepsCalled.add('testsPublishResults') }) @@ -59,7 +63,7 @@ class PiperPipelineStageAdditionalUnitTestsTest extends BasePiperTest { jsr.step.piperPipelineStageAdditionalUnitTests(script: nullScript, juStabUtils: utils) - assertThat(stepsCalled, not(anyOf(hasItem('batsExecuteTests'), hasItem('karmaExecuteTests'), hasItem('testsPublishResults')))) + assertThat(stepsCalled, not(anyOf(hasItem('batsExecuteTests'), hasItem('karmaExecuteTests'), hasItem('npmExecuteScripts'), hasItem('testsPublishResults')))) } @Test @@ -81,4 +85,14 @@ class PiperPipelineStageAdditionalUnitTestsTest extends BasePiperTest { assertThat(stepsCalled, hasItems('batsExecuteTests', 'testsPublishResults')) } + + @Test + void testAdditionalUnitTestsWithNpm() { + + nullScript.commonPipelineEnvironment.configuration = [runStep: ['Additional Unit Tests': [npmExecuteScripts: true]]] + + jsr.step.piperPipelineStageAdditionalUnitTests(script: nullScript, juStabUtils: utils) + + assertThat(stepsCalled, hasItems('npmExecuteScripts', 'testsPublishResults')) + } } diff --git a/vars/piperPipelineStageAdditionalUnitTests.groovy b/vars/piperPipelineStageAdditionalUnitTests.groovy index 3b712f568..815935751 100644 --- a/vars/piperPipelineStageAdditionalUnitTests.groovy +++ b/vars/piperPipelineStageAdditionalUnitTests.groovy @@ -15,6 +15,8 @@ import static com.sap.piper.Prerequisites.checkScript 'batsExecuteTests', /** Executes karma tests which are for example suitable for OPA5 testing as well as QUnit testing of SAP UI5 apps.*/ 'karmaExecuteTests', + /** Executes npm scripts to run frontend unit tests */ + 'npmExecuteScripts', /** Publishes test results to Jenkins. It will automatically be active in cases tests are executed. */ 'testsPublishResults' ] @@ -39,6 +41,7 @@ void call(Map parameters = [:]) { .mixin(parameters, PARAMETER_KEYS) .addIfEmpty('batsExecuteTests', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.batsExecuteTests) .addIfEmpty('karmaExecuteTests', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.karmaExecuteTests) + .addIfEmpty('npmExecuteScripts', script.commonPipelineEnvironment.configuration.runStep?.get(stageName)?.npmExecuteScripts) .use() piperStageWrapper (script: script, stageName: stageName) { @@ -59,5 +62,12 @@ void call(Map parameters = [:]) { testsPublishResults script: script } } + + if (config.npmExecuteScripts) { + durationMeasure(script: script, measurementName: 'npmExecuteScripts_duration') { + npmExecuteScripts script: script + testsPublishResults script: script + } + } } }