Skip to content

Commit 8abea48

Browse files
authored
Improve testing experience (foambubble#881)
* Improve testing experience * Support vscode-jest for unit tests
1 parent 2eeb2e1 commit 8abea48

File tree

14 files changed

+82
-74
lines changed

14 files changed

+82
-74
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ dist
99
docs/_site
1010
docs/.sass-cache
1111
docs/.jekyll-metadata
12+
.test-workspace

Diff for: .vscode/launch.json

+30-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
"version": "0.2.0",
77
"configurations": [
88
{
9-
"type": "node",
109
"name": "Debug Jest Tests",
10+
"type": "extensionHost",
1111
"request": "launch",
12-
"runtimeArgs": ["workspace", "foam-vscode", "run", "test"], // ${yarnWorkspaceName} is what we're missing
13-
"args": ["--runInBand"],
14-
"runtimeExecutable": "yarn",
15-
"console": "integratedTerminal",
16-
"internalConsoleOptions": "neverOpen",
17-
"disableOptimisticBPs": true
12+
"args": [
13+
"${workspaceFolder}/packages/foam-vscode/.test-workspace",
14+
"--disable-extensions",
15+
"--disable-workspace-trust",
16+
"--extensionDevelopmentPath=${workspaceFolder}/packages/foam-vscode",
17+
"--extensionTestsPath=${workspaceFolder}/packages/foam-vscode/out/test/suite"
18+
],
19+
"outFiles": [
20+
"${workspaceFolder}/packages/foam-vscode/out/**/*.js"
21+
],
22+
"preLaunchTask": "${defaultBuildTask}"
1823
},
1924
{
2025
"name": "Run VSCode Extension",
@@ -24,8 +29,25 @@
2429
"args": [
2530
"--extensionDevelopmentPath=${workspaceFolder}/packages/foam-vscode"
2631
],
27-
"outFiles": ["${workspaceFolder}/packages/foam-vscode/out/**/*.js"],
32+
"outFiles": [
33+
"${workspaceFolder}/packages/foam-vscode/out/**/*.js"
34+
],
2835
"preLaunchTask": "${defaultBuildTask}"
36+
},
37+
{
38+
"type": "node",
39+
"name": "vscode-jest-tests",
40+
"request": "launch",
41+
"console": "integratedTerminal",
42+
"internalConsoleOptions": "neverOpen",
43+
"disableOptimisticBPs": true,
44+
"cwd": "${workspaceFolder}/packages/foam-vscode",
45+
"runtimeExecutable": "yarn",
46+
"args": [
47+
"jest",
48+
"--runInBand",
49+
"--watchAll=false"
50+
]
2951
}
3052
]
3153
}

Diff for: .vscode/settings.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"prettier.requireConfig": true,
2525
"editor.formatOnSave": true,
2626
"editor.tabSize": 2,
27-
"jest.debugCodeLens.showWhenTestStateIn": ["fail", "unknown", "pass"],
27+
"jest.autoRun": "off",
28+
"jest.rootPath": "packages/foam-vscode",
29+
"jest.jestCommandLine": "yarn jest"
2830
"gitdoc.enabled": false,
29-
"jest.autoEnable": false,
30-
"jest.runAllTestsFirst": false,
3131
"search.mode": "reuseEditor"
3232
}

Diff for: .yarnrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--ignore-engines true

Diff for: packages/foam-vscode/.test-workspace/.keep

Whitespace-only changes.

Diff for: packages/foam-vscode/babel.config.js

-7
This file was deleted.

