Skip to content

Commit

Permalink
Improve testing experience (#881)
Browse files Browse the repository at this point in the history
* Improve testing experience
* Support vscode-jest for unit tests
  • Loading branch information
memeplex authored Dec 21, 2021
1 parent 2eeb2e1 commit 8abea48
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 74 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ dist
docs/_site
docs/.sass-cache
docs/.jekyll-metadata
.test-workspace
38 changes: 30 additions & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@
"version": "0.2.0",
"configurations": [
{
"type": "node",
"name": "Debug Jest Tests",
"type": "extensionHost",
"request": "launch",
"runtimeArgs": ["workspace", "foam-vscode", "run", "test"], // ${yarnWorkspaceName} is what we're missing
"args": ["--runInBand"],
"runtimeExecutable": "yarn",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
"args": [
"${workspaceFolder}/packages/foam-vscode/.test-workspace",
"--disable-extensions",
"--disable-workspace-trust",
"--extensionDevelopmentPath=${workspaceFolder}/packages/foam-vscode",
"--extensionTestsPath=${workspaceFolder}/packages/foam-vscode/out/test/suite"
],
"outFiles": [
"${workspaceFolder}/packages/foam-vscode/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"name": "Run VSCode Extension",
Expand All @@ -24,8 +29,25 @@
"args": [
"--extensionDevelopmentPath=${workspaceFolder}/packages/foam-vscode"
],
"outFiles": ["${workspaceFolder}/packages/foam-vscode/out/**/*.js"],
"outFiles": [
"${workspaceFolder}/packages/foam-vscode/out/**/*.js"
],
"preLaunchTask": "${defaultBuildTask}"
},
{
"type": "node",
"name": "vscode-jest-tests",
"request": "launch",
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true,
"cwd": "${workspaceFolder}/packages/foam-vscode",
"runtimeExecutable": "yarn",
"args": [
"jest",
"--runInBand",
"--watchAll=false"
]
}
]
}
6 changes: 3 additions & 3 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
"prettier.requireConfig": true,
"editor.formatOnSave": true,
"editor.tabSize": 2,
"jest.debugCodeLens.showWhenTestStateIn": ["fail", "unknown", "pass"],
"jest.autoRun": "off",
"jest.rootPath": "packages/foam-vscode",
"jest.jestCommandLine": "yarn jest"
"gitdoc.enabled": false,
"jest.autoEnable": false,
"jest.runAllTestsFirst": false,
"search.mode": "reuseEditor"
}
1 change: 1 addition & 0 deletions .yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--ignore-engines true
Empty file.
7 changes: 0 additions & 7 deletions packages/foam-vscode/babel.config.js

This file was deleted.

