Skip to content

Commit

Permalink
Add integration test (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LinLzis authored Jul 3, 2020
1 parent d859fb6 commit 2869a95
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"access": "public"
},
"scripts": {
"test": "npm run lint && npm run prettier",
"int-test": "jest ./tests/integration.test.js --testEnvironment node",
"test": "npm run lint && npm run prettier && npm run int-test",
"commitlint": "commitlint -f HEAD@{15}",
"lint": "eslint --ext .js,.ts,.tsx .",
"lint:fix": "eslint --fix --ext .js,.ts,.tsx .",
Expand Down Expand Up @@ -44,13 +45,16 @@
"@semantic-release/git": "^9.0.0",
"@semantic-release/npm": "^7.0.4",
"@semantic-release/release-notes-generator": "^9.0.1",
"@serverless/platform-client-china": "^1.0.19",
"axios": "^0.19.2",
"babel-eslint": "^10.1.0",
"dotenv": "^8.2.0",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-prettier": "^3.1.2",
"husky": "^4.2.3",
"jest": "^25.0.1",
"lint-staged": "^10.0.8",
"prettier": "^1.19.1",
"semantic-release": "^17.0.4"
Expand Down
2 changes: 1 addition & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ const prepareInputs = async (instance, credentials, inputs = {}) => {

// 对apigw inputs进行标准化
const apigatewayConf = inputs.apigatewayConf ? inputs.apigatewayConf : {}
apigatewayConf.isDisabled = inputs.apigatewayConf === true
apigatewayConf.isDisabled = apigatewayConf.isDisabled === true
apigatewayConf.fromClientRemark = fromClientRemark
apigatewayConf.serviceName = inputs.serviceName
apigatewayConf.description = `Serverless Framework Tencent-${capitalString(
Expand Down
62 changes: 62 additions & 0 deletions tests/integration.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { generateId, getServerlessSdk } = require('./utils')
const execSync = require('child_process').execSync
const path = require('path')
const axios = require('axios')

// set enough timeout for deployment to finish
jest.setTimeout(300000)

// the yaml file we're testing against
const instanceYaml = {
org: 'orgDemo',
app: 'appDemo',
component: 'koa@dev',
name: `koa-integration-tests-${generateId()}`,
stage: 'dev',
inputs: {
region: 'ap-guangzhou',
runtime: 'Nodejs8.9',
apigatewayConf: { environment: 'test' }
}
}

// get credentials from process.env but need to init empty credentials object
const credentials = {
tencent: {}
}

// get serverless construct sdk
const sdk = getServerlessSdk(instanceYaml.org)

it('should successfully deploy koa app', async () => {
const instance = await sdk.deploy(instanceYaml, { tencent: {} })
expect(instance).toBeDefined()
expect(instance.instanceName).toEqual(instanceYaml.name)
// get src from template by default
expect(instance.outputs.templateUrl).toBeDefined()
expect(instance.outputs.region).toEqual(instanceYaml.inputs.region)
expect(instance.outputs.scf).toBeDefined()
expect(instance.outputs.scf.runtime).toEqual(instanceYaml.inputs.runtime)
expect(instance.outputs.apigw).toBeDefined()
expect(instance.outputs.apigw.environment).toEqual(instanceYaml.inputs.apigatewayConf.environment)
})

it('should successfully update source code', async () => {
// change source to own source './src' and need to install packages before deploy
const srcPath = path.join(__dirname, 'src')
execSync('npm install', { cwd: srcPath })
instanceYaml.inputs.src = srcPath

const instance = await sdk.deploy(instanceYaml, credentials)
const response = await axios.get(instance.outputs.apigw.url)

expect(response.data).toEqual('Hello World')
expect(instance.outputs.templateUrl).not.toBeDefined()
})

it('should successfully remove koa app', async () => {
await sdk.remove(instanceYaml, credentials)
result = await sdk.getInstance(instanceYaml.org, instanceYaml.stage, instanceYaml.app, instanceYaml.name)

expect(result.instance.instanceStatus).toEqual('inactive')
})
14 changes: 14 additions & 0 deletions tests/src/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "koa-demo",
"version": "1.0.0",
"description": "",
"main": "sls.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "yugasun",
"license": "MIT",
"dependencies": {
"koa": "^2.11.0"
}
}
10 changes: 10 additions & 0 deletions tests/src/sls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const Koa = require('koa')
const app = new Koa()

app.use(async (ctx) => {
ctx.body = 'Hello World'
})

app.listen(3000)

module.exports = app
24 changes: 24 additions & 0 deletions tests/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { ServerlessSDK } = require('@serverless/platform-client-china')

/*
* Generate random id
*/
const generateId = () =>
Math.random()
.toString(36)
.substring(6)

/*
* Initializes and returns an instance of the serverless sdk
* @param ${string} orgName - the serverless org name.
*/
const getServerlessSdk = (orgName) => {
const sdk = new ServerlessSDK({
context: {
orgName
}
})
return sdk
}

module.exports = { generateId, getServerlessSdk }

0 comments on commit 2869a95

Please sign in to comment.