Skip to content

Commit 26018e1

Browse files
committed
fixed unit tests
1 parent 5658ba1 commit 26018e1

23 files changed

+59
-148
lines changed

Common/Helpers.ts

-20
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,3 @@
1-
import { Output, all } from '@pulumi/pulumi';
2-
3-
/*** This is using for unit test */
4-
export function outputPromise<T>(
5-
values: Array<Output<T | undefined> | undefined>
6-
): Promise<T[]>;
7-
8-
export function outputPromise<T>(
9-
value: Output<T | undefined> | undefined
10-
): Promise<T>;
11-
12-
export function outputPromise<T>(values: any): any {
13-
if (Array.isArray(values))
14-
return new Promise<T[]>((resolve) =>
15-
all(values).apply((v) => resolve(v as T[]))
16-
);
17-
return new Promise<T>((resolve) =>
18-
all([values]).apply((v) => resolve(v[0] as T))
19-
);
20-
}
211

222
/** Replace all characters in string*/
233
export function replaceAll(value: string, search: string, replace: string) {

tsconfig.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"strict": true,
44
"outDir": "bin",
55
"target": "esnext",
6-
"lib": [
7-
"es2019"
8-
],
6+
"lib": ["es2019"],
97
"module": "commonjs",
108
"moduleResolution": "node",
119
"sourceMap": true,

tsconfig.test.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"compilerOptions": {
44
"module": "CommonJS",
55
"esModuleInterop": true,
6-
"allowSyntheticDefaultImports": true
6+
"allowSyntheticDefaultImports": true,
7+
"noImplicitAny": false
78
}
89
}

z_tests/ACR/ACR.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import creator from '../../ContainerRegistry';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
4+
55

66
describe('ContainerRegistry Creator tests', () => {
77
it('ContainerRegistry Creator', async () => {
88
const rs = await creator({
99
name: 'drunkcoding',
1010
});
1111

12-
const [n, g] = await outputPromise([
13-
(rs.registry as any).registryName,
14-
(rs.registry as any).resourceGroupName,
15-
]);
16-
expect(n).to.equal('drunkcoding4acr');
17-
expect(g).to.equal('global-grp-hbd');
12+
(rs.registry as any).registryName.apply(n => {
13+
expect(n).to.equal('drunkcoding4acr');
14+
console.log(n);
15+
});
16+
17+
(rs.registry as any).resourceGroupName.apply(g => expect(g).to.equal('global-grp-hbd'));
1818
});
1919
});

z_tests/Aks/Aks.test.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ import '../_tools/Mocks';
22

33
import creator from '../../Aks';
44
import { expect } from 'chai';
5-
import { outputPromise } from '../../Common/Helpers';
65

