From f9aeb341400fd5123a7edffcb417c0f7f72e10ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Doma=C5=84ski?= Date: Tue, 14 Sep 2021 10:57:19 +0200 Subject: [PATCH] Example: how to unit test a component resource (#1063) * Example how to unit test a component resource * Update testing-unit-ts/mocha/bucket_pair.ts Co-authored-by: Piers Karsenbarg --- testing-unit-ts/{ => mocha}/Pulumi.yaml | 0 testing-unit-ts/{ => mocha}/README.md | 7 ++-- testing-unit-ts/mocha/bucket_pair.ts | 26 +++++++++++++ testing-unit-ts/mocha/bucket_pair_test.ts | 46 +++++++++++++++++++++++ testing-unit-ts/{ => mocha}/ec2tests.ts | 0 testing-unit-ts/{ => mocha}/index.ts | 0 testing-unit-ts/{ => mocha}/package.json | 0 testing-unit-ts/{ => mocha}/tsconfig.json | 4 +- 8 files changed, 78 insertions(+), 5 deletions(-) rename testing-unit-ts/{ => mocha}/Pulumi.yaml (100%) rename testing-unit-ts/{ => mocha}/README.md (78%) create mode 100644 testing-unit-ts/mocha/bucket_pair.ts create mode 100644 testing-unit-ts/mocha/bucket_pair_test.ts rename testing-unit-ts/{ => mocha}/ec2tests.ts (100%) rename testing-unit-ts/{ => mocha}/index.ts (100%) rename testing-unit-ts/{ => mocha}/package.json (100%) rename testing-unit-ts/{ => mocha}/tsconfig.json (86%) diff --git a/testing-unit-ts/Pulumi.yaml b/testing-unit-ts/mocha/Pulumi.yaml similarity index 100% rename from testing-unit-ts/Pulumi.yaml rename to testing-unit-ts/mocha/Pulumi.yaml diff --git a/testing-unit-ts/README.md b/testing-unit-ts/mocha/README.md similarity index 78% rename from testing-unit-ts/README.md rename to testing-unit-ts/mocha/README.md index 8a9d51f80..b416f957b 100644 --- a/testing-unit-ts/README.md +++ b/testing-unit-ts/mocha/README.md @@ -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 @@ -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 diff --git a/testing-unit-ts/mocha/bucket_pair.ts b/testing-unit-ts/mocha/bucket_pair.ts new file mode 100644 index 000000000..10cf2d75a --- /dev/null +++ b/testing-unit-ts/mocha/bucket_pair.ts @@ -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 + }); + } +} \ No newline at end of file diff --git a/testing-unit-ts/mocha/bucket_pair_test.ts b/testing-unit-ts/mocha/bucket_pair_test.ts new file mode 100644 index 000000000..b0025c5c1 --- /dev/null +++ b/testing-unit-ts/mocha/bucket_pair_test.ts @@ -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(); + }); + }); + }); +}); diff --git a/testing-unit-ts/ec2tests.ts b/testing-unit-ts/mocha/ec2tests.ts similarity index 100% rename from testing-unit-ts/ec2tests.ts rename to testing-unit-ts/mocha/ec2tests.ts diff --git a/testing-unit-ts/index.ts b/testing-unit-ts/mocha/index.ts similarity index 100% rename from testing-unit-ts/index.ts rename to testing-unit-ts/mocha/index.ts diff --git a/testing-unit-ts/package.json b/testing-unit-ts/mocha/package.json similarity index 100% rename from testing-unit-ts/package.json rename to testing-unit-ts/mocha/package.json diff --git a/testing-unit-ts/tsconfig.json b/testing-unit-ts/mocha/tsconfig.json similarity index 86% rename from testing-unit-ts/tsconfig.json rename to testing-unit-ts/mocha/tsconfig.json index d05576788..6eddd9745 100644 --- a/testing-unit-ts/tsconfig.json +++ b/testing-unit-ts/mocha/tsconfig.json @@ -16,7 +16,9 @@ }, "files": [ "index.ts", - "ec2tests.ts" + "ec2tests.ts", + "bucket_pair.ts", + "bucket_pair_test.ts", ] }