-
Notifications
You must be signed in to change notification settings - Fork 68
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
[WIP] custom jest output matchers #868
Draft
julienp
wants to merge
2
commits into
justin/tests-aws-typescript
Choose a base branch
from
julienp/matchers
base: justin/tests-aws-typescript
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,4 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import * as aws from "@pulumi/aws"; | ||
import * as awsx from "@pulumi/awsx"; | ||
import * as infra from "./infra"; | ||
|
||
// Create an AWS resource (S3 Bucket) | ||
const bucket = new aws.s3.BucketV2("my-bucket"); | ||
|
||
// Export the name of the bucket | ||
export const bucketName = bucket.id; | ||
// Export the name of the bucket. | ||
export const bucketName = infra.bucket.id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import * as aws from "@pulumi/aws"; | ||
import * as awsx from "@pulumi/awsx"; | ||
|
||
// Create an AWS resource (S3 Bucket) with tags. | ||
export const bucket = new aws.s3.BucketV2("my-bucket", { | ||
tags: { | ||
"Name": "My bucket", | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('ts-jest').JestConfigWithTsJest} **/ | ||
module.exports = { | ||
testEnvironment: "node", | ||
transform: { | ||
"^.+.tsx?$": ["ts-jest", {}], | ||
}, | ||
setupFilesAfterEnv: ['./jest.matchers.js'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const { Output } = require("@pulumi/pulumi") | ||
|
||
expect.extend({ | ||
async toEqualOutputOf(actual, expected) { | ||
if (!actual instanceof Output) { | ||
throw new Error(`Actual value must be an Output, got ${typeof actual}`) | ||
} | ||
return new Promise(resolve => | ||
actual.apply(unwrapped => { | ||
if (this.equals(unwrapped, expected)) { | ||
resolve({ | ||
pass: true, | ||
}) | ||
} else { | ||
resolve({ | ||
message: () => `expected ${this.utils.printExpected(expected)} to equal ${this.utils.printReceived(unwrapped)}`, | ||
pass: false, | ||
}) | ||
} | ||
}) | ||
) | ||
}, | ||
|
||
async apply(actual, applyFn) { | ||
if (!actual instanceof Output) { | ||
throw new Error(`Actual value must be an Output, got ${typeof actual}`) | ||
} | ||
return new Promise(resolve => | ||
actual.apply(async (...args) => { | ||
try { | ||
await applyFn(...args) | ||
resolve({ | ||
pass: true, | ||
}) | ||
} catch (e) { | ||
resolve({ | ||
message: () => e.message, | ||
pass: false, | ||
}) | ||
} | ||
}) | ||
) | ||
}, | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import * as pulumi from "@pulumi/pulumi"; | ||
import "jest"; | ||
|
||
// Test helper to convert a Pulumi Output to a Promise. | ||
// This should only be used in tests. | ||
function promiseOf<T>(output: pulumi.Output<T>): Promise<T> { | ||
return new Promise(resolve => output.apply(resolve)); | ||
} | ||
|
||
describe("infrastructure", () => { | ||
// Define the infra variable as a type whose shape matches the that of the | ||
// to-be-defined infra module. | ||
let infra: typeof import("../infra"); | ||
|
||
beforeAll(() => { | ||
// Put Pulumi in unit-test mode, mocking all calls to cloud-provider APIs. | ||
pulumi.runtime.setMocks({ | ||
// Mock calls to create new resources and return a canned response. | ||
newResource: (args: pulumi.runtime.MockResourceArgs) => { | ||
// Here, we're returning a same-shaped object for all resource types. | ||
// We could, however, use the arguments passed into this function to | ||
// customize the mocked-out properties of a particular resource. | ||
// See the unit-testing docs for details: | ||
// https://www.pulumi.com/docs/iac/concepts/testing/unit/ | ||
return { | ||
id: `${args.name}-id`, | ||
state: args.inputs, | ||
}; | ||
}, | ||
|
||
// Mock function calls and return an empty response. | ||
call: (args: pulumi.runtime.MockCallArgs) => { | ||
return {}; | ||
}, | ||
}); | ||
}); | ||
|
||
beforeEach(async () => { | ||
// Dynamically import the infra module. | ||
infra = await import("../infra"); | ||
}); | ||
|
||
// Example test. To run, uncomment and run `npm test`. | ||
describe("bucket", () => { | ||
it("must have a name tag", async () => { | ||
const tags = await promiseOf(infra.bucket.tags); | ||
expect(tags).toBeDefined(); | ||
expect(tags).toHaveProperty("Name"); | ||
}); | ||
it("must have the right tags", async () => { | ||
await (expect(infra.bucket.tags) as any).toEqualOutputOf({ "Name": "My bucket oops" }) | ||
}) | ||
it("some apply thing", async () => { | ||
await (expect(infra.bucket.tags) as any).apply((tags: any) => { | ||
expect(tags).toBeDefined(); | ||
expect(tags).toHaveProperty("Name2"); | ||
}) | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't extend the
expect
type to know about our matchers, with that we won't need the cast toany
here.