This packages is was created to help with the painful process of running unit tests on VS Code extensions.
It was create to support Code Spell Checker.
npm install --save-dev jest-mock-vscode
Based upon Manual Mocks · Jest add the following file to your project:
__mocks__/vscode.js
/* eslint-disable @typescript-eslint/no-require-imports */
module.exports = require('jest-mock-vscode').createVSCodeMock(jest);
import type { WorkspaceFolder } from 'vscode';
import { Uri, workspace } from 'vscode';
const rootUri = Uri.file(__dirname);
const workspaceFolder1: WorkspaceFolder = {
uri: Uri.joinPath(rootUri, 'Folder1'),
name: 'Folder1',
index: 0,
};
const workspaceFolder2: WorkspaceFolder = {
uri: Uri.joinPath(rootUri, 'Folder2'),
name: 'Folder2',
index: 1,
};
describe('workspace', () => {
test('getWorkspaceFolder', () => {
const uri = Uri.joinPath(workspaceFolder1.uri, 'code.test.ts');
const uri2 = Uri.joinPath(workspaceFolder2.uri, 'test.txt');
const spy = jest.spyOn(workspace, 'workspaceFolders', 'get');
spy.mockReturnValue([workspaceFolder1, workspaceFolder2]);
expect(workspace.workspaceFolders).toEqual([workspaceFolder1, workspaceFolder2]);
expect(workspace.getWorkspaceFolder(uri)).toEqual(workspaceFolder1);
expect(workspace.getWorkspaceFolder(uri2)).toEqual(workspaceFolder2);
});
});
Based upon:
You can add the following file to your project:
__mocks__/vscode.cts
import { createVSCodeMock } from 'jest-mock-vscode';
import { vi } from 'vitest';
const vscode = createVSCodeMock(vi);
module.exports = vscode;
import { afterEach, describe, expect, test, vi } from 'vitest';
import { Uri, window, workspace, type WorkspaceFolder } from 'vscode';
vi.mock('vscode');
const testFileUri = Uri.file(__filename);
const rootUri = Uri.file(__dirname);
const workspaceFolder1: WorkspaceFolder = {
uri: Uri.joinPath(rootUri, 'Folder1'),
name: 'Folder1',
index: 0,
};
const workspaceFolder2: WorkspaceFolder = {
uri: Uri.joinPath(rootUri, 'Folder2'),
name: 'Folder2',
index: 1,
};
describe('vscode.workspace', () => {
afterEach(() => {
vi.resetAllMocks();
});
test('getWorkspaceFolder', () => {
const uri = Uri.joinPath(workspaceFolder1.uri, 'code.test.ts');
const uri2 = Uri.joinPath(workspaceFolder2.uri, 'test.txt');
const spy = vi.spyOn(workspace, 'workspaceFolders', 'get');
spy.mockReturnValue([workspaceFolder1, workspaceFolder2]);
expect(workspace.workspaceFolders).toEqual([workspaceFolder1, workspaceFolder2]);
expect(workspace.getWorkspaceFolder(uri)).toEqual(workspaceFolder1);
expect(workspace.getWorkspaceFolder(uri2)).toEqual(workspaceFolder2);
});
test('openTextDocument', async () => {
const uri = testFileUri;
const doc = await workspace.openTextDocument(uri);
expect(doc.uri).toEqual(uri);
expect(doc.getText()).toContain("vi.mock('vscode');");
});
});
describe('vscode.window', () => {
afterEach(() => {
vi.resetAllMocks();
});
test('showTextDocument', async () => {
const uri = testFileUri;
const doc = await workspace.openTextDocument(uri);
const editor = await window.showTextDocument(doc);
expect(editor.document).toBe(doc);
});
});
import { afterEach, describe, expect, test, vi } from 'vitest';
import { Uri, workspace, type WorkspaceFolder } from 'vscode';
vi.mock('vscode', async () => (await import('jest-mock-vscode')).createVSCodeMock(vi));
describe(/* ... */);
import { readTextDocument } from 'jest-mock-vscode';
import { Uri } from 'vscode';
// vi.mock('vscode')
const pathToFixture = __filename;
const doc = await readTextDocument(Uri.file(pathToFixture));
Here are a few of items that have been implemented. Most of them are based upon the code from VS Code.
- CodeAction
- ColorTheme
- CompletionItem
- Diagnostic
- language
- MarkdownString
- Position
- Range
- Selection
- TaskGroup
- TextDocument
- TextEditor
- Uri
- workspace
- WorkspaceEdit
- No migration should be necessary.
@types/vscode
is now a peer dependency
Migration is only necessary if you used any of the following:
new MockWorkspace(...)
new MockTabGroups(...)
new MockTextEditor(...)
It is now necessary to use a create function.
new MockWorkspace(...)
->createWorkspace(...)
new MockTabGroups(...)
->createMockTabGroups(...)
new MockTextEditor(...)
->createMockTextEditor(...)
__mocks__/vscode.js
-module.exports = require('jest-mock-vscode');
+module.exports = require('jest-mock-vscode').createVSCodeMock(jest);
The idea is to use as much of the VS Code as possible. For example the vscode-uri
is used to implement the Uri
class. If Microsoft publishes a set of core classes, they will be used.
- Code copied from VS Code belongs to Microsoft.
- All other code falls under the MIT License