Skip to content

Commit 841a762

Browse files
feat: add support for switching test frameworks
1 parent c311e31 commit 841a762

File tree

10 files changed

+335
-9
lines changed

10 files changed

+335
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
node_modules
1+
node_modules
2+
dist

.gtconfig/setup-jest.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!/usr/bin/env node
2+
3+
const { execSync } = require('child_process');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
console.log('🔄 Setting up Jest...');
8+
9+
// Ensure src directory exists
10+
if (!fs.existsSync('src')) {
11+
fs.mkdirSync('src');
12+
console.log('✅ Created src directory');
13+
}
14+
15+
// Clean up Mocha files
16+
try {
17+
if (fs.existsSync('.mocharc.json')) {
18+
fs.unlinkSync('.mocharc.json');
19+
console.log('✅ Removed .mocharc.json');
20+
}
21+
} catch (error) {
22+
console.log('⚠️ Could not remove .mocharc.json:', error.message);
23+
}
24+
25+
// Uninstall Mocha dependencies
26+
console.log('📦 Uninstalling Mocha dependencies...');
27+
try {
28+
execSync('npm uninstall @types/chai @types/mocha chai mocha', { stdio: 'inherit' });
29+
console.log('✅ Mocha dependencies uninstalled');
30+
} catch (error) {
31+
console.log('⚠️ Some Mocha dependencies may not have been installed');
32+
}
33+
34+
// Install Jest dependencies
35+
console.log('📦 Installing Jest dependencies...');
36+
try {
37+
execSync('npm install --save-dev jest ts-jest @types/jest', { stdio: 'inherit' });
38+
console.log('✅ Jest dependencies installed');
39+
} catch (error) {
40+
console.error('❌ Failed to install Jest dependencies:', error.message);
41+
process.exit(1);
42+
}
43+
44+
// Create Jest config
45+
try {
46+
const jestConfig = `const { createDefaultPreset } = require("ts-jest");
47+
48+
const tsJestTransformCfg = createDefaultPreset().transform;
49+
50+
/** @type {import("jest").Config} **/
51+
module.exports = {
52+
testEnvironment: "node",
53+
transform: {
54+
...tsJestTransformCfg,
55+
},
56+
};`;
57+
fs.writeFileSync('jest.config.js', jestConfig);
58+
console.log('✅ Created jest.config.js');
59+
} catch (error) {
60+
console.error('❌ Failed to setup Jest config:', error.message);
61+
process.exit(1);
62+
}
63+
64+
// Create Jest test example
65+
try {
66+
const jestTest = `// This is a sample test written for Jest.
67+
//
68+
// If you prefer using Mocha+Chai instead, run \`npm run init:mocha\` in the terminal to setup Mocha dependencies.
69+
// This will switch your project to use Mocha as the test runner with Chai for assertions.
70+
71+
import assert from "assert";
72+
73+
describe('Jest Test suite', function () {
74+
it('should expect to add', function () {
75+
assert.equal(2 + 3, 5);
76+
});
77+
});`;
78+
fs.writeFileSync('src/main.test.ts', jestTest);
79+
console.log('✅ Created Jest test example in src/main.test.ts');
80+
} catch (error) {
81+
console.error('❌ Failed to setup Jest test:', error.message);
82+
process.exit(1);
83+
}
84+
85+
// Update package.json test script
86+
try {
87+
const packageJsonPath = 'package.json';
88+
if (fs.existsSync(packageJsonPath)) {
89+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
90+
packageJson.scripts = packageJson.scripts || {};
91+
packageJson.scripts.test = 'jest';
92+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
93+
console.log('✅ Updated package.json test script');
94+
}
95+
} catch (error) {
96+
console.error('❌ Failed to update package.json:', error.message);
97+
}
98+
99+
console.log('🎉 Jest setup complete! Reloading window now ...');
100+
101+
102+
try {
103+
const vscodeDir = '.vscode';
104+
const settingsFile = path.join(vscodeDir, 'settings.json');
105+
106+
if (!fs.existsSync(vscodeDir)) {
107+
fs.mkdirSync(vscodeDir);
108+
}
109+
110+
let settings = {};
111+
if (fs.existsSync(settingsFile)) {
112+
try {
113+
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
114+
settings = JSON.parse(settingsContent);
115+
} catch (error) {
116+
console.log('⚠️ Could not parse existing settings.json, creating new one');
117+
settings = {};
118+
}
119+
}
120+
121+
// Increment reload trigger counter
122+
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
123+
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;
124+
125+
// Write updated settings
126+
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
127+
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
128+
} catch (error) {
129+
console.log('⚠️ Could not reload workspace:', error.message);
130+
console.log('Please reload the window manually...');
131+
}
132+
133+
console.log('✅ Reloaded workspace');
134+
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');

