Skip to content

Commit 55d274c

Browse files
committed
changing mocha code to jest code and added files for jest environment
1 parent 472a919 commit 55d274c

File tree

12 files changed

+159
-24
lines changed

12 files changed

+159
-24
lines changed

jest.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1+
const path = require('path');
2+
13
module.exports = {
24
preset: 'ts-jest',
3-
testEnvironment: 'node',
5+
testEnvironment: './vscode-environment.js',
6+
modulePaths: ['<rootDir>'],
47
// Add other Jest configurations as needed
5-
testMatch: ['**/test/**/*.js', '**/?(*.)+(spec|test).js'], // Match JavaScript files
8+
// testMatch: ['**/test/**/*.js', '**/?(*.)+(spec|test).js'], // Match JavaScript files
69
moduleNameMapper: {
7-
'^@/(.*)$': 'build/src/$1', // Adjust this based on your project structure
10+
// '^@/(.*)$': 'build/src/$1', // Adjust this based on your project structure
11+
vscode: path.join(__dirname, 'vscode.js') // <----- most important line
812
},
913
};

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,14 @@
7070
"scripts": {
7171
"lint": "eslint .",
7272
"pretest": "npm run lint",
73-
"test": "jest",
73+
"test": "node build/src/test/runTest.js",
7474
"dev": "webpack --watch",
7575
"webpack": "webpack"
7676
},
7777
"devDependencies": {
7878
"@babel/preset-typescript": "^7.23.3",
7979
"@types/glob": "^8.1.0",
8080
"@types/jest": "^29.5.11",
81-
"@types/mocha": "^10.0.3",
8281
"@types/node": "18.x",
8382
"@types/react": "^18.2.45",
8483
"@types/react-dom": "^18.2.18",
@@ -88,7 +87,7 @@
8887
"eslint": "^8.54.0",
8988
"glob": "^10.3.10",
9089
"jest": "^29.7.0",
91-
"mocha": "^10.2.0",
90+
"jest-environment-node": "^29.7.0",
9291
"postcss-loader": "^7.3.3",
9392
"postcss-preset-env": "^9.3.0",
9493
"tailwindcss": "^3.3.6",

src/test/runTest.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as path from 'path';
2-
32
import { runTests } from '@vscode/test-electron';
43

54
async function main() {
@@ -19,7 +18,11 @@ async function main() {
1918
console.log('inside try block after second var declare');
2019

2120
// Download VS Code, unzip it and run the integration test
22-
await runTests({ extensionDevelopmentPath, extensionTestsPath });
21+
await runTests({
22+
version: '1.85.1',
23+
extensionDevelopmentPath,
24+
extensionTestsPath
25+
});
2326
} catch (err) {
2427
console.error('Failed to run tests', err);
2528
process.exit(1);

src/test/suite/extension.test.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1-
import * as assert from 'assert'
2-
31
// You can import and use all API from the 'vscode' module
42
// as well as import your extension to test it
53
import * as vscode from 'vscode'
64
// const myExtension = require('../extension');
75

8-
suite('Extension Test Suite', () => {
9-
vscode.window.showInformationMessage('Start all tests.');
6+
// we can either use test() or it() -- matter of style for team/project convention
7+
8+
describe('Extension Test Suite', () => {
9+
beforeEach(() => {
10+
vscode.window.showInformationMessage('Start all tests.');
11+
});
1012

11-
test('Sample test', () => {
12-
assert.strictEqual(-1, [1, 2, 3].indexOf(5)); // false
13-
assert.strictEqual(-1, [1, 2, 3].indexOf(0));
14-
assert.strictEqual(0, [1, 2, 3].indexOf(1)); // true
13+
it('Sample test', () => {
14+
expect([1, 2, 3].indexOf(5)).toBe(-1);
15+
expect([1, 2, 3].indexOf(0)).toBe(-1);
16+
expect([1, 2, 3].indexOf(1)).toBe(0);
1517
});
1618
});

src/test/suite/index.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as path from 'path';
2-
// import * as Mocha from 'mocha';
32
import { glob } from 'glob';
43

54
// export async function run(): Promise<void> {
@@ -34,14 +33,26 @@ import { glob } from 'glob';
3433
import * as jest from 'jest';
3534

3635
export async function run(): Promise<void> {
37-
const testsRoot = path.resolve(__dirname, '..');
38-
const files = await glob('**/**.test.js', { cwd: testsRoot });
39-
4036
try {
37+
console.log('inside try block of index.ts');
38+
39+
const testsRoot = path.resolve(__dirname, '..');
40+
const files = await glob('**/**.test.js', { cwd: testsRoot });
41+
42+
if (files.length === 0) {
43+
console.warn('No test files found');
44+
return;
45+
}
46+
47+
console.log('test files: ', files);
48+
4149
return new Promise(async (c, e) => {
4250
try {
43-
await jest.run();
51+
console.log('inside promise block of index.ts before await ')
52+
await jest.run([...files]);
53+
console.log('inside promise block of index.ts after await')
4454
c();
55+
console.log('inside promise block of index.ts after c()')
4556
} catch (err) {
4657
console.error(err);
4758
e(err);

src/test/suite/parser.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// import * as assert from 'assert' -- this one is from node
2+
import { Parser } from '../../parser';
3+
import * as path from 'path';
4+
import { beforeEach, expect, test } from '@jest/globals';
5+
6+
// You can import and use all API from the 'vscode' module
7+
// as well as import your extension to test it
8+
import * as vscode from 'vscode'
9+
// const myExtension = require('../extension');
10+
11+
describe('Parser Test Suite', () => {
12+
beforeEach(() => {
13+
vscode.window.showInformationMessage('Start all tests.');
14+
});
15+
16+
let parser, tree, file;
17+
18+
// UNPARSED TREE TEST
19+
describe('It initializes correctly', () => {
20+
beforeEach(() => {
21+
// declare var and assign it to a test file and make new instance of Parser
22+
// both of the paths below work
23+
// file = path.join(__dirname, '../test_cases/tc_0/index.js');
24+
file = path.join(__dirname, '../../../src/test/test_apps/test_0/index.js');
25+
parser = new Parser(file);
26+
});
27+
28+
test('It instantiates an object for the parser class', () => {
29+
expect((parser)).toBeInstanceOf(Parser);
30+
// assert.typeOf(parser, 'object', 'Value of new instance should be an object');
31+
// expect(parser).to.be.an('object');
32+
});
33+
34+
test('It begins with a suitable entry file and a tree that is not yet defined', () => {
35+
expect(parser.entryFile).toEqual(file);
36+
expect(tree).toBeUndefined();
37+
// below is my code
38+
// assert.strictEqual(parser.entryFile, file, 'These files are strictly equal');
39+
// assert.isUndefined(tree, 'Tree is defined');
40+
});
41+
});
42+
43+
// TEST ?: UNPARSED TREE TEST FOR REACT 18(createRoot)
44+
45+
// TEST 0: ONE CHILD
46+
// describe('It works for simple apps', () => {
47+
// before(() => {
48+
// file = path.join(__dirname, '');
49+
// parser = new Parser(file);
50+
// tree = parser.parse();
51+
// });
52+
53+
// test('It returns an defined object tree when parsed', () => {
54+
// assert.typeOf(tree, 'object', 'Value of parse() on new instance should be an object');
55+
// });
56+
// });
57+
58+
// TEST 0.5: CHECK IF COMPONENT IS CLIENT OR SERVER (USING HOOKS) => RENDERS A CERTAIN COLOR
59+
// TEST 1: NESTED CHILDREN
60+
// TEST 2: THIRD PARTY, REACT ROUTER, DESTRUCTURED IMPORTS
61+
// TEST 3: IDENTIFIES REDUX STORE CONNECTION
62+
// TEST 4: ALIASED IMPORTS
63+
// TEST 5: MISSING EXTENSIONS AND UNUSED IMPORTS
64+
// TEST 6: BAD IMPORT OF APP2 FROM APP1 COMPONENT
65+
// TEST 7: SYNTAX ERROR IN APP FILE CAUSES PARSER ERROR
66+
// TEST 8: MULTIPLE PROPS ON ONE COMPONENT
67+
// TEST 9: FINDING DIFFERENT PROPS ACROSS TWO OR MORE IDENTICAL COMPONENTS
68+
// TEST 10: CHECK CHILDREN WORKS AND COMPONENTS WORK
69+
// TEST 11: PARSER DOESN'T BREAK UPON RECURSIVE COMPONENTS
70+
// TEST 12: NEXT.JS APPS (pages & app router)
71+
// TEST 13: Variable Declaration Imports and React.lazy Imports
72+
});
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// import React from "react";
2+
export default function App() {
3+
return (
4+
<div>This is the App.</div>
5+
)
6+
}

src/test/test_cases/tc_0/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// what we are building to test for
2+
// we will point to index.js and call new instance of parser passing in file
3+
4+
// expecting:
5+
// if the value of new instance is an obj
6+
// tree is undefined with a proper file
7+
8+
// test case 0 - simple react app with one app component
9+
10+
import React from 'react';
11+
import { createRoot } from 'react-dom/client';
12+
import App from './components/App.jsx';
13+
14+
const root = createRoot(document.getElementById('root'));
15+
root.render(<App />);

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
"strict": false,
1313
"inlineSources": true,
1414
},
15+
"jest": {
16+
"tsconfig": "tsconfig.json"
17+
},
1518
"include": ["src/**/*.ts", "src/**/*.tsx", "src/**/**/*.tsx", "src/**/**/*.ts"],
1619
"exclude": [
1720
"node_modules",
1821
".vscode-test",
1922
"src/webviews",
20-
"src/test/test_apps"
23+
"node_modules/@types/mocha/index.d.ts"
2124
]
2225
}

0 commit comments

Comments
 (0)