Skip to content

feat: add support for switching test frameworks #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
dist
134 changes: 134 additions & 0 deletions .gtconfig/setup-jest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🔄 Setting up Jest...');

// Ensure src directory exists
if (!fs.existsSync('src')) {
fs.mkdirSync('src');
console.log('✅ Created src directory');
}

// Clean up Mocha files
try {
if (fs.existsSync('.mocharc.json')) {
fs.unlinkSync('.mocharc.json');
console.log('✅ Removed .mocharc.json');
}
} catch (error) {
console.log('⚠️ Could not remove .mocharc.json:', error.message);
}

// Uninstall Mocha dependencies
console.log('📦 Uninstalling Mocha dependencies...');
try {
execSync('npm uninstall @types/chai @types/mocha chai mocha', { stdio: 'inherit' });
console.log('✅ Mocha dependencies uninstalled');
} catch (error) {
console.log('⚠️ Some Mocha dependencies may not have been installed');
}

// Install Jest dependencies
console.log('📦 Installing Jest dependencies...');
try {
execSync('npm install --save-dev jest ts-jest @types/jest', { stdio: 'inherit' });
console.log('✅ Jest dependencies installed');
} catch (error) {
console.error('❌ Failed to install Jest dependencies:', error.message);
process.exit(1);
}

// Create Jest config
try {
const jestConfig = `const { createDefaultPreset } = require("ts-jest");

const tsJestTransformCfg = createDefaultPreset().transform;

/** @type {import("jest").Config} **/
module.exports = {
testEnvironment: "node",
transform: {
...tsJestTransformCfg,
},
};`;
fs.writeFileSync('jest.config.js', jestConfig);
console.log('✅ Created jest.config.js');
} catch (error) {
console.error('❌ Failed to setup Jest config:', error.message);
process.exit(1);
}

// Create Jest test example
try {
const jestTest = `// This is a sample test written for Jest.
//
// If you prefer using Mocha+Chai instead, run \`npm run init:mocha\` in the terminal to setup Mocha dependencies.
// This will switch your project to use Mocha as the test runner with Chai for assertions.

import assert from "assert";

describe('Jest Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});`;
fs.writeFileSync('src/main.test.ts', jestTest);
console.log('✅ Created Jest test example in src/main.test.ts');
} catch (error) {
console.error('❌ Failed to setup Jest test:', error.message);
process.exit(1);
}

// Update package.json test script
try {
const packageJsonPath = 'package.json';
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = 'jest';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Updated package.json test script');
}
} catch (error) {
console.error('❌ Failed to update package.json:', error.message);
}

console.log('🎉 Jest setup complete! Reloading window now ...');


try {
const vscodeDir = '.vscode';
const settingsFile = path.join(vscodeDir, 'settings.json');

if (!fs.existsSync(vscodeDir)) {
fs.mkdirSync(vscodeDir);
}

let settings = {};
if (fs.existsSync(settingsFile)) {
try {
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
settings = JSON.parse(settingsContent);
} catch (error) {
console.log('⚠️ Could not parse existing settings.json, creating new one');
settings = {};
}
}

// Increment reload trigger counter
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;

// Write updated settings
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
} catch (error) {
console.log('⚠️ Could not reload workspace:', error.message);
console.log('Please reload the window manually...');
}

console.log('✅ Reloaded workspace');
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');
129 changes: 129 additions & 0 deletions .gtconfig/setup-mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#!/usr/bin/env node

const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');

console.log('🔄 Setting up Mocha...');

// Ensure src directory exists
if (!fs.existsSync('src')) {
fs.mkdirSync('src');
console.log('✅ Created src directory');
}

// Clean up Jest files
try {
if (fs.existsSync('jest.config.js')) {
fs.unlinkSync('jest.config.js');
console.log('✅ Removed jest.config.js');
}
} catch (error) {
console.log('⚠️ Could not remove jest.config.js:', error.message);
}

// Uninstall Jest dependencies
console.log('📦 Uninstalling Jest dependencies...');
try {
execSync('npm uninstall jest ts-jest @types/jest', { stdio: 'inherit' });
console.log('✅ Jest dependencies uninstalled');
} catch (error) {
console.log('⚠️ Some Jest dependencies may not have been installed');
}

// Install Mocha dependencies
console.log('📦 Installing Mocha dependencies...');
try {
execSync('npm install --save-dev @types/chai @types/mocha chai mocha ts-node', { stdio: 'inherit' });
console.log('✅ Mocha dependencies installed');
} catch (error) {
console.error('❌ Failed to install Mocha dependencies:', error.message);
process.exit(1);
}

