Skip to content

Commit a858115

Browse files
authored
Add unit test for featureSupport.ts (#14)
1 parent bca8254 commit a858115

File tree

5 files changed

+492
-27
lines changed

5 files changed

+492
-27
lines changed

.mocharc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extension": [
3+
"ts"
4+
],
5+
"spec": "src/**/*.test.ts",
6+
"require": "ts-node/register"
7+
}

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,20 @@
105105
"main": "./out/extension.js",
106106
"segmentKey": "YErmvd89wPsrCuGcVnF2XAl846W9WIGl",
107107
"scripts": {
108-
"vscode:prepublish": "webpack --mode production",
108+
"vscode:prepublish": "yarn test && webpack --mode production",
109109
"webpack": "webpack --mode development",
110110
"compile": "tsc -b",
111111
"watch": "tsc -b -w",
112112
"package": "npx vsce package --yarn",
113-
"lint": "eslint . --ext .ts"
113+
"lint": "eslint . --ext .ts",
114+
"test": "env TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' mocha"
114115
},
115116
"devDependencies": {
116117
"@types/analytics-node": "^3.1.9",
117118
"@types/crypto-js": "4.1.1",
118119
"@types/google-protobuf": "^3.7.4",
119120
"@types/js-yaml": "^4.0.5",
121+
"@types/mocha": "^9.1.1",
120122
"@types/node": "16.x",
121123
"@types/node-fetch": "^2.5.12",
122124
"@types/semver": "^7.3.10",
@@ -133,7 +135,9 @@
133135
"eslint-plugin-header": "3.1.1",
134136
"eslint-plugin-jsdoc": "^19.1.0",
135137
"minimist": "^1.2.6",
138+
"mocha": "^10.0.0",
136139
"ts-loader": "^9.2.7",
140+
"ts-node": "^10.9.1",
137141
"typescript": "^4.6.3",
138142
"webpack": "^5.42.0",
139143
"webpack-cli": "^4.7.2"

src/featureSupport.test.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Gitpod. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
import { equal } from 'assert';
6+
import Log from './common/logger';
7+
import { getGitpodVersion, GitpodVersion, isFeatureSupported } from './featureSupport';
8+
9+
describe('feature support', () => {
10+
it('isFeatureSupported with versions', () => {
11+
const cases: Array<{ str?: string; version: GitpodVersion; supported: boolean }> = [
12+
{ version: new GitpodVersion('release-2022.06.1.7'), str: '2022.6.1', supported: false },
13+
{ version: new GitpodVersion('release-2022.06.1.0'), str: '2022.6.1', supported: false },
14+
{ version: new GitpodVersion('release-2022.77.1.0'), str: '2022.77.1', supported: true },
15+
{ version: new GitpodVersion('release-0.0.0.0'), str: '0.0.0', supported: false },
16+
{ version: new GitpodVersion('abcd.0123.0.0.0'), str: '0.0.0', supported: false },
17+
{ version: new GitpodVersion('abcd.123.0.0'), supported: false },
18+
{ version: new GitpodVersion('123'), supported: false },
19+
{ version: new GitpodVersion('123..'), supported: false },
20+
{ version: new GitpodVersion('123.0'), supported: false },
21+
{ version: new GitpodVersion('123.0.1'), supported: false },
22+
{ version: new GitpodVersion('9123.0.1'), supported: true },
23+
{ version: new GitpodVersion(), str: '0.0.0', supported: false },
24+
{ version: GitpodVersion.Max, str: GitpodVersion.MAX_VERSION, supported: true },
25+
{ version: GitpodVersion.Min, str: GitpodVersion.MIN_VERSION, supported: false },
26+
27+
// SaaS is processed in `getOrFetchVersionInfo` function
28+
{ version: new GitpodVersion('main.123'), supported: false },
29+
{ version: new GitpodVersion('main.9999'), supported: false },
30+
];
31+
for (let i = 0; i < cases.length; i++) {
32+
const { version, str, supported } = cases[i];
33+
equal(isFeatureSupported(version, 'localHeartbeat'), supported, `isFeatureSupported index: ${i}`);
34+
if (str) {
35+
equal(version.version, str, `version check index: ${i}`);
36+
}
37+
}
38+
});
39+
40+
describe('fetch version info', function () {
41+
this.timeout(10000);
42+
// const logger = { info: console.log, error: console.error } as Log;
43+
// @ts-ignore
44+
const logger = { info: (...args: any) => { }, error: (...args: any) => { } } as Log;
45+
46+
it.skip('unknown host retry and fallback to min', async () => {
47+
const version = await getGitpodVersion('https://unknown.gitpod.io', logger);
48+
equal(version.version, GitpodVersion.Min.version);
49+
});
50+
51+
it('SaaS Gitpod return max', async () => {
52+
const version = await getGitpodVersion('https://gitpod.io', logger);
53+
equal(version.version, GitpodVersion.Max.version);
54+
});
55+
});
56+
});

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
},
4141
"exclude": [
4242
"node_modules",
43-
".vscode-test"
43+
".vscode-test",
44+
"**/*.test.ts"
4445
]
4546
}

0 commit comments

Comments
 (0)