Skip to content

Commit

Permalink
Example: how to unit test a component resource (pulumi#1063)
Browse files Browse the repository at this point in the history
* Example how to unit test a component resource

* Update testing-unit-ts/mocha/bucket_pair.ts

Co-authored-by: Piers Karsenbarg <[email protected]>
  • Loading branch information
jandom and pierskarsenbarg committed Sep 14, 2021
1 parent 1081007 commit f9aeb34
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
File renamed without changes.
7 changes: 3 additions & 4 deletions testing-unit-ts/README.md → testing-unit-ts/mocha/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ An example of writing mock-based unit tests with both infrastructure definition

## Prerequisites

1. [Ensure you have the latest Node.js and NPM](https://nodejs.org/en/download/).
2. [Install the Mocha test framework](https://mochajs.org/#installation).
[Ensure you have the latest Node.js and NPM](https://nodejs.org/en/download/).

## Running the tests

Expand All @@ -15,10 +14,10 @@ An example of writing mock-based unit tests with both infrastructure definition
$ npm install
```

2. Run the tests:
2. Run the tests, with `mocha` installed locally in `node_modules/`:

```
$ mocha -r ts-node/register ec2tests.ts
$ npx mocha -r ts-node/register ec2tests.ts
Infrastructure
#server
Expand Down
26 changes: 26 additions & 0 deletions testing-unit-ts/mocha/bucket_pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as aws from "@pulumi/aws"
import * as pulumi from "@pulumi/pulumi";

export class BucketPair extends pulumi.ComponentResource {

contentBucket: aws.s3.Bucket;
logsBucket: aws.s3.Bucket;

constructor(contentBucketName: string, logsBucketName: string, opts: any) {
super("pulumi:examples:BucketPair", "BucketPair", {}, opts);

this.contentBucket = new aws.s3.Bucket("contentBucket", {
bucket: contentBucketName,
}, { parent: this });

this.logsBucket = new aws.s3.Bucket("logsBucket", {
bucket: logsBucketName,
}, { parent: this });

// Register output properties for this component
this.registerOutputs({
contentBucket: this.contentBucket,
logsBucket: this.logsBucket
});
}
}
46 changes: 46 additions & 0 deletions testing-unit-ts/mocha/bucket_pair_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright 2016-2020, Pulumi Corporation. All rights reserved.

import * as pulumi from "@pulumi/pulumi";
import "mocha";
import * as assert from 'assert';

pulumi.runtime.setMocks({
newResource: function(args: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
switch (args.type) {
default:
return {
id: args.inputs.name + "_id",
state: {
...args.inputs,
},
};
}
},
call: function(args: pulumi.runtime.MockCallArgs) {
switch (args.token) {
default:
return args;
}
},
});

describe("BucketPair", function() {
let module: typeof import("./bucket_pair");

before(async function() {
// It's important to import the program _after_ the mocks are defined.
module = await import("./bucket_pair");
});

describe("constructor", function() {
it("must pass bucket names", function(done) {
const bucketPair = new module.BucketPair('my_content_bucket', 'my_logs_bucket', {});
const outputs = [bucketPair.contentBucket.bucket, bucketPair.logsBucket.bucket];
pulumi.all(outputs).apply(([contentBucketName, logsBucketName]) => {
assert.strictEqual(contentBucketName, 'my_content_bucket');
assert.strictEqual(logsBucketName, 'my_logs_bucket');
done();
});
});
});
});
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
},
"files": [
"index.ts",
"ec2tests.ts"
"ec2tests.ts",
"bucket_pair.ts",
"bucket_pair_test.ts",
]
}

0 comments on commit f9aeb34

Please sign in to comment.