76
describe('Aks Creator tests', () => {
87
it('Aks Creator', async () => {
9-
10-
118
const group = { resourceGroupName: 'RG' };
9+
1210
const rs = await creator({
1311
name: 'cluster',
1412
group,
@@ -32,12 +30,8 @@ describe('Aks Creator tests', () => {
3230

3331
expect(rs!.aks).to.not.undefined;
3432

35-
const [g, n] = await outputPromise([
36-
(rs!.aks as any).nodeResourceGroup,
37-
(rs!.aks as any).resourceName,
38-
]);
33+
(rs!.aks as any).nodeResourceGroup.apply(g => expect(g).to.equal('test-stack-cluster-aks-nodes'));
34+
(rs!.aks as any).resourceName.apply(n => expect(n).to.equal('test-stack-cluster-aks'));
3935

40-
expect(g).to.equal('test-stack-cluster-aks-nodes');
41-
expect(n).to.equal('test-stack-cluster-aks');
42-
});
36+
}).timeout(5000);
4337
});

z_tests/AzAd/Role.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import roleCreator from '../../AzAd/Role';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
44
import { Environments } from '../../Common/AzureEnv';
5-
import { outputPromise } from '../../Common/Helpers';
65

76
describe('Role Creator tests', () => {
87
it('Role Creator', async () => {
@@ -15,7 +14,6 @@ describe('Role Creator tests', () => {
1514
permissions: [{ roleName: 'Contributor' }],
1615
});
1716

18-
const n = await outputPromise(group.displayName);
19-
expect(n).to.equal('ROL NON-PRD GLB HBD CONTRIBUTOR');
17+
group.displayName.apply(n => expect(n).to.equal('ROL NON-PRD GLB HBD CONTRIBUTOR'));
2018
});
2119
});

z_tests/Core/Random.test.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import '../_tools/Mocks';
33
import { randomPassword, randomSsh, randomUuId } from '../../Core/Random';
44

55
import { expect } from 'chai';
6-
import { outputPromise } from '../../Common/Helpers';
76

87
describe('Random Creator tests', () => {
98
it('Random Password monthly', async () => {
@@ -35,12 +34,13 @@ describe('Random Creator tests', () => {
3534
it('Random Uuid', async () => {
3635
const uid = randomUuId('aks');
3736

38-
const id = await outputPromise(uid.result);
39-
expect(id)
40-
.has.length(36)
41-
.and.match(
42-
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
43-
);
37+
uid.result.apply(id => {
38+
expect(id)
39+
.has.length(36)
40+
.and.match(
41+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$/i
42+
);
43+
});
4444
});
4545

4646
it('Random ssh', async () => {

z_tests/Core/ResourceCreator.test.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import * as native from '@pulumi/azure-native';
44

55
import { DefaultResourceArgs } from '../../types';
66
import { expect } from 'chai';
7-
import { outputPromise } from '../../Common/Helpers';
87
import rsCreator from '../../Core/ResourceCreator';
98

109
describe('Resource Creator tests. The resource creator will not reformat the name', () => {
@@ -13,8 +12,7 @@ describe('Resource Creator tests. The resource creator will not reformat the nam
1312
resourceGroupName: 'resource-group',
1413
} as native.resources.ResourceGroupArgs & DefaultResourceArgs);
1514

16-
const u = await outputPromise(rs.resource.urn);
17-
expect(u).to.include('resource-group');
15+
rs.resource.urn.apply(n => expect(n).to.include('resource-group'));
1816
});
1917

2018
it('Resource Creator with lock', async () => {
@@ -25,8 +23,7 @@ describe('Resource Creator tests. The resource creator will not reformat the nam
2523

2624
expect(locker).to.not.undefined;
2725

28-
const n = await outputPromise(locker!.name);
29-
expect(n).to.be.equal('resource-group-CanNotDelete');
26+
locker!.name.apply(n => expect(n).to.be.equal('resource-group-CanNotDelete'));
3027
});
3128

3229
it('Resource Creator with diagnostic', async () => {
@@ -37,7 +34,6 @@ describe('Resource Creator tests. The resource creator will not reformat the nam
3734

3835
expect(diagnostic).to.not.undefined;
3936

40-
const n = await outputPromise(diagnostic!.name);
41-
expect(n).to.be.equal('resource-group-diag');
37+
diagnostic!.name.apply(n => expect(n).to.be.equal('resource-group-diag'));
4238
});
4339
});

z_tests/Logs/AppInsight.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import '../_tools/Mocks';
22

33
import creator from '../../Logs/AppInsight';
44
import { expect } from 'chai';
5-
import { outputPromise } from '../../Common/Helpers';
65

76
describe('AppInsight Creator tests', () => {
87
it('AppInsight Creator', async () => {
@@ -12,7 +11,6 @@ describe('AppInsight Creator tests', () => {
1211
group,
1312
});
1413

15-
const n = await outputPromise((rs as any).resourceName);
16-
expect(n).to.equal('test-stack-dev-isg');
14+
(rs as any).resourceName.apply(n => expect(n).to.equal('test-stack-dev-isg'));
1715
});
1816
});

z_tests/Logs/LogAnalytics.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../Logs/LogAnalytics';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54

65
describe('LogAnalytics Creator tests', () => {
76
it('LogAnalytics Creator', async () => {
@@ -11,7 +10,6 @@ describe('LogAnalytics Creator tests', () => {
1110
group,
1211
});
1312

14-
const n = await outputPromise((rs.log as any).workspaceName);
15-
expect(n).to.equal('test-stack-root-log');
13+
(rs.log as any).workspaceName.apply(n => expect(n).to.equal('test-stack-root-log'));
1614
});
1715
});

z_tests/RedisCahe/RedisCache.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../RedisCache';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54

65
describe('RedisCache Creator tests', () => {
76
it('Redis Cache Creator', async () => {
@@ -11,7 +10,6 @@ describe('RedisCache Creator tests', () => {
1110
group,
1211
});
1312

14-
const n = await outputPromise(rs.name);
15-
expect(n).to.equal('test-stack-cache-rds');
13+
rs.name.apply(n => expect(n).to.equal('test-stack-cache-rds'));
1614
});
1715
});

z_tests/ServiceBus/ServiceBus.test.ts

+5-16
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../ServiceBus';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54

65
describe('ServiceBus Creator tests', function () {
76
this.timeout(5000);
@@ -15,8 +14,7 @@ describe('ServiceBus Creator tests', function () {
1514
//vaultInfo,
1615
});
1716

18-
const n = await outputPromise((rs.resource as any).namespaceName);
19-
expect(n).to.equal('test-stack-aks-bus');
17+
(rs.resource as any).namespaceName.apply(n => expect(n).to.equal('test-stack-aks-bus'));
2018

2119
expect(rs.topics).to.undefined;
2220
expect(rs.queues).to.undefined;
@@ -51,19 +49,11 @@ describe('ServiceBus Creator tests', function () {
5149
await Promise.all(
5250
rs.topics!.map(async (t) => {
5351
//Verify the topic name
54-
const n = await outputPromise((t.topic as any).topicName);
55-
expect(n).to.equal('cake-v1-tp');
52+
(t.topic as any).topicName.apply(n => expect(n).to.equal('cake-v1-tp'));
5653

5754
//Verify the Subscription name
58-
const sn1 = await outputPromise(
59-
(t.subs![0].resource as any).subscriptionName
60-
);
61-
expect(sn1).to.equal('eat-cakev1-sub');
62-
63-
const sn2 = await outputPromise(
64-
(t.subs![1].resource as any).subscriptionName
65-
);
66-
expect(sn2).to.equal('eat-cakev1-session-sub');
55+
(t.subs![0].resource as any).subscriptionName.apply(sn1 => expect(sn1).to.equal('eat-cakev1-sub'));
56+
(t.subs![1].resource as any).subscriptionName.apply(sn2 => expect(sn2).to.equal('eat-cakev1-session-sub'));
6757
})
6858
);
6959
});
@@ -83,8 +73,7 @@ describe('ServiceBus Creator tests', function () {
8373
await Promise.all(
8474
rs.queues!.map(async (t) => {
8575
//Verify the topic name
86-
const n = await outputPromise((t.queue as any).queueName);
87-
expect(n).to.equal('cake-v1-que');
76+
(t.queue as any).queueName.apply(n => expect(n).to.equal('cake-v1-que'));
8877
})
8978
);
9079
});

z_tests/SignalR/SignalR.test.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../SignalR';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54

65
describe('SignalR Creator tests', () => {
76
it('SignalR Creator', async () => {
@@ -12,11 +11,7 @@ describe('SignalR Creator tests', () => {
1211
group,
1312
});
1413

15-
const n = await outputPromise([
16-
(signalR as any).resourceName,
17-
(privateEndpoint as any).privateEndpointName,
18-
]);
19-
expect(n[0]).to.equal('test-stack-aaa-sigr');
20-
expect(n[1]).to.equal('test-stack-aaa-pre');
14+
(signalR as any).resourceName.apply(n => expect(n).to.equal('test-stack-aaa-sigr'));
15+
(privateEndpoint as any).privateEndpointName.apply(n => expect(n).to.equal('test-stack-aaa-pre'));
2116
});
2217
});

z_tests/SqlServer/Sql.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../Sql';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54
import { defaultAlertEmails } from '../../Common/GlobalEnv';
65

76
describe('Sql Creator tests', () => {
@@ -25,16 +24,14 @@ describe('Sql Creator tests', () => {
2524
databases: [{ name: 'hello' }],
2625
});
2726

28-
const n = await outputPromise((rs.resource as any).serverName);
29-
expect(n).to.equal('test-stack-aks-sql');
27+
(rs.resource as any).serverName.apply(n => expect(n).to.equal('test-stack-aks-sql'));
3028

3129
expect(rs.elasticPool).to.not.undefined;
3230
expect(rs.databases).to.not.undefined;
3331

3432
await Promise.all(
3533
rs.databases!.map(async (db) => {
36-
const n = await outputPromise((db.resource as any).databaseName);
37-
expect(n).to.equal('test-stack-hello-db');
34+
(db.resource as any).databaseName.apply(n => expect(n).to.equal('test-stack-hello-db'));
3835
})
3936
);
4037
});

z_tests/Storage/Storage.test.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import creator from '../../Storage';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54

65
describe('Storage Creator tests', () => {
76
it('Storage Creator', async () => {
@@ -10,8 +9,7 @@ describe('Storage Creator tests', () => {
109
group: { resourceGroupName: 'RG' },
1110
});
1211

13-
const n = await outputPromise((rs.storage as any).accountName);
14-
expect(n).to.equal('teststackstoragestg');
12+
(rs.storage as any).accountName.apply(n => expect(n).to.equal('teststackstoragestg'));
1513
});
1614

1715
it('Storage Creator', async () => {
@@ -21,7 +19,6 @@ describe('Storage Creator tests', () => {
2119
group: { resourceGroupName: 'RG' },
2220
});
2321

24-
const n = await outputPromise((rs.storage as any).allowSharedKeyAccess);
25-
expect(n).to.equal(true);
22+
(rs.storage as any).allowSharedKeyAccess.apply(n => expect(n).to.equal(true));
2623
});
2724
});

z_tests/Vault/AddSecret.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { addKey, addSecret, getKey } from '../../KeyVault/Helper';
22
import '../_tools/Mocks';
33
import { expect } from 'chai';
4-
import { outputPromise } from '../../Common/Helpers';
54
import { KeyVaultInfo } from '../../types';
65

76
describe('Key Vault tests', () => {
@@ -17,8 +16,8 @@ describe('Key Vault tests', () => {
1716
vaultInfo,
1817
});
1918

20-
const n = await outputPromise((rs as any).keyName);
21-
expect(n).to.equal('cache-primary-connection-string');
19+
20+
(rs as any).keyName.apply(n => expect(n).to.equal('cache-primary-connection-string'));
2221
});
2322

2423
it('Add Secret test', async () => {
@@ -28,7 +27,6 @@ describe('Key Vault tests', () => {
2827
vaultInfo,
2928
});
3029

31-
const n = await outputPromise((rs as any).secretName);
32-
expect(n).to.equal('cache-primary');
30+
(rs as any).secretName.apply(n => expect(n).to.equal('cache-primary'));
3331
});
3432
});

0 commit comments

Comments
 (0)