// Create Mocha config
try {
const mochaConfig = `{
"extension": [
"ts"
],
"spec": "src/**/*.test.ts",
"require": "ts-node/register"
}`;
fs.writeFileSync('.mocharc.json', mochaConfig);
console.log('✅ Created .mocharc.json');
} catch (error) {
console.error('❌ Failed to setup Mocha config:', error.message);
process.exit(1);
}

// Create Mocha test example
try {
const mochaTest = `// We support Mocha + Chai for unit testing by default.
//
// If you prefer using Jest instead, run \`npm run init:jest\` in the terminal to setup Jest dependencies.
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.

import { assert } from 'chai';

describe('Mocha Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});`;
fs.writeFileSync('src/main.test.ts', mochaTest);
console.log('✅ Created Mocha test example in src/main.test.ts');
} catch (error) {
console.error('❌ Failed to setup Mocha test:', error.message);
process.exit(1);
}

// Update package.json test script
try {
const packageJsonPath = 'package.json';
if (fs.existsSync(packageJsonPath)) {
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
packageJson.scripts = packageJson.scripts || {};
packageJson.scripts.test = 'mocha';
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson, null, 2));
console.log('✅ Updated package.json test script');
}
} catch (error) {
console.error('❌ Failed to update package.json:', error.message);
}

console.log('🎉 Mocha setup complete! Reloading window now ...');

try {
const vscodeDir = '.vscode';
const settingsFile = path.join(vscodeDir, 'settings.json');

if (!fs.existsSync(vscodeDir)) {
fs.mkdirSync(vscodeDir);
}

let settings = {};
if (fs.existsSync(settingsFile)) {
try {
const settingsContent = fs.readFileSync(settingsFile, 'utf8');
settings = JSON.parse(settingsContent);
} catch (error) {
console.log('⚠️ Could not parse existing settings.json, creating new one');
settings = {};
}
}

// Increment reload trigger counter
const currentCounter = settings["live-interview-companion.reloadTriggerCounter"] || 0;
settings["live-interview-companion.reloadTriggerCounter"] = currentCounter + 1;

// Write updated settings
console.log(`🔄 Reloading workspace (counter: ${currentCounter + 1})`);
fs.writeFileSync(settingsFile, JSON.stringify(settings, null, 2));
} catch (error) {
console.log('⚠️ Could not reload workspace:', error.message);
console.log('Please reload the window manually...');
}

console.log('✅ Reloaded workspace');
console.log('🎉 Workspace setup complete! Run "npm test" to run tests.');
5 changes: 5 additions & 0 deletions .gtignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ package.json
yarn.lock
package-lock.json
.theia/
.vscode/
# Ignore all node_modules folders
**/node_modules/
.gtconfig/
.mocharc.json
jest.config.js
dist/
7 changes: 7 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extension": [
"ts"
],
"spec": "src/**/*.test.ts",
"require": "ts-node/register"
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"jest.runMode": "on-demand",
"editor.formatOnSave": true
}
22 changes: 22 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Install Dependencies",
"type": "shell",
"command": "npm install",
"group": "build",
"presentation": {
"echo": true,
"reveal": "never",
"focus": false,
"panel": "dedicated",
"close": true
},
"runOptions": {
"runOn": "folderOpen"
},
"problemMatcher": []
}
]
}
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@
"name": "typescript-interview-command-line-starter-kit",
"version": "0.1.0",
"devDependencies": {
"@types/node": "^20.19.7"
"@types/chai": "^5.2.2",
"@types/mocha": "^10.0.10",
"@types/node": "^24.0.14",
"chai": "^5.2.1",
"mocha": "^11.7.1",
"ts-node": "^10.9.2",
"typescript": "^5.8.3"
},
"license": "MIT"
"scripts": {
"test": "mocha",
"init:jest": "node .gtconfig/setup-jest.js",
"init:mocha": "node .gtconfig/setup-mocha.js"
},
"license": "MIT"
}
12 changes: 12 additions & 0 deletions src/main.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// We support Mocha + Chai for unit testing by default.
//
// If you prefer using Jest instead, run `npm run init:jest` in the terminal to setup Jest dependencies.
// This will switch your project to use Jest which includes built-in assertions and mocking capabilities.

import { assert } from 'chai';

describe('Mocha Test suite', function () {
it('should expect to add', function () {
assert.equal(2 + 3, 5);
});
});
13 changes: 7 additions & 6 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"typeRoots": [
"./node_modules/@types",
"/usr/lib/node_modules/@types",
"/usr/local/share/.config/yarn/global/node_modules/@types"
]
},
"lib": [
"es2015"
],
"include": ["src/**/*"],
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
".gtconfig"
],
}