Skip to content

Commit bad1b65

Browse files
committed
Add unit tests to aws-typescript
1 parent cfd629c commit bad1b65

6 files changed

Lines changed: 78 additions & 12 deletions

File tree

aws-typescript/index.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import * as pulumi from "@pulumi/pulumi";
2-
import * as aws from "@pulumi/aws";
3-
import * as awsx from "@pulumi/awsx";
1+
import * as infra from "./infra";
42

5-
// Create an AWS resource (S3 Bucket)
6-
const bucket = new aws.s3.BucketV2("my-bucket");
7-
8-
// Export the name of the bucket
9-
export const bucketName = bucket.id;
3+
// Export the name of the bucket.
4+
export const bucketName = infra.bucket.id;

aws-typescript/infra.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as pulumi from "@pulumi/pulumi";
2+
import * as aws from "@pulumi/aws";
3+
import * as awsx from "@pulumi/awsx";
4+
5+
// Create an AWS resource (S3 Bucket) with tags.
6+
export const bucket = new aws.s3.BucketV2("my-bucket", {
7+
tags: {
8+
"Name": "My bucket",
9+
},
10+
});

aws-typescript/jest.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @type {import('ts-jest').JestConfigWithTsJest} **/
2+
module.exports = {
3+
testEnvironment: "node",
4+
transform: {
5+
"^.+.tsx?$": ["ts-jest", {}],
6+
},
7+
};

aws-typescript/package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,18 @@
22
"name": "${PROJECT}",
33
"main": "index.ts",
44
"devDependencies": {
5+
"@types/jest": "^29.5.14",
56
"@types/node": "^18",
7+
"jest": "^29.7.0",
8+
"ts-jest": "^29.2.5",
69
"typescript": "^5.0.0"
710
},
811
"dependencies": {
912
"@pulumi/aws": "^6.0.0",
1013
"@pulumi/awsx": "^2.0.2",
1114
"@pulumi/pulumi": "^3.113.0"
15+
},
16+
"scripts": {
17+
"test": "jest"
1218
}
1319
}

aws-typescript/tests/infra.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
});

aws-typescript/tsconfig.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,5 @@
1111
"noFallthroughCasesInSwitch": true,
1212
"noImplicitReturns": true,
1313
"forceConsistentCasingInFileNames": true
14-
},
15-
"files": [
16-
"index.ts"
17-
]
14+
}
1815
}

0 commit comments

Comments
 (0)