https://pulumi.com logo
#typescript
Title
# typescript
w

worried-boots-97291

05/11/2021, 10:57 AM
Hello, we're using Pulumi v3.2.1 and Azure-Native 1.1.0. We want to unit test creating resources. We've followed the guide here: https://www.pulumi.com/docs/guides/testing/unit/ Unit tests are written in Typescript with Jest Our Mock looks like this:
Copy code
runtime.setMocks({
    newResource(args: MockResourceArgs): { id: string | undefined; state: Record<string, any> } {
        const { type, id, name } = args;
        expect(type).toEqual("azure-nextgen:network/latest:Subnet");
        return {
            id,
            state: {
                ...args.inputs,
                name,
            },
        };
    },
    call(args: MockCallArgs) {
        return args.inputs;
    },
});
Running tests give: `Program run without the Pulumi engine available; re-run using the
pulumi
CLI` Is anyone able to help with why this is?
l

little-cartoon-10569

05/11/2021, 8:48 PM
This isn't a problem with setMocks, just with the runtime not detecting that you're running as a test. Normally it does detect this correctly, but it is possible to "break" this detection. For example, in some of my test code I use pulumi.runtime.registerStackTransformation() which does not play well with Pulumi's test-detection logic.
The workaround is to run the tests where this problem happens within a pulumi.runtime.runInPulumiStack() block. You don't need to change your pulumi.runtime.setMocks() code, just the tests where Pulumi resources are constructed.
In my code, the calls to setMocks() and runInPulumiStack() are at the same level. It's important not to nest setMocks() inside runInPulumiStack().