Skip to content

Commit 2869a95

Browse files
authored
Add integration test (#34)
* chore(test): add integration test * chore: update jest version to support node8.x * chore: remove process.env variables of test * chore: update test case * chore: update test case
1 parent d859fb6 commit 2869a95

File tree

6 files changed

+116
-2
lines changed

6 files changed

+116
-2
lines changed

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"access": "public"
77
},
88
"scripts": {
9-
"test": "npm run lint && npm run prettier",
9+
"int-test": "jest ./tests/integration.test.js --testEnvironment node",
10+
"test": "npm run lint && npm run prettier && npm run int-test",
1011
"commitlint": "commitlint -f HEAD@{15}",
1112
"lint": "eslint --ext .js,.ts,.tsx .",
1213
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
@@ -44,13 +45,16 @@
4445
"@semantic-release/git": "^9.0.0",
4546
"@semantic-release/npm": "^7.0.4",
4647
"@semantic-release/release-notes-generator": "^9.0.1",
48+
"@serverless/platform-client-china": "^1.0.19",
49+
"axios": "^0.19.2",
4750
"babel-eslint": "^10.1.0",
4851
"dotenv": "^8.2.0",
4952
"eslint": "^6.8.0",
5053
"eslint-config-prettier": "^6.10.0",
5154
"eslint-plugin-import": "^2.20.1",
5255
"eslint-plugin-prettier": "^3.1.2",
5356
"husky": "^4.2.3",
57+
"jest": "^25.0.1",
5458
"lint-staged": "^10.0.8",
5559
"prettier": "^1.19.1",
5660
"semantic-release": "^17.0.4"

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {
250250

251251
// 对apigw inputs进行标准化
252252
const apigatewayConf = inputs.apigatewayConf ? inputs.apigatewayConf : {}
253-
apigatewayConf.isDisabled = inputs.apigatewayConf === true
253+
apigatewayConf.isDisabled = apigatewayConf.isDisabled === true
254254
apigatewayConf.fromClientRemark = fromClientRemark
255255
apigatewayConf.serviceName = inputs.serviceName
256256
apigatewayConf.description = `Serverless Framework Tencent-${capitalString(

tests/integration.test.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { generateId, getServerlessSdk } = require('./utils')
2+
const execSync = require('child_process').execSync
3+
const path = require('path')
4+
const axios = require('axios')
5+
6+
// set enough timeout for deployment to finish
7+
jest.setTimeout(300000)
8+
9+
// the yaml file we're testing against
10+
const instanceYaml = {
11+
org: 'orgDemo',
12+
app: 'appDemo',
13+
component: 'koa@dev',
14+
name: `koa-integration-tests-${generateId()}`,
15+
stage: 'dev',
16+
inputs: {
17+
region: 'ap-guangzhou',
18+
runtime: 'Nodejs8.9',
19+
apigatewayConf: { environment: 'test' }
20+
}
21+
}
22+
23+
// get credentials from process.env but need to init empty credentials object
24+
const credentials = {
25+
tencent: {}
26+
}
27+
28+
// get serverless construct sdk
29+
const sdk = getServerlessSdk(instanceYaml.org)
30+
31+
it('should successfully deploy koa app', async () => {
32+
const instance = await sdk.deploy(instanceYaml, { tencent: {} })
33+
expect(instance).toBeDefined()
34+
expect(instance.instanceName).toEqual(instanceYaml.name)
35+
// get src from template by default
36+
expect(instance.outputs.templateUrl).toBeDefined()
37+
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
38+
expect(instance.outputs.scf).toBeDefined()
39+
expect(instance.outputs.scf.runtime).toEqual(instanceYaml.inputs.runtime)
40+
expect(instance.outputs.apigw).toBeDefined()
41+
expect(instance.outputs.apigw.environment).toEqual(instanceYaml.inputs.apigatewayConf.environment)
42+
})
43+
44+
it('should successfully update source code', async () => {
45+
// change source to own source './src' and need to install packages before deploy
46+
const srcPath = path.join(__dirname, 'src')
47+
execSync('npm install', { cwd: srcPath })
48+
instanceYaml.inputs.src = srcPath
49+
50+
const instance = await sdk.deploy(instanceYaml, credentials)
51+
const response = await axios.get(instance.outputs.apigw.url)
52+
53+
expect(response.data).toEqual('Hello World')
54+
expect(instance.outputs.templateUrl).not.toBeDefined()
55+
})
56+
57+
it('should successfully remove koa app', async () => {
58+
await sdk.remove(instanceYaml, credentials)
59+
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)
60+
61+
expect(result.instance.instanceStatus).toEqual('inactive')
62+
})

tests/src/package.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "koa-demo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "sls.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "yugasun",
10+
"license": "MIT",
11+
"dependencies": {
12+
"koa": "^2.11.0"
13+
}
14+
}

tests/src/sls.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const Koa = require('koa')
2+
const app = new Koa()
3+
4+
app.use(async (ctx) => {
5+
ctx.body = 'Hello World'
6+
})
7+
8+
app.listen(3000)
9+
10+
module.exports = app

tests/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { ServerlessSDK } = require('@serverless/platform-client-china')
2+
3+
/*
4+
* Generate random id
5+
*/
6+
const generateId = () =>
7+
Math.random()
8+
.toString(36)
9+
.substring(6)
10+
11+
/*
12+
* Initializes and returns an instance of the serverless sdk
13+
* @param ${string} orgName - the serverless org name.
14+
*/
15+
const getServerlessSdk = (orgName) => {
16+
const sdk = new ServerlessSDK({
17+
context: {
18+
orgName
19+
}
20+
})
21+
return sdk
22+
}
23+
24+
module.exports = { generateId, getServerlessSdk }

0 commit comments

Comments
 (0)