question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[pipelines] generate CodeBuild reports within @aws-cdk/cdk-pipelines

See original GitHub issue

Description

To be able to easily use @aws-cdk/assert within @aws-cdk/cdk-pipelines. and use the reports function of CodeBuild.

Use Case

Running tests on CDK using @aws-cdk/assert to increase the ability to Fail Fast and implement TDD.

Proposed Solution

I have considered 2 options so far: ( there may be other options, and there may well be some other/better way to do this )

Option1: add a testAction that adds another stage to codepipeline using another codebuild project :

const pipeline = new CdkPipeline(this, 'Pipeline', {
      pipelineName: 'PipelineWithTests',
      sourceAction: ...
      synthAction: ...
      testAction: simpleTestAction(...)
    });

Where the new testAction includes support for reports in the buildSpec.

Option2: Enable adding reports to the existing SimpleSynthAction(and npm yarn variants) buildSpec: adding the needed changes to the build step commands “yarn build && yarn test”. and adding along the lines of the bellow to the build spec perhaps with a testEnable Flag in the SynthAction.

reports:
  jest_reports:
    files:
      - <report filename>
    file-format: JUNITXML
    base-directory: <test report directory>

Other

I understand that this can be done by creating a codeBuild on its own and adding to the pipeline, but that breaks the elegance and simplicity of using cdk-pipelines which dose a great job at simplifying the codepipeline/buid experience.

I would expect that jest tests would be ran by yarn or npm as described in the @aws-cdk/assert docs. Reference: test-report-jest


This is a 🚀 Feature Request

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:19
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
nbailliecommented, Nov 27, 2020

@BrianFarnhill if you drop back to SimpleSynthAction from SimpleSynthAction.standardNpmSynth it already has the testCommand available. So would just be to do the second part as you have described above.

synthAction: new SimpleSynthAction({
        sourceArtifact,
        cloudAssemblyArtifact,
        installCommand: "yarn install --frozen-lockfile",
        buildCommand: `yarn build`,
        testCommands: [`yarn test unit`],
        synthCommand: `npx cdk synth`
}),

would become

synthAction: new SimpleSynthAction({
        sourceArtifact,
        cloudAssemblyArtifact,
        installCommand: "yarn install --frozen-lockfile",
        buildCommand: `yarn build`,
        testCommands: [`yarn test unit`],
        synthCommand: `npx cdk synth`,
        testReports: [{
            name: "GroupName" | "GroupArn",
            reportType: "Test" | "CodeCoverage",
            files: string[],
            fileFormat: "JUNITXML" | "OTHER_TYPES" | etc...,
            baseDirectory: string,
        }]
}),
5reactions
bilalqcommented, Nov 8, 2021

Based on the sample you shared, this is a working example I came up with to get both test and coverage reports working:

import { BuildSpec, ReportGroup, CfnReportGroup } from '@aws-cdk/aws-codebuild'
import * as cdk from '@aws-cdk/core'
import * as pipelines from '@aws-cdk/pipelines'
import * as iam from '@aws-cdk/aws-iam'

interface SomePipelineProps extends cdk.StackProps {
  // ...
}

export class SomePipeline extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props: SomePipelineProps) {
    super(scope, id, props)

    const repo = 'Your Repo here'
    const branch = 'Your branch here'
    const codeStarConnection = pipelines.CodePipelineSource.connection(repo, branch, {
      connectionArn: 'Your CodeStar connection ARN here',
      triggerOnPush: true
    })

    const jestReportGroup = new ReportGroup(this, 'JestReportGroup', {})
    const unitTestCoverageGroup = new ReportGroup(this, 'CoverageReportGroup', {})
    const cfnUnitTestCoverageGroup = unitTestCoverageGroup.node.defaultChild as CfnReportGroup
    cfnUnitTestCoverageGroup.type = 'CODE_COVERAGE'

    const customSynthStep = new pipelines.CodeBuildStep('ExperimentalSynth', {
      input: codeStarConnection,
      env: {
        CI: 'true'
      },
      installCommands: ['npm ci'],
      commands: ['npm run build', 'npm test', 'npx cdk synth'],
      partialBuildSpec: BuildSpec.fromObject({
        version: '0.2',
        // Make sure your jest config outputs to locations that match what's here
        reports: {
          [jestReportGroup.reportGroupArn]: {
            files: ['jest-results.xml'],
            'file-format': 'JUNITXML',
            'base-directory': 'build/reports/unit-test'
          },
          [unitTestCoverageGroup.reportGroupArn]: {
            files: ['clover.xml'],
            'file-format': 'CLOVERXML',
            'base-directory': 'build/reports/coverage'
          }
        }
      }),
      primaryOutputDirectory: 'cdk.out'
    })

    const pipeline = new pipelines.CodePipeline(this, 'SomePipeline', {
      pipelineName: 'SomePipeline',
      synth: customSynthStep
    })

    // Do whatever else you want on your pipeline
    // ...

    // Granting access here has to happen after calling buildPipeline so the CloudAssembly references resolve
    // All of this is only necessary becasue of limitations discussed in https://github.com/aws/aws-cdk/issues/10464
    pipeline.buildPipeline()
    jestReportGroup.grantWrite(customSynthStep.grantPrincipal)
    unitTestCoverageGroup.grantWrite(customSynthStep.grantPrincipal)
    // This next grant is necessary because the grantWrite command won't add the permission needed for coverage reports
    // https://github.com/aws/aws-cdk/issues/14279
    iam.Grant.addToPrincipal({
      grantee: customSynthStep.grantPrincipal,
      actions: ['codebuild:BatchPutCodeCoverages'],
      resourceArns: [unitTestCoverageGroup.reportGroupArn]
    })
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Working with test reporting in AWS CodeBuild
You can create reports in CodeBuild that contain details about tests that are run during builds. You can create tests such as unit...
Read more >
Create a test report in CodeBuild using the Amazon CLI sample
This sample shows you how to use the Amazon CLI to incorporate tests into builds in CodeBuild. You can use JUnit to create...
Read more >
AWS CodeBuild - Cypress Documentation
How to run Cypress tests with AWS CodeBuild as part of CI/CD pipeline ... The Build caching in AWS CodeBuild document offers details...
Read more >
Using CodeBuild test reports in CDK Pipelines (C#).
I had been trying to figure out how to make CodeBuild test reports work with CDK Pipelines. Last week when I got back...
Read more >
AWS CodeBuild Report Groups 101 - YouTube
AWS CodeBuild provides a fully managed, Docker-based build environment. You can use it in standalone mode or together with CodePipeline.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found