I wrote some unit tests for my Pulumi stack. The t...
# typescript
r
I wrote some unit tests for my Pulumi stack. The tests run correctly but I have promise leaks. I'm trying to debug the promise leaks but they aren't overly helpful. Any ideas of what it could be? Cloud provider is Azure. I am making some AD calls that I may suspect could be part of the issue.
l
Lots of ideas. Are you using Mocha? You need to return your assertions as promises from your it()s: you don't assert inside the function, let Mocha await them later.
This sort of thing:
Copy code
it('should exist', async function s3Bucket(): Promise<any> {
      return expect(bucketUnderTest.bucket.id.promise()).to.eventually.exist;
    });
r
Yes using mocha. I am awaiting the outputs and I have a utility function that converts the outputs to a promise. So example would be expect(await getOutput(keyvault.name)).to.equal(“blah”)
I'll try something like this
l
That's the problem then. You'll need to change that.
Use chai-as-promised to get the assertion as a promise and return it, else you'll have leaked promises.
👍 1
I suppose you might be able to return a redundant promise and always pass, but async Mocha requires promises to be returned.
It's not a Pulumi thing, it's the way async functions are handled by Mocha that's doing this.
Actually you can use done(), but I've never done that. Here's the docs: https://mochajs.org/#asynchronous-code
Hmm.. there's a bit in there ("Using async / await") that says your code should work... I may be misleading you....
I've never done it that way, sorry, I may have spoken too soon.
r
No worries, I converted to chai as promised and had leaks still but good to know this information and understand it better. I'm probably going to comment out the resources in chunks and see if I can pin down exactly what is the culprit and tackle it.
👍 1
I figured out my issue is I am using the function listDatabaseAccountConnectionStringsOutput and I need to mock a response output and haven't quite figured that piece out yet as to how to mock it.
l
That's done in pulumi.runtime.setMocks(), the
call()
function.
For example, I have found that constructing an instance of awsx.ec2.Vpc requires the getAvailabilityZones function to be stubbed out (it's not mocking!), and this is what I use:
Copy code
pulumi.runtime.setMocks({
      newResource: (type: string, name: string, inputs: any): { id: string, state: any } => {
        // ... snip ...
      },
      call: (token: string, args: any, _provider?: string) => {
        switch (token) {
          case "aws:index/getAvailabilityZones:getAvailabilityZones":
            return {
              names: ["ap-southeast-2a", "ap-southeast-2b", "ap-southeast-2c"],
              zoneIds: ["southeast2a", "southeast2b", "southeast2c"],
            };
        }
        return args;
      },
    });
r
That's very helpful I'm going to try that tomorrow. Thanks for all the help!
To update that fixed my issues! Thank you again.