|
| 1 | +import * as pulumi from "@pulumi/pulumi"; |
| 2 | +import "jest"; |
| 3 | + |
| 4 | +// Test helper to convert a Pulumi Output to a Promise. |
| 5 | +// This should only be used in tests. |
| 6 | +function promiseOf<T>(output: pulumi.Output<T>): Promise<T> { |
| 7 | + return new Promise(resolve => output.apply(resolve)); |
| 8 | +} |
| 9 | + |
| 10 | +describe("infrastructure", () => { |
| 11 | + // Define the infra variable as a type whose shape matches the that of the |
| 12 | + // to-be-defined infra module. |
| 13 | + let infra: typeof import("../infra"); |
| 14 | + |
| 15 | + beforeAll(() => { |
| 16 | + // Put Pulumi in unit-test mode, mocking all calls to cloud-provider APIs. |
| 17 | + pulumi.runtime.setMocks({ |
| 18 | + // Mock calls to create new resources and return a canned response. |
| 19 | + newResource: (args: pulumi.runtime.MockResourceArgs) => { |
| 20 | + // Here, we're returning a same-shaped object for all resource types. |
| 21 | + // We could, however, use the arguments passed into this function to |
| 22 | + // customize the mocked-out properties of a particular resource. |
| 23 | + // See the unit-testing docs for details: |
| 24 | + // https://www.pulumi.com/docs/iac/concepts/testing/unit/ |
| 25 | + return { |
| 26 | + id: `${args.name}-id`, |
| 27 | + state: args.inputs, |
| 28 | + }; |
| 29 | + }, |
| 30 | + |
| 31 | + // Mock function calls and return an empty response. |
| 32 | + call: (args: pulumi.runtime.MockCallArgs) => { |
| 33 | + return {}; |
| 34 | + }, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(async () => { |
| 39 | + // Dynamically import the infra module. |
| 40 | + infra = await import("../infra"); |
| 41 | + }); |
| 42 | + |
| 43 | + // Example test. To run, uncomment and run `npm test`. |
| 44 | + // describe("bucket", () => { |
| 45 | + // it("must have a name tag", async () => { |
| 46 | + // const tags = await promiseOf(infra.bucket.tags); |
| 47 | + // expect(tags).toBeDefined(); |
| 48 | + // expect(tags).toHaveProperty("Name"); |
| 49 | + // }); |
| 50 | + // }); |
| 51 | +}); |
0 commit comments