https://pulumi.com logo
Title
c

crooked-dawn-93088

01/04/2022, 2:07 PM
Hi there, I got a question regarding unit testing: My tests are running in a time-out presumably because the infrastructure definition contains a getter. How to mock a getter on an existing resource? Let's say this is the index.ts with a
get
to import the default security group for further reference:
import * as aws from '@pulumi/aws';
import * as awsx from '@pulumi/awsx';

export const vpc = new awsx.ec2.Vpc('mdm-db-vpc', {
  subnets: [{ type: 'isolated' }],
});

export const securityGroup = aws.ec2.SecurityGroup.get(
  'default',
  vpc.vpc.defaultSecurityGroupId,
);
l

little-cartoon-10569

01/04/2022, 8:07 PM
The
setMock
function takes an object that must have a
call
function. This function is where you fake out calls to Pulumi functions.
In this case, it is the way to go.
The docs show where the code goes, but there's no example code: https://www.pulumi.com/docs/guides/testing/unit/#add-mocks
However there is an example of the
aws.ec2.getAmi()
function being faked here: https://github.com/pulumi/examples/blob/947273e7a62cf31649559344b2dd8df78bddacd9/testing-unit-ts/mocha/ec2tests.ts#L44
c

crooked-dawn-93088

01/05/2022, 7:51 PM
Thanks for the hints but it doesn't seem that the getter can be mocked within the
call
function of the mocks. I logged
args.token
of all invokations to the console and none of them where matching but I still had this error message.
[UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "Error: failed to read resource #undefined 'default' [aws:ec2/securityGroup:SecurityGroup]: req.getCustom is not a function".] {
  code: 'ERR_UNHANDLED_REJECTION'
}
Anyway I considered that I do not need to export the whole
securityGroup
just the
securityGroupId
that I already got from the VPC so problem solved. 🙂