Skip to content

Commit 7b15b7f

Browse files
committed
test(lambda): improve tests
1 parent 7f87164 commit 7b15b7f

File tree

2 files changed

+44
-70
lines changed

2 files changed

+44
-70
lines changed

packages/core/src/test/awsService/appBuilder/lambda2sam/lambda2samCoreLogic.test.ts

Lines changed: 38 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,7 @@ describe('lambda2samCoreLogic', function () {
431431
} as any
432432

433433
// Setup Lambda node
434-
const lambdaNode = {
435-
name: 'test-function',
436-
regionCode: 'us-west-2',
437-
} as LambdaFunctionNode
434+
const lambdaNode = mockLambdaNode()
438435

439436
const resourceToImport: ResourcesToImport = [
440437
{
@@ -490,10 +487,7 @@ describe('lambda2samCoreLogic', function () {
490487
} as any
491488

492489
// Setup Lambda node
493-
const lambdaNode = {
494-
name: 'test-function',
495-
regionCode: 'us-west-2',
496-
} as LambdaFunctionNode
490+
const lambdaNode = mockLambdaNode()
497491

498492
// Make createChangeSet fail
499493
cfnClientStub.createChangeSet.resolves({}) // No Id
@@ -522,24 +516,10 @@ describe('lambda2samCoreLogic', function () {
522516
describe('callExternalApiForCfnTemplate', function () {
523517
it('extracts function name from ARN in ResourceIdentifier', async function () {
524518
// Setup Lambda node
525-
const lambdaNode = {
526-
name: 'test-function',
527-
regionCode: 'us-east-2',
528-
arn: 'arn:aws:lambda:us-east-2:123456789012:function:test-function',
529-
} as LambdaFunctionNode
519+
const lambdaNode = mockLambdaNode(true)
530520

531521
// Mock IAM connection
532-
const mockConnection = {
533-
type: 'iam' as const,
534-
id: 'test-connection',
535-
label: 'Test Connection',
536-
state: 'valid' as const,
537-
getCredentials: sandbox.stub().resolves({
538-
accessKeyId: 'test-key',
539-
secretAccessKey: 'test-secret',
540-
}),
541-
endpointUrl: undefined,
542-
}
522+
const mockConnection = mockIamConnection()
543523
sandbox.stub(authUtils, 'getIAMConnection').resolves(mockConnection)
544524

545525
// Mock fetch response
@@ -581,24 +561,10 @@ describe('lambda2samCoreLogic', function () {
581561

582562
it('preserves function name when not an ARN', async function () {
583563
// Setup Lambda node
584-
const lambdaNode = {
585-
name: 'test-function',
586-
regionCode: 'us-east-2',
587-
arn: 'arn:aws:lambda:us-east-2:123456789012:function:test-function',
588-
} as LambdaFunctionNode
564+
const lambdaNode = mockLambdaNode(true)
589565

590566
// Mock IAM connection
591-
const mockConnection = {
592-
type: 'iam' as const,
593-
id: 'test-connection',
594-
label: 'Test Connection',
595-
state: 'valid' as const,
596-
getCredentials: sandbox.stub().resolves({
597-
accessKeyId: 'test-key',
598-
secretAccessKey: 'test-secret',
599-
}),
600-
endpointUrl: undefined,
601-
}
567+
const mockConnection = mockIamConnection()
602568
sandbox.stub(authUtils, 'getIAMConnection').resolves(mockConnection)
603569

604570
// Mock fetch response
@@ -633,24 +599,10 @@ describe('lambda2samCoreLogic', function () {
633599

634600
it('handles non-Lambda resources without modification', async function () {
635601
// Setup Lambda node
636-
const lambdaNode = {
637-
name: 'test-function',
638-
regionCode: 'us-east-2',
639-
arn: 'arn:aws:lambda:us-east-2:123456789012:function:test-function',
640-
} as LambdaFunctionNode
602+
const lambdaNode = mockLambdaNode(true)
641603

642604
// Mock IAM connection
643-
const mockConnection = {
644-
type: 'iam' as const,
645-
id: 'test-connection',
646-
label: 'Test Connection',
647-
state: 'valid' as const,
648-
getCredentials: sandbox.stub().resolves({
649-
accessKeyId: 'test-key',
650-
secretAccessKey: 'test-secret',
651-
}),
652-
endpointUrl: undefined,
653-
}
605+
const mockConnection = mockIamConnection()
654606
sandbox.stub(authUtils, 'getIAMConnection').resolves(mockConnection)
655607

656608
// Mock fetch response
@@ -699,10 +651,7 @@ describe('lambda2samCoreLogic', function () {
699651
describe('lambdaToSam', function () {
700652
it('converts a Lambda function to a SAM project', async function () {
701653
// Setup Lambda node
702-
const lambdaNode = {
703-
name: 'test-function',
704-
regionCode: 'us-west-2',
705-
} as LambdaFunctionNode
654+
const lambdaNode = mockLambdaNode()
706655

707656
// Setup AWS Lambda client responses
708657
lambdaClientStub.getFunction.resolves({
@@ -784,4 +733,33 @@ describe('lambda2samCoreLogic', function () {
784733
)
785734
})
786735
})
736+
737+
function mockLambdaNode(withArn: boolean = false) {
738+
if (withArn) {
739+
return {
740+
name: 'test-function',
741+
regionCode: 'us-east-2',
742+
arn: 'arn:aws:lambda:us-east-2:123456789012:function:test-function',
743+
} as LambdaFunctionNode
744+
} else {
745+
return {
746+
name: 'test-function',
747+
regionCode: 'us-east-2',
748+
} as LambdaFunctionNode
749+
}
750+
}
751+
752+
function mockIamConnection() {
753+
return {
754+
type: 'iam' as const,
755+
id: 'test-connection',
756+
label: 'Test Connection',
757+
state: 'valid' as const,
758+
getCredentials: sandbox.stub().resolves({
759+
accessKeyId: 'test-key',
760+
secretAccessKey: 'test-secret',
761+
}),
762+
endpointUrl: undefined,
763+
}
764+
}
787765
})

packages/core/src/test/lambda/remoteDebugging/localStackLambdaDebugger.test.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('RemoteDebugController with LocalStackLambdaDebugger', () => {
6060
layerArn: undefined,
6161
lambdaTimeout: undefined,
6262
})
63-
mockFunctionConfig = createMockFunctionConfig()
63+
mockFunctionConfig = createMockFunctionConfig({ Runtime: 'nodejs22.x' })
6464
})
6565

6666
afterEach(() => {
@@ -128,14 +128,13 @@ describe('RemoteDebugController with LocalStackLambdaDebugger', () => {
128128
)
129129
)
130130

131-
await controller.startDebugging(mockConfig.functionArn, 'nodejs18.x', mockConfig)
131+
await controller.startDebugging(mockConfig.functionArn, 'nodejs22.x', mockConfig)
132132

133133
// Assert state changes
134134
assert.strictEqual(controller.isDebugging, true, 'Should be in debugging state')
135-
// Qualifier is only set for version publishing, not for $LATEST
135+
// Qualifier is not set for LocalStack
136136
assert.strictEqual(controller.qualifier, undefined, 'Should not set qualifier for $LATEST')
137137

138-
// Verify LdkClient calls
139138
assert(mockLdkClient.getFunctionDetail.calledWith(mockConfig.functionArn), 'Should get function details')
140139

141140
assert(fetchStubHealth.calledOnce, 'Should call LocalStack health check once')
@@ -146,7 +145,7 @@ describe('RemoteDebugController with LocalStackLambdaDebugger', () => {
146145
result: 'Succeeded',
147146
source: 'LocalStackDebug',
148147
action: '{"remoteRoot":"/var/task","skipFiles":[],"shouldPublishVersion":false,"isLambdaRemote":false}',
149-
runtimeString: 'nodejs18.x',
148+
runtimeString: 'nodejs22.x',
150149
})
151150
})
152151

@@ -186,11 +185,10 @@ describe('RemoteDebugController with LocalStackLambdaDebugger', () => {
186185
// Mock revertExistingConfig
187186
setupMockRevertExistingConfig(sandbox)
188187

189-
let errorThrown = false
190188
try {
191-
await controller.startDebugging(mockConfig.functionArn, 'nodejs18.x', mockConfig)
189+
await controller.startDebugging(mockConfig.functionArn, 'nodejs22.x', mockConfig)
190+
assert.fail('Should have thrown an error')
192191
} catch (error) {
193-
errorThrown = true
194192
assert(error instanceof Error, 'Should throw an error')
195193
assert(
196194
error.message.includes('Error StartDebugging') ||
@@ -201,8 +199,6 @@ describe('RemoteDebugController with LocalStackLambdaDebugger', () => {
201199
)
202200
}
203201

204-
assert(errorThrown, 'Should have thrown an error')
205-
206202
// Assert state is cleaned up
207203
assert.strictEqual(controller.isDebugging, false, 'Should not be in debugging state after failure')
208204
assert(fetchStubCleanup.calledOnce, 'Should attempt cleanup')

0 commit comments

Comments
 (0)