aws / aws-sam-cli

CLI tool to build, test, debug, and deploy Serverless applications using AWS SAM
https://aws.amazon.com/serverless/sam/
Apache License 2.0
6.5k stars 1.17k forks source link

Feature request: Run Node tests as part of sam build #7348

Open saraiyakush opened 1 month ago

saraiyakush commented 1 month ago

Describe your idea/feature/enhancement

The sam build command should run tests as well for Node runtime, just like it does for Java runtime.

Proposal

Include one more step below in the npm builder to run any available tests. One way to do this is to include a step that runs npm run test command, which will eventually run the command as specified in the package.json.

~ sam build
Starting Build use cache
Manifest file is changed (new hash: 64859513bdbd90c2fad6e086b18218ab) or dependency folder (.aws-sam/deps/6b249e35-3277-4962-a9f6-31718d68bd22) is
missing for (HelloWorldFunction), downloading dependencies and copying/building source
Building codeuri: /Users/kush.saraiya/Documents/Workspace/Personal/sample-sam-app/hello-world runtime: nodejs20.x metadata: {} architecture: x86_64
functions: HelloWorldFunction
 Running NodejsNpmBuilder:NpmPack
 Running NodejsNpmBuilder:CopyNpmrcAndLockfile
 Running NodejsNpmBuilder:CopySource
 Running NodejsNpmBuilder:NpmInstall
 Running NodejsNpmBuilder:CleanUp
 Running NodejsNpmBuilder:CopyDependencies
 Running NodejsNpmBuilder:CleanUpNpmrc
 Running NodejsNpmBuilder:LockfileCleanUp
 Running NodejsNpmBuilder:LockfileCleanUp

Build Succeeded

Things to consider:

  1. Will this require any updates to the SAM Spec - No

Additional Details

I came across this limitation where I was maintaining two SAM projects - one with Java and one with Node. We used sam build in our pipeline for both of them and I noticed that the sam build command ran the Java unit tests but not for Node. I think this will be a great feature to be added.

I am very much interested to work on this feature and can open a PR if you think this is worth it.

hawflau commented 1 month ago

Hey @saraiyakush thanks for raising the feature request. Your proposal makes sense and I think it's a good feature to add. Feel free to raise a PR. We will review it when it's ready!

FYI the underlying build workflow is powered by AWS Lambda Builders - https://github.com/aws/aws-lambda-builders. I think your change will likely be made in Lambda Builders

saraiyakush commented 1 week ago

Opened the PR https://github.com/aws/aws-lambda-builders/pull/681