Skip to content

Commit 98b1ea9

Browse files
committed
feature(test): add command tests
- added unit and integration tests for the init, launch and serve commands - refactored the init command codebase to make them easier to test and debug - moved flags and questions(prompts) config now live to separate files outside the command classes - added config to query-engine flag to prevent invalid engines - added config to storage flag to prevent an invalid string - configured proper test environment to prevent overwriting the user's config file
1 parent be0f20c commit 98b1ea9

23 files changed

+3029
-799
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ yalc.lock
1414
/src/plugins
1515
/cg
1616
/dgraph
17-
.DS_Store
17+
.DS_Store
18+
/coverage
19+
/test/.testConfigDir
20+
/test/.testData

jest.config.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
collectCoverage: true,
3+
collectCoverageFrom: ['src/**/*.ts'],
4+
coveragePathIgnorePatterns: [
5+
'/templates/',
6+
'/plugins/',
7+
'/examples/',
8+
'/docs/',
9+
'/coverage/',
10+
'/bin/',
11+
],
12+
coverageReporters: ['lcov', 'text-summary'],
13+
coverageThreshold: {
14+
global: {
15+
branches: 80,
16+
functions: 80,
17+
lines: 80,
18+
statements: 80,
19+
},
20+
},
21+
moduleFileExtensions: ['ts', 'js', 'json'],
22+
testEnvironment: 'node',
23+
testMatch: ['<rootDir>/test/**/*.test.ts'],
24+
transform: { '\\.ts$': 'ts-jest' },
25+
}

package.json

