Skip to content

Commit 2ee79cf

Browse files
authored
test: refactor with typescript (#181)
* fix: fix some typings * refactor(ts): all test are now in typescript 🎉 * test: add babel.config.js for jest running typescript test * fix: fix build error
1 parent 07498a6 commit 2ee79cf

35 files changed

+186
-138
lines changed

__tests__/apigw.test.js renamed to __tests__/apigw.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Apigw } = require('../lib');
1+
import { Apigw } from '../src';
22

33
const deepClone = (obj) => {
44
return JSON.parse(JSON.stringify(obj));

__tests__/cam.test.js renamed to __tests__/cam.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Cam } = require('../lib');
1+
import { Cam } from '../src';
22

33
describe('Cam', () => {
44
const credentials = {
@@ -23,13 +23,13 @@ describe('Cam', () => {
2323
test('should create role success', async () => {
2424
await cam.CreateRole(roleName, policy);
2525
const { RoleInfo } = await cam.GetRole(roleName);
26-
const exist = await cam.isRoleExist(roleName, {});
26+
const exist = await cam.isRoleExist(roleName);
2727
expect(RoleInfo.RoleName).toBe(roleName);
2828
expect(exist).toBe(true);
2929
});
3030

3131
test('should delete role success', async () => {
32-
await cam.DeleteRole(roleName, {});
32+
await cam.DeleteRole(roleName);
3333
try {
3434
await cam.GetRole(roleName);
3535
} catch (e) {

__tests__/cdn.test.js renamed to __tests__/cdn.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
const { Cdn } = require('../lib');
2-
const { getCdnByDomain } = require('../lib/modules/cdn/utils');
1+
import { CdnDeployInputs } from './../src/modules/cdn/interface';
2+
import { Cdn } from '../src';
3+
import { getCdnByDomain } from '../src/modules/cdn/utils';
34

45
describe('Cdn', () => {
56
jest.setTimeout(600000);
67
const credentials = {
78
SecretId: process.env.TENCENT_SECRET_ID,
89
SecretKey: process.env.TENCENT_SECRET_KEY,
910
};
10-
const inputs = {
11+
const inputs: CdnDeployInputs = {
1112
async: false,
1213
area: 'overseas',
1314
domain: process.env.SUB_DOMAIN,
@@ -32,7 +33,7 @@ describe('Cdn', () => {
3233
redirectStatusCode: 301,
3334
},
3435
};
35-
const cdn = new Cdn(credentials, process.env.REGION);
36+
const cdn = new Cdn(credentials);
3637

3738
test('should deploy CDN success with originType = cos', async () => {
3839
const res = await cdn.deploy(inputs);

__tests__/cfs.test.js renamed to __tests__/cfs.test.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
const { sleep } = require('@ygkit/request');
2-
const { Cfs } = require('../lib');
3-
const utils = require('../lib/modules/cfs/utils').default;
1+
import { CFSDeployInputs } from './../src/modules/cfs/interface';
2+
import { sleep } from '@ygkit/request';
3+
import { Cfs } from '../src';
4+
import utils from '../src/modules/cfs/utils';
45

56
describe('Cfs', () => {
67
const credentials = {
78
SecretId: process.env.TENCENT_SECRET_ID,
89
SecretKey: process.env.TENCENT_SECRET_KEY,
910
};
1011

11-
const inputs = {
12+
let fsId: string;
13+
14+
const inputs: CFSDeployInputs = {
1215
fsName: 'cfs-test',
1316
region: 'ap-guangzhou',
1417
zone: 'ap-guangzhou-3',
@@ -38,12 +41,15 @@ describe('Cfs', () => {
3841
fileSystemId: expect.stringContaining('cfs-'),
3942
tags: inputs.tags,
4043
});
41-
inputs.fileSystemId = res.fileSystemId;
44+
fsId = res.fileSystemId;
4245
});
4346

4447
test('should remove CFS success', async () => {
4548
await sleep(1000);
46-
const res = await cfs.remove(inputs);
49+
const res = await cfs.remove({
50+
fsName: inputs.fsName,
51+
fileSystemId: fsId,
52+
});
4753
const detail = await utils.getCfs(cfs.capi, inputs.fileSystemId);
4854
expect(res).toEqual({});
4955
expect(detail).toBeUndefined();

__tests__/cls.test.js renamed to __tests__/cls.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { Cls } = require('../lib');
2-
const { sleep } = require('@ygkit/request');
1+
import { ClsDeployInputs, ClsDeployOutputs } from './../src/modules/cls/interface';
2+
import { Cls } from '../src';
3+
import { sleep } from '@ygkit/request';
34

45
describe('Cls', () => {
56
const credentials = {
@@ -8,9 +9,9 @@ describe('Cls', () => {
89
};
910
const client = new Cls(credentials, process.env.REGION);
1011

11-
let outputs = {};
12+
let outputs: ClsDeployOutputs;
1213

13-
const inputs = {
14+
const inputs: ClsDeployInputs = {
1415
region: 'ap-guangzhou',
1516
name: 'cls-test',
1617
topic: 'cls-topic-test',

__tests__/cns.test.js renamed to __tests__/cns.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
const { Cns } = require('../lib');
1+
import { CnsDeployInputs, CnsDeployOutputs } from './../src/modules/cns/interface';
2+
import { Cns } from '../src';
23

34
describe('Cns', () => {
45
const credentials = {
56
SecretId: process.env.TENCENT_SECRET_ID,
67
SecretKey: process.env.TENCENT_SECRET_KEY,
78
};
8-
const inputs = {
9+
const inputs: CnsDeployInputs = {
910
domain: process.env.DOMAIN,
1011
records: [
1112
{
@@ -30,7 +31,7 @@ describe('Cns', () => {
3031
};
3132
const cns = new Cns(credentials, process.env.REGION);
3233

33-
let recordList;
34+
let recordList: CnsDeployOutputs;
3435
test('should deploy Cns success', async () => {
3536
recordList = await cns.deploy(inputs);
3637
expect(recordList).toEqual({

__tests__/cos.test.js renamed to __tests__/cos.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
const { Cos } = require('../lib');
2-
const path = require('path');
3-
const axios = require('axios');
4-
const { sleep } = require('@ygkit/request');
1+
import { CosDeployInputs, CosWebsiteInputs } from './../src/modules/cos/interface';
2+
import { Cos } from '../src';
3+
import path from 'path';
4+
import axios from 'axios';
5+
import { sleep } from '@ygkit/request';
56

67
describe('Cos', () => {
78
const credentials = {
@@ -28,7 +29,7 @@ describe('Cos', () => {
2829
],
2930
version: '2.0',
3031
};
31-
const inputs = {
32+
const inputs: CosDeployInputs = {
3233
bucket: bucket,
3334
src: staticPath,
3435
force: true,
@@ -52,7 +53,7 @@ describe('Cos', () => {
5253
],
5354
};
5455

55-
const websiteInputs = {
56+
const websiteInputs: CosWebsiteInputs = {
5657
code: {
5758
src: staticPath,
5859
index: 'index.html',

__tests__/cynosdb.test.js renamed to __tests__/cynosdb.test.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
const { Cynosdb } = require('../lib');
2-
const {
3-
getClusterDetail,
4-
sleep,
5-
generatePwd,
6-
isValidPwd,
7-
} = require('../lib/modules/cynosdb/utils');
1+
import { CynosdbDeployInputs } from './../src/modules/cynosdb/interface';
2+
import { Cynosdb } from '../src';
3+
import { getClusterDetail, sleep, generatePwd, isValidPwd } from '../src/modules/cynosdb/utils';
84

95
describe('Cynosdb', () => {
106
jest.setTimeout(600000);
@@ -15,7 +11,7 @@ describe('Cynosdb', () => {
1511
const region = 'ap-shanghai';
1612
const client = new Cynosdb(credentials, region);
1713

18-
const inputs = {
14+
const inputs: CynosdbDeployInputs = {
1915
region,
2016
zone: 'ap-shanghai-2',
2117
vpcConfig: {

__tests__/domain.test.js renamed to __tests__/domain.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Domain } = require('../lib');
1+
import { Domain } from '../src';
22

33
describe('Domain', () => {
44
const credentials = {

__tests__/error.test.js renamed to __tests__/error.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { ApiTypeError, ApiError } = require('../lib/utils/error');
1+
import { ApiTypeError, ApiError } from '../src/utils/error';
22

33
describe('Custom Error', () => {
44
test('TypeError', async () => {

__tests__/layer.test.js renamed to __tests__/layer.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { sleep } = require('@ygkit/request');
2-
const { Layer } = require('../lib');
1+
import { LayerDeployInputs } from './../src/modules/layer/interface';
2+
import { sleep } from '@ygkit/request';
3+
import { Layer } from '../src';
34

45
describe('Layer', () => {
56
const credentials = {
@@ -8,7 +9,7 @@ describe('Layer', () => {
89
};
910
const layer = new Layer(credentials, process.env.REGION);
1011

11-
const inputs = {
12+
const inputs: LayerDeployInputs = {
1213
region: 'ap-guangzhou',
1314
name: 'layer-test',
1415
bucket: process.env.BUCKET,

__tests__/metrics.test.js renamed to __tests__/metrics.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { Metrics } = require('../lib');
1+
import { Metrics } from '../src';
22

33
describe('Metrics', () => {
44
const credentials = {

__tests__/pg.test.js renamed to __tests__/pg.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const { Postgresql } = require('../lib');
2-
const { getDbInstanceDetail } = require('../lib/modules/postgresql/utils');
3-
const { sleep } = require('@ygkit/request');
1+
import { PostgresqlDeployInputs } from './../src/modules/postgresql/interface';
2+
import { Postgresql } from '../src';
3+
import { getDbInstanceDetail } from '../src/modules/postgresql/utils';
4+
import { sleep } from '@ygkit/request';
45

56
describe('Postgresql', () => {
67
const credentials = {
@@ -9,7 +10,7 @@ describe('Postgresql', () => {
910
};
1011
const pg = new Postgresql(credentials, process.env.REGION);
1112

12-
const inputs = {
13+
const inputs: PostgresqlDeployInputs = {
1314
region: process.env.REGION,
1415
zone: process.env.ZONE,
1516
dBInstanceName: 'serverless-test',

__tests__/scf.test.js renamed to __tests__/scf.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { sleep } = require('@ygkit/request');
2-
const { Scf, Cfs, Layer } = require('../lib');
1+
import { ScfDeployInputs } from './../src/modules/scf/interface';
2+
import { sleep } from '@ygkit/request';
3+
import { Scf, Cfs, Layer } from '../src';
34

45
describe('Scf', () => {
56
const credentials = {
@@ -72,7 +73,7 @@ describe('Scf', () => {
7273
},
7374
};
7475

75-
const inputs = {
76+
const inputs: ScfDeployInputs = {
7677
name: `serverless-test-${Date.now()}`,
7778
code: {
7879
bucket: process.env.BUCKET,

__tests__/tag.test.js renamed to __tests__/tag.test.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { Tag } = require('../lib');
1+
import { TagDeployInputs } from './../src/modules/tag/interface';
2+
import { Tag } from '../src';
3+
import { ApiServiceType } from '../src/modules/interface';
24

35
describe('Tag', () => {
46
const credentials = {
@@ -7,15 +9,15 @@ describe('Tag', () => {
79
};
810
const functionName = 'serverless-unit-test';
911
const tagItem = { TagKey: 'slstest', TagValue: 'slstest' };
10-
const commonInputs = {
12+
const commonInputs: TagDeployInputs = {
1113
resourceIds: [`default/function/${functionName}`],
1214
resourcePrefix: 'namespace',
13-
serviceType: 'scf',
15+
serviceType: ApiServiceType.scf,
1416
};
1517
const tag = new Tag(credentials, process.env.REGION);
1618

1719
test('attach tags', async () => {
18-
delete commonInputs.addTags;
20+
// delete commonInputs.addTags;
1921
commonInputs.attachTags = [tagItem];
2022

2123
const res = await tag.deploy(commonInputs);
@@ -30,7 +32,7 @@ describe('Tag', () => {
3032
});
3133

3234
test('detach tags', async () => {
33-
delete commonInputs.addTags;
35+
// delete commonInputs.addTags;
3436
delete commonInputs.attachTags;
3537
commonInputs.detachTags = [tagItem];
3638

__tests__/triggers/cls.test.js renamed to __tests__/triggers/cls.test.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const { Cls, Scf } = require('../../lib');
2-
const ClsTrigger = require('../../lib/modules/triggers/cls').default;
3-
const { sleep } = require('@ygkit/request');
1+
import { ClsTriggerInputsParams } from './../../src/modules/triggers/interface';
2+
import { Cls, Scf } from '../../src';
3+
import ClsTrigger from '../../src/modules/triggers/cls';
4+
import { sleep } from '@ygkit/request';
45

56
describe('Cls', () => {
67
const credentials = {
@@ -11,7 +12,7 @@ describe('Cls', () => {
1112
const cls = new Cls(credentials, process.env.REGION);
1213
const scf = new Scf(credentials, process.env.REGION);
1314

14-
const data = {
15+
const data: ClsTriggerInputsParams = {
1516
qualifier: '$DEFAULT',
1617
maxWait: 60,
1718
maxSize: 100,

__tests__/triggers/mps.test.js renamed to __tests__/triggers/mps.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
const { Scf } = require('../../lib');
2-
const MpsTrigger = require('../../lib/modules/triggers/mps').default;
1+
import { MpsTriggerInputsParams } from './../../src/modules/triggers/interface';
2+
import { Scf } from '../../src';
3+
import MpsTrigger from '../../src/modules/triggers/mps';
34

45
// FIXME: all mps trigger bind fail
56
describe('Mps', () => {
@@ -10,7 +11,7 @@ describe('Mps', () => {
1011
const client = new MpsTrigger({ credentials, region: process.env.REGION });
1112
const scfClient = new Scf(credentials, process.env.REGION);
1213

13-
const data = {
14+
const data: MpsTriggerInputsParams = {
1415
qualifier: '$DEFAULT',
1516
type: 'EditMediaTask',
1617
};

__tests__/vpc.test.js renamed to __tests__/vpc.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
const { Vpc } = require('../lib');
2-
const vpcUtils = require('../lib/modules/vpc/utils').default;
1+
import { VpcDeployInputs } from './../src/modules/vpc/interface';
2+
import { Vpc } from '../src';
3+
import vpcUtils from '../src/modules/vpc/utils';
34

45
describe('Vpc', () => {
56
const credentials = {
67
SecretId: process.env.TENCENT_SECRET_ID,
78
SecretKey: process.env.TENCENT_SECRET_KEY,
89
};
9-
const inputs = {
10+
const inputs: VpcDeployInputs = {
1011
region: process.env.REGION,
1112
zone: process.env.ZONE,
1213
vpcName: 'serverless-test',
@@ -38,7 +39,10 @@ describe('Vpc', () => {
3839

3940
test('should success remove a vpc', async () => {
4041
if (inputs.vpcId) {
41-
await vpc.remove(inputs);
42+
await vpc.remove({
43+
vpcId: inputs.vpcId,
44+
subnetId: inputs.subnetId,
45+
});
4246
const vpcDetail = await vpcUtils.getVpcDetail(vpc.capi, inputs.vpcId);
4347
const subnetDetail = await vpcUtils.getSubnetDetail(vpc.capi, inputs.subnetId);
4448

babel.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// babel.config.js
2+
module.exports = {
3+
presets: [['@babel/preset-env', { targets: { node: 'current' } }], '@babel/preset-typescript'],
4+
};

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
},
5858
"homepage": "https://github.com/serverless-tencent/tencent-component-toolkit#readme",
5959
"devDependencies": {
60+
"@babel/preset-env": "^7.12.11",
61+
"@babel/preset-typescript": "^7.12.7",
6062
"@commitlint/cli": "^8.3.5",
6163
"@commitlint/config-conventional": "^8.3.4",
6264
"@semantic-release/changelog": "^5.0.0",
@@ -83,6 +85,7 @@
8385
"dependencies": {
8486
"@tencent-sdk/capi": "^1.1.8",
8587
"@tencent-sdk/cls": "^0.1.7",
88+
"@types/jest": "^26.0.20",
8689
"@types/lodash": "^4.14.167",
8790
"@types/node": "^14.14.20",
8891
"@ygkit/request": "^0.1.8",

0 commit comments

Comments
 (0)