mirror of
https://github.com/sasjs/server.git
synced 2026-01-08 23:10:05 +00:00
test(deploy): cover deploy route with unit test
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -3,4 +3,5 @@ dist/
|
|||||||
node_modules/
|
node_modules/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.env*
|
.env*
|
||||||
sas/
|
sas/
|
||||||
|
tmp/
|
||||||
13
jest.config.js
Normal file
13
jest.config.js
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
module.exports = {
|
||||||
|
preset: 'ts-jest',
|
||||||
|
testEnvironment: 'node',
|
||||||
|
coverageThreshold: {
|
||||||
|
global: {
|
||||||
|
branches: 80,
|
||||||
|
functions: 80,
|
||||||
|
lines: 80,
|
||||||
|
statements: -10
|
||||||
|
}
|
||||||
|
},
|
||||||
|
collectCoverageFrom: ['src/**/{!(index),}.ts']
|
||||||
|
}
|
||||||
3371
package-lock.json
generated
3371
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
12
package.json
12
package.json
@@ -2,12 +2,13 @@
|
|||||||
"name": "server",
|
"name": "server",
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"description": "SASjs server",
|
"description": "SASjs server",
|
||||||
"main": "./src/index.ts",
|
"main": "./src/server.ts",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "nodemon ./src/index.ts",
|
"start": "nodemon ./src/index.ts",
|
||||||
"build": "tsc --project ./",
|
"build": "tsc --project ./",
|
||||||
"semantic-release": "semantic-release -d",
|
"semantic-release": "semantic-release -d",
|
||||||
"postinstall": "[ -d .git ] && git config core.hooksPath ./.git-hooks || true",
|
"prepare": "[ -d .git ] && git config core.hooksPath ./.git-hooks || true",
|
||||||
|
"test": "jest --coverage",
|
||||||
"lint:fix": "npx prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"",
|
"lint:fix": "npx prettier --write \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\"",
|
||||||
"lint": "npx prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\""
|
"lint": "npx prettier --check \"src/**/*.{ts,tsx,js,jsx,html,css,sass,less,yml,md,graphql}\""
|
||||||
},
|
},
|
||||||
@@ -18,16 +19,21 @@
|
|||||||
},
|
},
|
||||||
"author": "Analytium Ltd",
|
"author": "Analytium Ltd",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@sasjs/utils": "^2.19.0",
|
"@sasjs/utils": "^2.23.3",
|
||||||
"child_process": "^1.0.2",
|
"child_process": "^1.0.2",
|
||||||
"express": "^4.17.1"
|
"express": "^4.17.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/express": "^4.17.12",
|
"@types/express": "^4.17.12",
|
||||||
|
"@types/jest": "^26.0.24",
|
||||||
"@types/node": "^15.12.2",
|
"@types/node": "^15.12.2",
|
||||||
|
"@types/supertest": "^2.0.11",
|
||||||
|
"jest": "^27.0.6",
|
||||||
"nodemon": "^2.0.7",
|
"nodemon": "^2.0.7",
|
||||||
"prettier": "^2.3.1",
|
"prettier": "^2.3.1",
|
||||||
"semantic-release": "^17.4.3",
|
"semantic-release": "^17.4.3",
|
||||||
|
"supertest": "^6.1.3",
|
||||||
|
"ts-jest": "^27.0.3",
|
||||||
"ts-node": "^10.0.0",
|
"ts-node": "^10.0.0",
|
||||||
"typescript": "^4.3.2"
|
"typescript": "^4.3.2"
|
||||||
}
|
}
|
||||||
|
|||||||
10
src/app.ts
Normal file
10
src/app.ts
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
import express from 'express'
|
||||||
|
import indexRouter from './routes'
|
||||||
|
|
||||||
|
const app = express()
|
||||||
|
|
||||||
|
app.use(express.json())
|
||||||
|
|
||||||
|
app.use('/', indexRouter)
|
||||||
|
|
||||||
|
export default app
|
||||||
97
src/routes/spec/routes.spec.ts
Normal file
97
src/routes/spec/routes.spec.ts
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
import request from 'supertest'
|
||||||
|
import app from '../../app'
|
||||||
|
import { getTreeExample } from '../../controllers/deploy'
|
||||||
|
import { getTmpFilesFolderPath } from '../../utils/file'
|
||||||
|
import { folderExists, fileExists, readFile, deleteFolder } from '@sasjs/utils'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
|
describe('deploy', () => {
|
||||||
|
const shouldFailAssertion = async (payload: any) => {
|
||||||
|
const res = await request(app).post('/deploy').send(payload)
|
||||||
|
|
||||||
|
expect(res.statusCode).toEqual(400)
|
||||||
|
expect(res.body).toEqual(getTreeExample())
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should respond with payload example if valid payload was not provided', async () => {
|
||||||
|
await shouldFailAssertion(null)
|
||||||
|
await shouldFailAssertion(undefined)
|
||||||
|
await shouldFailAssertion('data')
|
||||||
|
await shouldFailAssertion({})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
userId: 1,
|
||||||
|
title: 'test is cool'
|
||||||
|
})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
membersWRONG: []
|
||||||
|
})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
members: {}
|
||||||
|
})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
members: [
|
||||||
|
{
|
||||||
|
nameWRONG: 'jobs',
|
||||||
|
type: 'folder',
|
||||||
|
members: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
members: [
|
||||||
|
{
|
||||||
|
name: 'jobs',
|
||||||
|
type: 'WRONG',
|
||||||
|
members: []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
await shouldFailAssertion({
|
||||||
|
members: [
|
||||||
|
{
|
||||||
|
name: 'jobs',
|
||||||
|
type: 'folder',
|
||||||
|
members: [
|
||||||
|
{
|
||||||
|
name: 'extract',
|
||||||
|
type: 'folder',
|
||||||
|
members: [
|
||||||
|
{
|
||||||
|
name: 'makedata1',
|
||||||
|
type: 'service',
|
||||||
|
codeWRONG: '%put Hello World!;'
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should respond with payload example if valid payload was not provided', async () => {
|
||||||
|
const res = await request(app)
|
||||||
|
.post('/deploy')
|
||||||
|
.send(getTreeExample().supportedFormat)
|
||||||
|
|
||||||
|
expect(res.statusCode).toEqual(200)
|
||||||
|
expect(res.text).toEqual('Files deployed successfully to @sasjs/server.')
|
||||||
|
await expect(folderExists(getTmpFilesFolderPath())).resolves.toEqual(true)
|
||||||
|
|
||||||
|
const testJobFolder = path.join(getTmpFilesFolderPath(), 'jobs', 'extract')
|
||||||
|
await expect(folderExists(testJobFolder)).resolves.toEqual(true)
|
||||||
|
|
||||||
|
const testJobFile = path.join(
|
||||||
|
testJobFolder,
|
||||||
|
getTreeExample().supportedFormat.members[0].members[0].members[0].name
|
||||||
|
)
|
||||||
|
|
||||||
|
await expect(fileExists(testJobFile)).resolves.toEqual(true)
|
||||||
|
|
||||||
|
await expect(readFile(testJobFile)).resolves.toEqual(
|
||||||
|
getTreeExample().supportedFormat.members[0].members[0].members[0].code
|
||||||
|
)
|
||||||
|
|
||||||
|
await deleteFolder(getTmpFilesFolderPath())
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -1,9 +1,4 @@
|
|||||||
import express from 'express'
|
import app from './app'
|
||||||
import indexRouter from './routes'
|
|
||||||
|
|
||||||
const app = express()
|
|
||||||
|
|
||||||
app.use('/', indexRouter)
|
|
||||||
|
|
||||||
const port = 5000
|
const port = 5000
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
Reference in New Issue
Block a user