+9-7
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"devDependencies": {
4141
"@autocloud/eslint-config": "^0.1.0",
4242
"@oclif/dev-cli": "^1",
43-
"@oclif/test": "^1",
43+
"@oclif/test": "^1.2.8",
4444
"@semantic-release/changelog": "^5.0.1",
4545
"@semantic-release/git": "^9.0.0",
4646
"@semantic-release/gitlab": "^6.2.2",
@@ -49,12 +49,12 @@
4949
"@types/cli-table": "^0.3.0",
5050
"@types/express": "^4.17.13",
5151
"@types/inquirer": "^7.3.2",
52-
"@types/mocha": "^5",
52+
"@types/jest": "^27.0.1",
5353
"@types/node": "^14",
5454
"@types/pino": "^6.3.8",
5555
"@typescript-eslint/eslint-plugin": "^4.28.5",
5656
"@typescript-eslint/parser": "^4.28.5",
57-
"chai": "^4",
57+
"chai": "^4.3.4",
5858
"eslint-config-airbnb-base": "14.2.1",
5959
"eslint-config-oclif": "^3.1",
6060
"eslint-config-oclif-typescript": "^0.1",
@@ -63,11 +63,12 @@
6363
"eslint-plugin-prettier": "^3.4.0",
6464
"globby": "^10",
6565
"husky": "^4.3.0",
66+
"jest": "^27.1.0",
67+
"jest-diff": "^27.1.0",
6668
"lint-staged": "^11.1.1",
67-
"mocha": "^5",
68-
"nyc": "^14",
6969
"semantic-release": "^17.4.4",
70-
"ts-node": "^8",
70+
"ts-jest": "^27.0.5",
71+
"ts-node": "^10.2.1",
7172
"typescript": "^4.3.5"
7273
},
7374
"engines": {
@@ -106,7 +107,8 @@
106107
"run:scan": "NODE_ENV=development ./bin/run scan",
107108
"run:scan:aws": "NODE_ENV=development ./bin/run scan aws",
108109
"run:launch": "NODE_ENV=development ./bin/run launch",
109-
"test": "nyc --extension .ts mocha --forbid-only \"test/**/*.test.ts\"",
110+
"test": "CG_DEBUG=-1 NODE_ENV=test jest --coverage=true --detectOpenHandles --runInBand --forceExit",
111+
"test:debug": "CG_DEBUG=5 NODE_ENV=test jest --coverage=false --detectOpenHandles --runInBand --forceExit",
110112
"version": "oclif-dev readme && git add README.md",
111113
"lint": "eslint --config .eslintrc.json --ext .js,.ts ./",
112114
"lint:fix": "eslint --fix --config .eslintrc.json --ext .js,.ts ./"

src/commands/base.ts

+20-54
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
1-
import Command, { flags } from '@oclif/command'
1+
import Command from '@oclif/command'
22
import { Input } from '@oclif/parser'
33
import CloudGraph, { Logger } from '@cloudgraph/sdk'
44
import { cosmiconfigSync } from 'cosmiconfig'
5-
import path from 'path'
6-
import inquirer from 'inquirer'
75
import chalk from 'chalk'
6+
import inquirer from 'inquirer'
7+
import path from 'path'
88
import gt from 'semver/functions/gt'
99
import Manager from '../manager'
1010
import EngineMap from '../storage'
1111
import QueryEngine from '../server'
1212
import { StorageEngine, StorageEngineConnectionConfig } from '../storage/types'
13-
import { getStorageEngineConnectionConfig, printWelcomeMessage, printBoxMessage } from '../utils'
13+
import {
14+
getDefaultEndpoint,
15+
getDefaultStorageEngineConnectionConfig,
16+
getStorageEngineConnectionConfig,
17+
printWelcomeMessage,
18+
printBoxMessage
19+
} from '../utils'
20+
import flagsDefinition from '../utils/flags'
1421
import openBrowser from '../utils/open'
15-
import { DEFAULT_CONFIG } from '../utils/constants'
1622

1723
export default abstract class BaseCommand extends Command {
1824
constructor(argv: any, config: any) {
@@ -37,49 +43,7 @@ export default abstract class BaseCommand extends Command {
3743

3844
storedConfig: { [key: string]: any } | undefined
3945

40-
static flags = {
41-
// devMode flag
42-
dev: flags.boolean({ description: 'Turn on developer mode' }),
43-
// dgraph host
44-
dgraph: flags.string({
45-
char: 'd',
46-
env: 'CG_HOST_PORT',
47-
description: 'Set where dgraph is running (default localhost:8997)',
48-
}),
49-
// storage engine to use
50-
storage: flags.string({
51-
char: 's',
52-
description:
53-
'Select a storage engine to use. Currently only supports Dgraph',
54-
}),
55-
// dir to store cloud graph data versions in
56-
directory: flags.string({
57-
description:
58-
'Set the folder where CloudGraph will store data. (default cg)',
59-
}),
60-
// serve query engine after scan/load
61-
'no-serve': flags.boolean({
62-
default: false,
63-
description: 'Set to not serve a query engine',
64-
}),
65-
// port for query engine
66-
port: flags.integer({
67-
char: 'p',
68-
env: 'CG_QUERY_PORT',
69-
description: 'Set port to serve query engine',
70-
}),
71-
// Query Engine to use
72-
'query-engine': flags.string({
73-
char: 'q',
74-
description: 'Query engine to launch',
75-
}),
76-
// version limit
77-
'version-limit': flags.string({
78-
char: 'l',
79-
description:
80-
'Limit the amount of version folders stored on the filesystem (default 10)',
81-
}),
82-
}
46+
static flags = flagsDefinition
8347

8448
static hidden = true
8549

@@ -205,12 +169,10 @@ Run ${chalk.italic.green('npm i -g @cloudgraph/cli')} to install`)
205169
// nothing found, return default location
206170

207171
showInitialStatus &&
208-
this.logger.info(
209-
`Dgraph host set as: ${DEFAULT_CONFIG.scheme}://${DEFAULT_CONFIG.host}:${DEFAULT_CONFIG.port}`
210-
)
211-
return DEFAULT_CONFIG
172+
this.logger.info(`Dgraph host set as: ${getDefaultEndpoint()}`)
173+
return getDefaultStorageEngineConnectionConfig()
212174
}
213-
return DEFAULT_CONFIG
175+
return getDefaultStorageEngineConnectionConfig()
214176
}
215177

216178
getHost(config: StorageEngineConnectionConfig): string {
@@ -222,7 +184,11 @@ Run ${chalk.italic.green('npm i -g @cloudgraph/cli')} to install`)
222184
flags: { dev: devMode },
223185
} = this.parse(this.constructor as Input<{ dev: boolean }>)
224186
if (!this.manager) {
225-
this.manager = new Manager({ logger: this.logger, devMode, cliConfig: this.config })
187+
this.manager = new Manager({
188+
logger: this.logger,
189+
devMode,
190+
cliConfig: this.config,
191+
})
226192
}
227193
return this.manager
228194
}

0 commit comments

Comments
 (0)