Skip to content

Commit b23b26f

Browse files
committed
test: add integration test
1 parent ae8c1db commit b23b26f

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ dist
33
node_modules
44
CHANGELOG.md
55
*.test.js
6+
.nuxt

package.json

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

tests/integration.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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(600000)
8+
9+
// the yaml file we're testing against
10+
const instanceYaml = {
11+
org: 'orgDemo',
12+
app: 'appDemo',
13+
component: 'nuxtjs',
14+
name: `nuxtjs-integration-tests-${generateId()}`,
15+
stage: 'dev',
16+
inputs: {
17+
region: 'ap-guangzhou',
18+
runtime: 'Nodejs10.15',
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 nuxtjs app', async () => {
32+
const instance = await sdk.deploy(instanceYaml, { tencent: {} })
33+
34+
expect(instance).toBeDefined()
35+
expect(instance.instanceName).toEqual(instanceYaml.name)
36+
expect(instance.outputs).toBeDefined()
37+
// get src from template by default
38+
expect(instance.outputs.templateUrl).toBeDefined()
39+
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
40+
expect(instance.outputs.scf).toBeDefined()
41+
expect(instance.outputs.scf.runtime).toEqual(instanceYaml.inputs.runtime)
42+
expect(instance.outputs.apigw).toBeDefined()
43+
expect(instance.outputs.apigw.environment).toEqual(instanceYaml.inputs.apigatewayConf.environment)
44+
45+
const response = await axios.get(instance.outputs.apigw.url)
46+
expect(response.data.includes('Nuxt.js')).toBeTruthy()
47+
})
48+
49+
it('should successfully remove nuxtjs app', async () => {
50+
await sdk.remove(instanceYaml, credentials)
51+
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)
52+
53+
expect(result.instance.instanceStatus).toEqual('inactive')
54+
})

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)