Diff for: packages/foam-vscode/jest.config.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module.exports = {
8282
// moduleNameMapper: {},
8383

8484
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
85-
// modulePathIgnorePatterns: [],
85+
modulePathIgnorePatterns: ['.vscode-test'],
8686

8787
// Activates notifications for test results
8888
// notify: false,
@@ -91,7 +91,7 @@ module.exports = {
9191
// notifyMode: "failure-change",
9292

9393
// A preset that is used as a base for Jest's configuration
94-
// preset: undefined,
94+
preset: 'ts-jest',
9595

9696
// Run tests from one or more projects
9797
// projects: undefined,
@@ -126,13 +126,13 @@ module.exports = {
126126
// setupFiles: [],
127127

128128
// A list of paths to modules that run some code to configure or set up the testing framework before each test
129-
// setupFilesAfterEnv: [],
129+
setupFilesAfterEnv: ['jest-extended'],
130130

131131
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
132132
// snapshotSerializers: [],
133133

134134
// The test environment that will be used for testing
135-
testEnvironment: "node"
135+
testEnvironment: 'node',
136136

137137
// Options that will be passed to the testEnvironment
138138
// testEnvironmentOptions: {},
@@ -152,7 +152,10 @@ module.exports = {
152152
// ],
153153

154154
// The regexp pattern or array of patterns that Jest uses to detect test files
155-
// testRegex: [],
155+
// This is overridden in every runCLI invocation but it's here as the default
156+
// for vscode-jest. We only want unit tests in the test explorer (sidebar),
157+
// since spec tests require the entire extension host to be launched before.
158+
testRegex: ['\\.test\\.ts$'],
156159

157160
// This option allows the use of a custom results processor
158161
// testResultsProcessor: undefined,

Diff for: packages/foam-vscode/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@
356356
"build": "tsc -p ./",
357357
"pretest": "yarn build",
358358
"test": "node ./out/test/run-tests.js",
359+
"pretest:unit": "yarn build",
359360
"test:unit": "node ./out/test/run-tests.js --unit",
361+
"pretest:e2e": "yarn build",
360362
"test:e2e": "node ./out/test/run-tests.js --e2e",
361363
"lint": "tsdx lint src",
362364
"clean": "rimraf out",
@@ -388,12 +390,10 @@
388390
"@types/vscode": "^1.47.1",
389391
"@typescript-eslint/eslint-plugin": "^2.30.0",
390392
"@typescript-eslint/parser": "^2.30.0",
391-
"babel-jest": "^26.2.2",
392393
"eslint": "^6.8.0",
393394
"eslint-plugin-import": "^2.24.2",
394395
"husky": "^4.2.5",
395396
"jest": "^26.2.2",
396-
"jest-environment-vscode": "^1.0.0",
397397
"jest-extended": "^0.11.5",
398398
"markdown-it": "^12.0.4",
399399
"rimraf": "^3.0.2",

Diff for: packages/foam-vscode/src/test/run-tests.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import fs from 'fs';
2-
import os from 'os';
31
import path from 'path';
42
import { runTests } from 'vscode-test';
53
import { runUnit } from './suite-unit';
@@ -35,23 +33,17 @@ async function main() {
3533
// The path to the extension test script
3634
// Passed to --extensionTestsPath
3735
const extensionTestsPath = path.join(__dirname, 'suite');
38-
const tmpWorkspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'foam-'));
3936

4037
// Download VS Code, unzip it and run the integration test
4138
await runTests({
4239
extensionDevelopmentPath,
4340
extensionTestsPath,
4441
launchArgs: [
45-
tmpWorkspaceDir,
42+
path.join(extensionDevelopmentPath, '.test-workspace'),
4643
'--disable-extensions',
4744
'--disable-workspace-trust',
4845
],
49-
// Running the tests with vscode 1.53.0 is causing issues in the output/error stream management,
50-
// which is causing a stack overflow, possibly due to a recursive callback.
51-
// Also see https://github.com/foambubble/foam/pull/479#issuecomment-774167127
52-
// Forcing the version to 1.52.0 solves the problem.
53-
// TODO: to review, further investigate, and roll back this workaround.
54-
version: '1.52.0',
46+
version: '1.60.0',
5547
});
5648
} catch (err) {
5749
console.log('Error occurred while running Foam e2e tests:', err);

Diff for: packages/foam-vscode/src/test/suite-unit.ts

+7-11
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,28 @@
88
* they will make direct use of the vscode API to be invoked as commands, create editors,
99
* and so on..
1010
*/
11+
12+
/* eslint-disable import/first */
13+
14+
// Set before imports, see https://github.com/facebook/jest/issues/12162
15+
process.env.FORCE_COLOR = '1';
16+
process.env.NODE_ENV = 'test';
17+
1118
import path from 'path';
1219
import { runCLI } from '@jest/core';
1320

1421
const rootDir = path.join(__dirname, '..', '..');
1522

1623
export function runUnit(): Promise<void> {
17-
process.env.FORCE_COLOR = '1';
18-
process.env.NODE_ENV = 'test';
19-
process.env.BABEL_ENV = 'test';
20-
2124
return new Promise(async (resolve, reject) => {
2225
try {
2326
const { results } = await runCLI(
2427
{
2528
rootDir,
2629
roots: ['<rootDir>/src'],
27-
transform: JSON.stringify({ '^.+\\.ts$': 'ts-jest' }),
2830
runInBand: true,
2931
testRegex: '\\.(test)\\.ts$',
3032
setupFiles: ['<rootDir>/src/test/support/jest-setup.ts'],
31-
setupFilesAfterEnv: ['jest-extended'],
32-
globals: JSON.stringify({
33-
'ts-jest': {
34-
tsconfig: path.join(rootDir, 'tsconfig.json'),
35-
},
36-
}),
3733
testTimeout: 20000,
3834
verbose: false,
3935
silent: false,

Diff for: packages/foam-vscode/src/test/suite.ts

+19-14
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,46 @@
99
* and so on..
1010
*/
1111

12+
/* eslint-disable import/first */
13+
14+
// Set before imports, see https://github.com/facebook/jest/issues/12162
15+
process.env.FORCE_COLOR = '1';
16+
process.env.NODE_ENV = 'test';
17+
1218
import path from 'path';
1319
import { runCLI } from '@jest/core';
20+
import { cleanWorkspace } from './test-utils-vscode';
1421

15-
const rootDir = path.resolve(__dirname, '../..');
22+
const rootDir = path.join(__dirname, '../..');
1623

1724
export function run(): Promise<void> {
1825
const errWrite = process.stderr.write;
26+
27+
let remaining = '';
1928
process.stderr.write = (buffer: string) => {
20-
console.log(buffer);
29+
const lines = (remaining + buffer).split('\n');
30+
remaining = lines.pop() as string;
31+
// Trim long lines because some uninformative code dumps will flood the
32+
// console or, worse, be suppressed altogether because of their size.
33+
lines.forEach(l => console.log(l.substr(0, 300)));
2134
return true;
2235
};
36+
2337
// process.on('unhandledRejection', err => {
2438
// throw err;
2539
// });
26-
process.env.FORCE_COLOR = '1';
27-
process.env.NODE_ENV = 'test';
28-
process.env.BABEL_ENV = 'test';
2940

3041
return new Promise(async (resolve, reject) => {
42+
await cleanWorkspace();
3143
try {
3244
const { results } = await runCLI(
3345
{
3446
rootDir,
3547
roots: ['<rootDir>/src'],
36-
transform: JSON.stringify({ '^.+\\.ts$': 'ts-jest' }),
3748
runInBand: true,
3849
testRegex: '\\.(test|spec)\\.ts$',
39-
testEnvironment:
40-
'<rootDir>/src/test/support/extended-vscode-environment.js',
50+
testEnvironment: '<rootDir>/src/test/support/vscode-environment.js',
4151
setupFiles: ['<rootDir>/src/test/support/jest-setup.ts'],
42-
setupFilesAfterEnv: ['jest-extended'],
43-
globals: JSON.stringify({
44-
'ts-jest': {
45-
tsconfig: path.resolve(rootDir, './tsconfig.json'),
46-
},
47-
}),
4852
testTimeout: 30000,
4953
useStderr: true,
5054
verbose: true,
@@ -71,6 +75,7 @@ export function run(): Promise<void> {
7175
return reject(error);
7276
} finally {
7377
process.stderr.write = errWrite.bind(process.stderr);
78+
await cleanWorkspace();
7479
}
7580
});
7681
}
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
// Based on https://github.com/svsool/vscode-memo/blob/master/src/test/env/ExtendedVscodeEnvironment.js
2-
const VscodeEnvironment = require('jest-environment-vscode');
1+
const NodeEnvironment = require('jest-environment-node');
32
const vscode = require('vscode');
43

5-
const initialVscode = vscode;
6-
class ExtendedVscodeEnvironment extends VscodeEnvironment {
4+
class VscodeEnvironment extends NodeEnvironment {
75
async setup() {
86
await super.setup();
7+
this.global.vscode = vscode;
8+
99
// Expose RegExp otherwise document.getWordRangeAtPosition won't work as supposed.
1010
// Implementation of getWordRangeAtPosition uses "instanceof RegExp" which returns false
1111
// due to Jest running tests in the different vm context.
1212
// See https://github.com/nodejs/node-v0.x-archive/issues/1277.
1313
// And also https://github.com/microsoft/vscode-test/issues/37#issuecomment-700167820
1414
this.global.RegExp = RegExp;
15-
this.global.vscode = vscode;
1615

1716
vscode.workspace
1817
.getConfiguration()
1918
.update('foam.edit.linkReferenceDefinitions', 'off');
2019
}
20+
2121
async teardown() {
22-
this.global.vscode = initialVscode;
22+
this.global.vscode = {};
2323
await super.teardown();
2424
}
2525
}
2626

27-
module.exports = ExtendedVscodeEnvironment;
27+
module.exports = VscodeEnvironment;

Diff for: packages/foam-vscode/src/test/test-utils-vscode.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { randomString, wait } from './test-utils';
1313
Logger.setLevel('error');
1414

1515
export const cleanWorkspace = async () => {
16-
const files = await vscode.workspace.findFiles('**', '.vscode');
16+
const files = await vscode.workspace.findFiles('**', '{.vscode,.keep}');
1717
await Promise.all(files.map(f => vscode.workspace.fs.delete(f)));
1818
};
1919

Diff for: yarn.lock

+1-6
Original file line numberDiff line numberDiff line change
@@ -2954,7 +2954,7 @@ babel-jest@^24.9.0:
29542954
chalk "^2.4.2"
29552955
slash "^2.0.0"
29562956

2957-
babel-jest@^26.2.2, babel-jest@^26.6.3:
2957+
babel-jest@^26.6.3:
29582958
version "26.6.3"
29592959
resolved "https://registry.yarnpkg.com/babel-jest/-/babel-jest-26.6.3.tgz#d87d25cb0037577a0c89f82e5755c5d293c01056"
29602960
integrity sha512-pl4Q+GAVOHwvjrck6jKjvmGhnO3jHX/xuB9d27f+EJZ/6k+6nMuPjorrYp7s++bKKdANwzElBWnLWaObvTnaZA==
@@ -6515,11 +6515,6 @@ jest-environment-node@^26.6.2:
65156515
jest-mock "^26.6.2"
65166516
jest-util "^26.6.2"
65176517

6518-
jest-environment-vscode@^1.0.0:
6519-
version "1.0.0"
6520-
resolved "https://registry.yarnpkg.com/jest-environment-vscode/-/jest-environment-vscode-1.0.0.tgz#96367fe8531047e64359e0682deafc973bfaea91"
6521-
integrity sha512-VKlj5j5pNurFEwWPaDiX1kBgmhWqcJTAZsvEX1x5lh0/+5myjk+qipEs/dPJVRbBPb3XFxiR48XzGn+wOU7SSQ==
6522-
65236518
jest-extended@^0.11.5:
65246519
version "0.11.5"
65256520
resolved "https://registry.yarnpkg.com/jest-extended/-/jest-extended-0.11.5.tgz#f063b3f1eaadad8d7c13a01f0dfe0f538d498ccf"

0 commit comments

Comments
 (0)