.gtconfig/setup-mocha.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env node
2+
3+
const { execSync } = require('child_process');
4+
const fs = require('fs');
5+
const path = require('path');
6+
7+
console.log('🔄 Setting up Mocha...');
8+
9+
// Ensure src directory exists
10+
if (!fs.existsSync('src')) {
11+
fs.mkdirSync('src');
12+
console.log('✅ Created src directory');
13+
}
14+
15+
// Clean up Jest files
16+
try {
17+
if (fs.existsSync('jest.config.js')) {
18+
fs.unlinkSync('jest.config.js');
19+
console.log('✅ Removed jest.config.js');
20+
}
21+
} catch (error) {
22+
console.log('⚠️ Could not remove jest.config.js:', error.message);
23+
}
24+
25+
// Uninstall Jest dependencies
26+
console.log('📦 Uninstalling Jest dependencies...');
27+
try {
28+
execSync('npm uninstall jest ts-jest @types/jest', { stdio: 'inherit' });
29+
console.log('✅ Jest dependencies uninstalled');
30+
} catch (error) {
31+
console.log('⚠️ Some Jest dependencies may not have been installed');
32+
}
33+
34+
// Install Mocha dependencies
35+
console.log('📦 Installing Mocha dependencies...');
36+
try {
37+
execSync('npm install --save-dev @types/chai @types/mocha chai mocha ts-node', { stdio: 'inherit' });
38+
console.log('✅ Mocha dependencies installed');
39+
} catch (error) {
40+
console.error('❌ Failed to install Mocha dependencies:', error.message);
41+
process.exit(1);
42+
}
43+
44+
// Create Mocha config
45+
try {
46+
const mochaConfig = `{
47+
"extension": [
48+
"ts"
49+
],
50+
"spec": "src/**/*.test.ts",
51+
"require": "ts-node/register"
52+
}`;
53+
fs.writeFileSync('.mocharc.json', mochaConfig);
54+
console.log('✅ Created .mocharc.json');
55+
} catch (error) {
56+
console.error('❌ Failed to setup Mocha config:', error.message);
57+
process.exit(1);
58+
}
59+
60+
// Create Mocha test example
61+
try {
62+
const mochaTest = `// We support Mocha + Chai for unit testing by default.
63+
//
64+
// If you prefer using Jest instead, run \`npm run init:jest\` in the terminal to setup Jest dependencies.
65+
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.
66+
67+
import { assert } from 'chai';
68+
69+
describe('Mocha Test suite', function () {
70+
it('should expect to add', function () {
71+
assert.equal(2 + 3, 5);
72+
});
73+
});`;
74+
fs.writeFileSync('src/main.test.ts', mochaTest);
75+
console.log('✅ Created Mocha test example in src/main.test.ts');
76+
} catch (error) {
77+
console.error('❌ Failed to setup Mocha test:', error.message);
78+
process.exit(1);
79+
}
80+
81+
// Update package.json test script
82+
try {
83+
const packageJsonPath = 'package.json';
84+
if (fs.existsSync(packageJsonPath)) {
85+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
86+
packageJson.scripts = packageJson.scripts || {};
87+
packageJson.scripts.test = 'mocha';
88+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
89+
console.log('✅ Updated package.json test script');
90+
}
91+
} catch (error) {
92+
console.error('❌ Failed to update package.json:', error.message);
93+
}
94+
95+
console.log('🎉 Mocha setup complete! Reloading window now ...');
96+
97+
try {
98+
const vscodeDir = '.vscode';
99+
const settingsFile = path.join(vscodeDir, 'settings.json');
100+
101+
if (!fs.existsSync(vscodeDir)) {
102+
fs.mkdirSync(vscodeDir);
103+
}
104+
105+
let settings = {};
106+
if (fs.existsSync(settingsFile)) {
107+
try {
108+
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
109+
settings = JSON.parse(settingsContent);
110+
} catch (error) {
111+
console.log('⚠️ Could not parse existing settings.json, creating new one');
112+
settings = {};
113+
}
114+
}
115+
116+
// Increment reload trigger counter
117+
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
118+
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;
119+
120+
// Write updated settings
121+
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
122+
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
123+
} catch (error) {
124+
console.log('⚠️ Could not reload workspace:', error.message);
125+
console.log('Please reload the window manually...');
126+
}
127+
128+
console.log('✅ Reloaded workspace');
129+
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');

.gtignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@ package.json
66
yarn.lock
77
package-lock.json
88
.theia/
9+
.vscode/
910
# Ignore all node_modules folders
1011
**/node_modules/
12+
.gtconfig/
13+
.mocharc.json
14+
jest.config.js
15+
dist/

.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+
}

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"jest.runMode": "on-demand",
3+
"editor.formatOnSave": true
4+
}

.vscode/tasks.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"version": "2.0.0",
3+
"tasks": [
4+
{
5+
"label": "Install Dependencies",
6+
"type": "shell",
7+
"command": "npm install",
8+
"group": "build",
9+
"presentation": {
10+
"echo": true,
11+
"reveal": "never",
12+
"focus": false,
13+
"panel": "dedicated",
14+
"close": true
15+
},
16+
"runOptions": {
17+
"runOn": "folderOpen"
18+
},
19+
"problemMatcher": []
20+
}
21+
]
22+
}

package.json

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@
22
"name": "typescript-interview-command-line-starter-kit",
33
"version": "0.1.0",
44
"devDependencies": {
5-
"@types/node": "^20.19.7"
5+
"@types/chai": "^5.2.2",
6+
"@types/mocha": "^10.0.10",
7+
"@types/node": "^24.0.14",
8+
"chai": "^5.2.1",
9+
"mocha": "^11.7.1",
10+
"ts-node": "^10.9.2",
11+
"typescript": "^5.8.3"
612
},
7-
"license": "MIT"
13+
"scripts": {
14+
"test": "mocha",
15+
"init:jest": "node .gtconfig/setup-jest.js",
16+
"init:mocha": "node .gtconfig/setup-mocha.js"
17+
},
18+
"license": "MIT"
819
}

src/main.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// We support Mocha + Chai for unit testing by default.
2+
//
3+
// If you prefer using Jest instead, run `npm run init:jest` in the terminal to setup Jest dependencies.
4+
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.
5+
6+
import { assert } from 'chai';
7+
8+
describe('Mocha Test suite', function () {
9+
it('should expect to add', function () {
10+
assert.equal(2 + 3, 5);
11+
});
12+
});

tsconfig.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
"moduleResolution": "node",
77
"sourceMap": true,
88
"outDir": "dist",
9-
"typeRoots": [
10-
"./node_modules/@types",
11-
"/usr/lib/node_modules/@types",
12-
"/usr/local/share/.config/yarn/global/node_modules/@types"
13-
]
149
},
1510
"lib": [
1611
"es2015"
1712
],
18-
"include": ["src/**/*"],
13+
"include": [
14+
"src/**/*"
15+
],
16+
"exclude": [
17+
"node_modules",
18+
".gtconfig"
19+
],
1920
}

0 commit comments

Comments
 (0)