Skip to content

Commit d335fa8

Browse files
authored
Fix TypeScript unit testing example (#2171)
The unit testing example does not work for systems running node 23.x. This commit adds importing files by extension and the necessary TS config settings to permit importing files by extension.
1 parent a75e0e8 commit d335fa8

File tree

3 files changed

+20
-19
lines changed

3 files changed

+20
-19
lines changed

testing-unit-ts/mocha/bucket_pair_test.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "mocha";
55
import * as assert from 'assert';
66

77
pulumi.runtime.setMocks({
8-
newResource: function (args: pulumi.runtime.MockResourceArgs): { id: string, state: any } {
8+
newResource: function (args: pulumi.runtime.MockResourceArgs): { id: string, state: any; } {
99
switch (args.type) {
1010
default:
1111
return {
@@ -27,12 +27,12 @@ pulumi.runtime.setMocks({
2727
describe("BucketPair", function () {
2828
this.timeout(10000); // Extend the timeout for this suite
2929

30-
let module: typeof import("./bucket_pair");
30+
let module: typeof import("./bucket_pair.ts");
3131

3232
before(async function () {
3333
this.timeout(10000); // Extend timeout for the import
3434
// It's important to import the program _after_ the mocks are defined.
35-
module = await import("./bucket_pair");
35+
module = await import("./bucket_pair.ts");
3636
});
3737

3838
describe("constructor", function () {

testing-unit-ts/mocha/ec2tests.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as pulumi from "@pulumi/pulumi";
44
import "mocha";
55

66
pulumi.runtime.setMocks({
7-
newResource: function(args: pulumi.runtime.MockResourceArgs): {id: string, state: any} {
7+
newResource: function (args: pulumi.runtime.MockResourceArgs): { id: string, state: any; } {
88
switch (args.type) {
99
case "aws:ec2/securityGroup:SecurityGroup":
1010
return {
@@ -39,7 +39,7 @@ pulumi.runtime.setMocks({
3939
};
4040
}
4141
},
42-
call: function(args: pulumi.runtime.MockCallArgs) {
42+
call: function (args: pulumi.runtime.MockCallArgs) {
4343
switch (args.token) {
4444
case "aws:ec2/getAmi:getAmi":
4545
return {
@@ -52,17 +52,17 @@ pulumi.runtime.setMocks({
5252
},
5353
});
5454

55-
describe("Infrastructure", function() {
56-
let infra: typeof import("./index");
55+
describe("Infrastructure", function () {
56+
let infra: typeof import("./index.ts");
5757

58-
before(async function() {
58+
before(async function () {
5959
// It's important to import the program _after_ the mocks are defined.
60-
infra = await import("./index");
60+
infra = await import("./index.ts");
6161
});
6262

63-
describe("#server", function() {
63+
describe("#server", function () {
6464
// check 1: Instances have a Name tag.
65-
it("must have a name tag", function(done) {
65+
it("must have a name tag", function (done) {
6666
pulumi.all([infra.server.urn, infra.server.tags]).apply(([urn, tags]) => {
6767
if (!tags || !tags["Name"]) {
6868
done(new Error(`Missing a name tag on server ${urn}`));
@@ -73,7 +73,7 @@ describe("Infrastructure", function() {
7373
});
7474

7575
// check 2: Instances must not use an inline userData script.
76-
it("must not use userData (use an AMI instead)", function(done) {
76+
it("must not use userData (use an AMI instead)", function (done) {
7777
pulumi.all([infra.server.urn, infra.server.userData]).apply(([urn, userData]) => {
7878
if (userData) {
7979
done(new Error(`Illegal use of userData on server ${urn}`));
@@ -84,13 +84,13 @@ describe("Infrastructure", function() {
8484
});
8585
});
8686

87-
describe("#group", function() {
87+
describe("#group", function () {
8888
// check 3: Instances must not have SSH open to the Internet.
89-
it("must not open port 22 (SSH) to the Internet", function(done) {
90-
pulumi.all([infra.group.urn, infra.group.ingress]).apply(([ urn, ingress ]) => {
89+
it("must not open port 22 (SSH) to the Internet", function (done) {
90+
pulumi.all([infra.group.urn, infra.group.ingress]).apply(([urn, ingress]) => {
9191
if (ingress.find(rule =>
9292
rule.fromPort === 22 && (rule.cidrBlocks || []).find(block => block === "0.0.0.0/0"))) {
93-
done(new Error(`Illegal SSH port 22 open to the Internet (CIDR 0.0.0.0/0) on group ${urn}`));
93+
done(new Error(`Illegal SSH port 22 open to the Internet (CIDR 0.0.0.0/0) on group ${urn}`));
9494
} else {
9595
done();
9696
}

testing-unit-ts/mocha/tsconfig.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
"pretty": true,
1313
"noFallthroughCasesInSwitch": true,
1414
"noImplicitReturns": true,
15-
"forceConsistentCasingInFileNames": true
15+
"forceConsistentCasingInFileNames": true,
16+
"allowImportingTsExtensions": true,
17+
"noEmit": true
1618
},
1719
"files": [
1820
"index.ts",
1921
"ec2tests.ts",
2022
"bucket_pair.ts",
2123
"bucket_pair_test.ts",
2224
]
23-
}
24-
25+
}

0 commit comments

Comments
 (0)