Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example: how to unit test a component resource #1063

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 23 additions & 0 deletions testing-unit-ts/mocha/bucket_pair.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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();
jandom marked this conversation as resolved.
Show resolved Hide resolved
}
}
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",
]
}