diff --git a/.gitignore b/.gitignore index b512c09..f06235c 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +dist diff --git a/.gtconfig/setup-jest.js b/.gtconfig/setup-jest.js new file mode 100644 index 0000000..1412757 --- /dev/null +++ b/.gtconfig/setup-jest.js @@ -0,0 +1,136 @@ +#!/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; + settings["jest.enable"] = true; + settings["jest.runMode"] = "on-demand”; + + // 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.'); diff --git a/.gtconfig/setup-mocha.js b/.gtconfig/setup-mocha.js new file mode 100644 index 0000000..c944ef7 --- /dev/null +++ b/.gtconfig/setup-mocha.js @@ -0,0 +1,130 @@ +#!/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; + settings["jest.enable"] = false; + + // 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.'); diff --git a/.gtignore b/.gtignore index 135d1c5..105199e 100644 --- a/.gtignore +++ b/.gtignore @@ -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/ diff --git a/.mocharc.json b/.mocharc.json new file mode 100644 index 0000000..0d53a44 --- /dev/null +++ b/.mocharc.json @@ -0,0 +1,7 @@ +{ + "extension": [ + "ts" + ], + "spec": "src/**/*.test.ts", + "require": "ts-node/register" +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..d7abae4 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,4 @@ +{ + "jest.runMode": "on-demand", + "editor.formatOnSave": true +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..295706a --- /dev/null +++ b/.vscode/tasks.json @@ -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": [] + } + ] +} diff --git a/package.json b/package.json index 3b85373..ec8d0c7 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/main.test.ts b/src/main.test.ts new file mode 100644 index 0000000..5497d78 --- /dev/null +++ b/src/main.test.ts @@ -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); + }); +}); diff --git a/tsconfig.json b/tsconfig.json index 59517da..92e1bfc 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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" + ], }