13 changes: 8 additions & 5 deletions packages/foam-vscode/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ module.exports = {
// moduleNameMapper: {},

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

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

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

// Run tests from one or more projects
// projects: undefined,
Expand Down Expand Up @@ -126,13 +126,13 @@ module.exports = {
// setupFiles: [],

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

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

// The test environment that will be used for testing
testEnvironment: "node"
testEnvironment: 'node',

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

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

// This option allows the use of a custom results processor
// testResultsProcessor: undefined,
Expand Down
4 changes: 2 additions & 2 deletions packages/foam-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@
"build": "tsc -p ./",
"pretest": "yarn build",
"test": "node ./out/test/run-tests.js",
"pretest:unit": "yarn build",
"test:unit": "node ./out/test/run-tests.js --unit",
"pretest:e2e": "yarn build",
"test:e2e": "node ./out/test/run-tests.js --e2e",
"lint": "tsdx lint src",
"clean": "rimraf out",
Expand Down Expand Up @@ -388,12 +390,10 @@
"@types/vscode": "^1.47.1",
"@typescript-eslint/eslint-plugin": "^2.30.0",
"@typescript-eslint/parser": "^2.30.0",
"babel-jest": "^26.2.2",
"eslint": "^6.8.0",
"eslint-plugin-import": "^2.24.2",
"husky": "^4.2.5",
"jest": "^26.2.2",
"jest-environment-vscode": "^1.0.0",
"jest-extended": "^0.11.5",
"markdown-it": "^12.0.4",
"rimraf": "^3.0.2",
Expand Down
12 changes: 2 additions & 10 deletions packages/foam-vscode/src/test/run-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import fs from 'fs';
import os from 'os';
import path from 'path';
import { runTests } from 'vscode-test';
import { runUnit } from './suite-unit';
Expand Down Expand Up @@ -35,23 +33,17 @@ async function main() {
// The path to the extension test script
// Passed to --extensionTestsPath
const extensionTestsPath = path.join(__dirname, 'suite');
const tmpWorkspaceDir = fs.mkdtempSync(path.join(os.tmpdir(), 'foam-'));

// Download VS Code, unzip it and run the integration test
await runTests({
extensionDevelopmentPath,
extensionTestsPath,
launchArgs: [
tmpWorkspaceDir,
path.join(extensionDevelopmentPath, '.test-workspace'),
'--disable-extensions',
'--disable-workspace-trust',
],
// Running the tests with vscode 1.53.0 is causing issues in the output/error stream management,
// which is causing a stack overflow, possibly due to a recursive callback.
// Also see https://github.com/foambubble/foam/pull/479#issuecomment-774167127
// Forcing the version to 1.52.0 solves the problem.
// TODO: to review, further investigate, and roll back this workaround.
version: '1.52.0',
version: '1.60.0',
});
} catch (err) {
console.log('Error occurred while running Foam e2e tests:', err);
Expand Down
18 changes: 7 additions & 11 deletions packages/foam-vscode/src/test/suite-unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,28 @@
* they will make direct use of the vscode API to be invoked as commands, create editors,
* and so on..
*/

/* eslint-disable import/first */

// Set before imports, see https://github.com/facebook/jest/issues/12162
process.env.FORCE_COLOR = '1';
process.env.NODE_ENV = 'test';

import path from 'path';
import { runCLI } from '@jest/core';

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

export function runUnit(): Promise<void> {
process.env.FORCE_COLOR = '1';
process.env.NODE_ENV = 'test';
process.env.BABEL_ENV = 'test';

return new Promise(async (resolve, reject) => {
try {
const { results } = await runCLI(
{
rootDir,
roots: ['<rootDir>/src'],
transform: JSON.stringify({ '^.+\\.ts$': 'ts-jest' }),
runInBand: true,
testRegex: '\\.(test)\\.ts$',
setupFiles: ['<rootDir>/src/test/support/jest-setup.ts'],
setupFilesAfterEnv: ['jest-extended'],
globals: JSON.stringify({
'ts-jest': {
tsconfig: path.join(rootDir, 'tsconfig.json'),
},
}),
testTimeout: 20000,
verbose: false,
silent: false,
Expand Down
33 changes: 19 additions & 14 deletions packages/foam-vscode/src/test/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,46 @@
* and so on..
*/

/* eslint-disable import/first */

// Set before imports, see https://github.com/facebook/jest/issues/12162
process.env.FORCE_COLOR = '1';
process.env.NODE_ENV = 'test';

import path from 'path';
import { runCLI } from '@jest/core';
import { cleanWorkspace } from './test-utils-vscode';

const rootDir = path.resolve(__dirname, '../..');
const rootDir = path.join(__dirname, '../..');

export function run(): Promise<void> {
const errWrite = process.stderr.write;

let remaining = '';
process.stderr.write = (buffer: string) => {
console.log(buffer);
const lines = (remaining + buffer).split('\n');
remaining = lines.pop() as string;
// Trim long lines because some uninformative code dumps will flood the
// console or, worse, be suppressed altogether because of their size.
lines.forEach(l => console.log(l.substr(0, 300)));
return true;
};

// process.on('unhandledRejection', err => {
// throw err;
// });
process.env.FORCE_COLOR = '1';
process.env.NODE_ENV = 'test';
process.env.BABEL_ENV = 'test';

return new Promise(async (resolve, reject) => {
await cleanWorkspace();
try {
const { results } = await runCLI(
{
rootDir,
roots: ['<rootDir>/src'],
transform: JSON.stringify({ '^.+\\.ts$': 'ts-jest' }),
runInBand: true,
testRegex: '\\.(test|spec)\\.ts$',
testEnvironment:
'<rootDir>/src/test/support/extended-vscode-environment.js',
testEnvironment: '<rootDir>/src/test/support/vscode-environment.js',
setupFiles: ['<rootDir>/src/test/support/jest-setup.ts'],
setupFilesAfterEnv: ['jest-extended'],
globals: JSON.stringify({
'ts-jest': {
tsconfig: path.resolve(rootDir, './tsconfig.json'),
},
}),
testTimeout: 30000,
useStderr: true,
verbose: true,
Expand All @@ -71,6 +75,7 @@ export function run(): Promise<void> {
return reject(error);
} finally {
process.stderr.write = errWrite.bind(process.stderr);
await cleanWorkspace();
}
});
}
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
// Based on https://github.com/svsool/vscode-memo/blob/master/src/test/env/ExtendedVscodeEnvironment.js
const VscodeEnvironment = require('jest-environment-vscode');
const NodeEnvironment = require('jest-environment-node');
const vscode = require('vscode');

const initialVscode = vscode;
class ExtendedVscodeEnvironment extends VscodeEnvironment {
class VscodeEnvironment extends NodeEnvironment {
async setup() {
await super.setup();
this.global.vscode = vscode;

// Expose RegExp otherwise document.getWordRangeAtPosition won't work as supposed.
// Implementation of getWordRangeAtPosition uses "instanceof RegExp" which returns false
// due to Jest running tests in the different vm context.
// See https://github.com/nodejs/node-v0.x-archive/issues/1277.
// And also https://github.com/microsoft/vscode-test/issues/37#issuecomment-700167820
this.global.RegExp = RegExp;
this.global.vscode = vscode;

vscode.workspace
.getConfiguration()
.update('foam.edit.linkReferenceDefinitions', 'off');
}

async teardown() {
this.global.vscode = initialVscode;
this.global.vscode = {};
await super.teardown();
}
}

module.exports = ExtendedVscodeEnvironment;
module.exports = VscodeEnvironment;
2 changes: 1 addition & 1 deletion packages/foam-vscode/src/test/test-utils-vscode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { randomString, wait } from './test-utils';
Logger.setLevel('error');

export const cleanWorkspace = async () => {
const files = await vscode.workspace.findFiles('**', '.vscode');
const files = await vscode.workspace.findFiles('**', '{.vscode,.keep}');
await Promise.all(files.map(f => vscode.workspace.fs.delete(f)));
};

Expand Down
7 changes: 1 addition & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2954,7 +2954,7 @@ babel-jest@^24.9.0:
chalk "^2.4.2"
slash "^2.0.0"

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

jest-environment-vscode@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/jest-environment-vscode/-/jest-environment-vscode-1.0.0.tgz#96367fe8531047e64359e0682deafc973bfaea91"
integrity sha512-VKlj5j5pNurFEwWPaDiX1kBgmhWqcJTAZsvEX1x5lh0/+5myjk+qipEs/dPJVRbBPb3XFxiR48XzGn+wOU7SSQ==

jest-extended@^0.11.5:
version "0.11.5"
resolved "https://registry.yarnpkg.com/jest-extended/-/jest-extended-0.11.5.tgz#f063b3f1eaadad8d7c13a01f0dfe0f538d498ccf"
Expand Down

0 comments on commit 8abea48

Please sign in to comment.