Skip to content

Commit 0974700

Browse files
committed
Convert pure tests to Jest
This converts all pure tests to Jest. This was done by first running `npx jest-codemods` with the Mocha transformation, then manually fixing any places where it hadn't automatically converted the correct thing or had missed things (mostly Sinon). This also sets up VSCode correctly for running Jest.
1 parent 03f1547 commit 0974700

31 files changed

Lines changed: 625 additions & 505 deletions

.vscode/extensions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"recommendations": [
66
"amodio.tsl-problem-matcher",
77
"dbaeumer.vscode-eslint",
8-
"esbenp.prettier-vscode"
8+
"esbenp.prettier-vscode",
9+
"Orta.vscode-jest",
910
],
1011
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
1112
"unwantedRecommendations": []

.vscode/launch.json

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,16 @@
2929
"name": "Launch Unit Tests (vscode-codeql)",
3030
"type": "node",
3131
"request": "launch",
32-
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/mocha/bin/_mocha",
32+
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/jest/bin/jest.js",
3333
"showAsyncStacks": true,
3434
"cwd": "${workspaceFolder}/extensions/ql-vscode",
35-
"runtimeArgs": [
36-
"--inspect=9229"
37-
],
3835
"env": {
39-
"LANG": "en-US"
36+
"LANG": "en-US",
37+
"TZ": "UTC"
4038
},
4139
"args": [
42-
"--exit",
43-
"-u",
44-
"bdd",
45-
"--colors",
46-
"--diff",
47-
"--config",
48-
".mocharc.json",
49-
"test/pure-tests/**/*.ts"
40+
"--projects",
41+
"test"
5042
],
5143
"stopOnEntry": false,
5244
"sourceMaps": true,
@@ -60,6 +52,10 @@
6052
"program": "${workspaceFolder}/extensions/ql-vscode/node_modules/jest/bin/jest.js",
6153
"showAsyncStacks": true,
6254
"cwd": "${workspaceFolder}/extensions/ql-vscode",
55+
"args": [
56+
"--projects",
57+
"src/view"
58+
],
6359
"stopOnEntry": false,
6460
"sourceMaps": true,
6561
"console": "integratedTerminal",
@@ -117,7 +113,7 @@
117113
"--disable-extension",
118114
"github.copilot",
119115
"${workspaceRoot}/extensions/ql-vscode/src/vscode-tests/cli-integration/data",
120-
// Uncomment the last line and modify the path to a checked out
116+
// Uncomment the last line and modify the path to a checked out
121117
// instance of the codeql repository so the libraries are
122118
// available in the workspace for the tests.
123119
// "${workspaceRoot}/../codeql"

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@
3737
"javascript.preferences.quoteStyle": "single",
3838
"editor.wordWrapColumn": 100,
3939
"jest.rootPath": "./extensions/ql-vscode",
40+
"jest.autoRun": "watch",
41+
"jest.nodeEnv": {
42+
"LANG": "en-US",
43+
"TZ": "UTC"
44+
},
4045
"[typescript]": {
4146
"editor.defaultFormatter": "esbenp.prettier-vscode",
4247
"editor.formatOnSave": true,

extensions/ql-vscode/.mocharc.json

Lines changed: 0 additions & 4 deletions
This file was deleted.

extensions/ql-vscode/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
*/
55

66
module.exports = {
7-
projects: ["<rootDir>/src/view"],
7+
projects: ["<rootDir>/src/view", "<rootDir>/test"],
88
};

extensions/ql-vscode/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,7 @@
12701270
"watch:webpack": "gulp watchView",
12711271
"watch:files": "gulp watchTestData",
12721272
"test": "npm-run-all -p test:*",
1273-
"test:unit": "mocha --config .mocharc.json 'test/pure-tests/**/*.ts'",
1273+
"test:unit": "jest --projects test",
12741274
"test:view": "jest --projects src/view",
12751275
"integration": "node ./out/vscode-tests/run-integration-tests.js no-workspace,minimal-workspace",
12761276
"integration:no-workspace": "node ./out/vscode-tests/run-integration-tests.js no-workspace",
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
env: {
3-
mocha: true
3+
jest: true,
44
},
55
parserOptions: {
6-
project: './test/tsconfig.json',
6+
project: "./test/tsconfig.json",
77
},
8-
}
8+
};
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import type { Config } from "jest";
2+
3+
/*
4+
* For a detailed explanation regarding each configuration property and type check, visit:
5+
* https://jestjs.io/docs/configuration
6+
*/
7+
8+
const config: Config = {
9+
// All imported modules in your tests should be mocked automatically
10+
// automock: false,
11+
12+
// Stop running tests after `n` failures
13+
// bail: 0,
14+
15+
// The directory where Jest should store its cached dependency information
16+
// cacheDirectory: "/private/var/folders/6m/1394pht172qgd7dmw1fwjk100000gn/T/jest_dx",
17+
18+
// Automatically clear mock calls, instances, contexts and results before every test
19+
// clearMocks: true,
20+
21+
// Indicates whether the coverage information should be collected while executing the test
22+
// collectCoverage: false,
23+
24+
// An array of glob patterns indicating a set of files for which coverage information should be collected
25+
// collectCoverageFrom: undefined,
26+
27+
// The directory where Jest should output its coverage files
28+
// coverageDirectory: undefined,
29+
30+
// An array of regexp pattern strings used to skip coverage collection
31+
// coveragePathIgnorePatterns: [
32+
// "/node_modules/"
33+
// ],
34+
35+
// Indicates which provider should be used to instrument code for coverage
36+
coverageProvider: "v8",
37+
38+
// A list of reporter names that Jest uses when writing coverage reports
39+
// coverageReporters: [
40+
// "json",
41+
// "text",
42+
// "lcov",
43+
// "clover"
44+
// ],
45+
46+
// An object that configures minimum threshold enforcement for coverage results
47+
// coverageThreshold: undefined,
48+
49+
// A path to a custom dependency extractor
50+
// dependencyExtractor: undefined,
51+
52+
// Make calling deprecated APIs throw helpful error messages
53+
// errorOnDeprecated: false,
54+
55+
// The default configuration for fake timers
56+
// fakeTimers: {
57+
// "enableGlobally": false
58+
// },
59+
60+
// Force coverage collection from ignored files using an array of glob patterns
61+
// forceCoverageMatch: [],
62+
63+
// A path to a module which exports an async function that is triggered once before all test suites
64+
// globalSetup: undefined,
65+
66+
// A path to a module which exports an async function that is triggered once after all test suites
67+
// globalTeardown: undefined,
68+
69+
// A set of global variables that need to be available in all test environments
70+
// globals: {},
71+
72+
// Insert Jest's globals (expect, test, describe, beforeEach etc.) into the global environment. If you set this to false, you should import from @jest/globals.
73+
// injectGlobals: false,
74+
75+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
76+
// maxWorkers: 1,
77+
78+
// An array of directory names to be searched recursively up from the requiring module's location
79+
// moduleDirectories: [
80+
// "node_modules"
81+
// ],
82+
83+
// An array of file extensions your modules use
84+
moduleFileExtensions: ["js", "mjs", "cjs", "jsx", "ts", "tsx", "json"],
85+
86+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
87+
// modulePathIgnorePatterns: [],
88+
89+
// Activates notifications for test results
90+
// notify: false,
91+
92+
// An enum that specifies notification mode. Requires { notify: true }
93+
// notifyMode: "failure-change",
94+
95+
// A preset that is used as a base for Jest's configuration
96+
preset: "ts-jest",
97+
98+
// Run tests from one or more projects
99+
// projects: undefined,
100+
101+
// Use this configuration option to add custom reporters to Jest
102+
// reporters: undefined,
103+
104+
// Automatically reset mock state before every test
105+
// resetMocks: false,
106+
107+
// Reset the module registry before running each individual test
108+
// resetModules: false,
109+
110+
// A path to a custom resolver
111+
// resolver: undefined,
112+
113+
// Automatically restore mock state and implementation before every test
114+
// restoreMocks: false,
115+
116+
// The root directory that Jest should scan for tests and modules within
117+
// rootDir: undefined,
118+
119+
// A list of paths to directories that Jest should use to search for files in
120+
// roots: [
121+
// "<rootDir>"
122+
// ],
123+
124+
// Allows you to use a custom runner instead of Jest's default test runner
125+
// runner: 'vscode',
126+
127+
// The paths to modules that run some code to configure or set up the testing environment before each test
128+
// setupFiles: [],
129+
130+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
131+
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
132+
133+
// The number of seconds after which a test is considered as slow and reported as such in the results.
134+
// slowTestThreshold: 5,
135+
136+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
137+
// snapshotSerializers: [],
138+
139+
// The test environment that will be used for testing
140+
// testEnvironment: 'jsdom',
141+
142+
// Options that will be passed to the testEnvironment
143+
// testEnvironmentOptions: {},
144+
145+
// Adds a location field to test results
146+
// testLocationInResults: false,
147+
148+
// The glob patterns Jest uses to detect test files
149+
testMatch: ["**/*.test.[jt]s"],
150+
151+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
152+
// testPathIgnorePatterns: [
153+
// "/node_modules/"
154+
// ],
155+
156+
// The regexp pattern or array of patterns that Jest uses to detect test files
157+
// testRegex: [],
158+
159+
// This option allows the use of a custom results processor
160+
// testResultsProcessor: undefined,
161+
162+
// This option allows use of a custom test runner
163+
// testRunner: "jest-circus/runner",
164+
165+
// A map from regular expressions to paths to transformers
166+
transform: {
167+
"^.+\\.tsx?$": [
168+
"ts-jest",
169+
{
170+
tsconfig: "<rootDir>/tsconfig.json",
171+
},
172+
],
173+
node_modules: [
174+
"babel-jest",
175+
{
176+
presets: ["@babel/preset-env"],
177+
plugins: ["@babel/plugin-transform-modules-commonjs"],
178+
},
179+
],
180+
},
181+
182+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
183+
// 'transformIgnorePatterns': [
184+
// // These use ES modules, so need to be transformed
185+
// 'node_modules/(?!(?:@vscode/webview-ui-toolkit|@microsoft/.+|exenv-es6)/.*)'
186+
// ],
187+
188+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
189+
// unmockedModulePathPatterns: undefined,
190+
191+
// Indicates whether each individual test should be reported during the run
192+
// verbose: undefined,
193+
194+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
195+
// watchPathIgnorePatterns: [],
196+
197+
// Whether to use watchman for file crawling
198+
// watchman: true,
199+
};
200+
201+
export default config;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
process.env.TZ = "UTC";
2+
process.env.LANG = "en-US";

extensions/ql-vscode/test/mocha.setup.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)