|
| 1 | +import { App, Stack, CfnResource } from 'aws-cdk-lib'; |
| 2 | +import { Template } from 'aws-cdk-lib/assertions'; |
| 3 | +import { addCfnResourceDependency } from '../utils/cfn-dependency'; |
| 4 | + |
| 5 | +const makePair = () => { |
| 6 | + const stack = new Stack(new App(), 'TestStack'); |
| 7 | + const target = new CfnResource(stack, 'Target', { type: 'AWS::IAM::Role', properties: {} }); |
| 8 | + const source = new CfnResource(stack, 'Source', { type: 'AWS::AppSync::DataSource', properties: {} }); |
| 9 | + return { stack, source, target }; |
| 10 | +}; |
| 11 | + |
| 12 | +describe('addCfnResourceDependency', () => { |
| 13 | + it('emits a CloudFormation DependsOn entry', () => { |
| 14 | + const { stack, source, target } = makePair(); |
| 15 | + addCfnResourceDependency(source, target); |
| 16 | + const resources = Template.fromStack(stack).toJSON().Resources; |
| 17 | + expect(resources.Source.DependsOn).toEqual([stack.resolve(target.logicalId)]); |
| 18 | + }); |
| 19 | + |
| 20 | + it('prefers addResourceDependency when the installed CDK exposes it', () => { |
| 21 | + const { source, target } = makePair(); |
| 22 | + const spy = jest.fn(); |
| 23 | + (source as unknown as Record<string, unknown>).addResourceDependency = spy; |
| 24 | + const legacy = jest.spyOn(source, 'addDependency'); |
| 25 | + addCfnResourceDependency(source, target); |
| 26 | + expect(spy).toHaveBeenCalledWith(target); |
| 27 | + expect(legacy).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + it('falls back to addDependency on CDK versions without addResourceDependency', () => { |
| 31 | + const { source, target } = makePair(); |
| 32 | + const capable = source as unknown as { addResourceDependency?: unknown }; |
| 33 | + const original = capable.addResourceDependency; |
| 34 | + capable.addResourceDependency = undefined; |
| 35 | + const legacy = jest.spyOn(source, 'addDependency'); |
| 36 | + addCfnResourceDependency(source, target); |
| 37 | + expect(legacy).toHaveBeenCalledWith(target); |
| 38 | + capable.addResourceDependency = original; |
| 39 | + }); |
| 40 | +}); |
0 commit comments