).mockReturnValue(mockPanel);
+ });
+ describe('create a regexp from test names', () => {
+ it.each`
+ testName | regString
+ ${'simple name'} | ${'simple name'}
+ ${'with $name'} | ${'with \\$name'}
+ ${'a string.with.dots*'} | ${'a string\\.with\\.dots\\*'}
+ ${'title
'} | ${'title<\\/p>'}
+ `('$testName', async ({ testName, regString }) => {
+ mockSnapshot.getSnapshotContent.mockReturnValue(Promise.resolve(undefined));
+ const provider = new SnapshotProvider();
+ await provider.previewSnapshot('whatever', testName);
+ expect(mockSnapshot.getSnapshotContent).toHaveBeenCalledWith(
+ 'whatever',
+ new RegExp(`^${regString} [0-9]+$`)
+ );
+ });
+ });
+ it('display content in a WebviewPanel', async () => {
+ const content1 = { 'some test': ' result' };
+ const content2 = ' "some quoted text"';
+ const content3 = { '3rd test': " 'single quote' & this" };
+ mockSnapshot.getSnapshotContent
+ .mockReturnValueOnce(Promise.resolve(content1))
+ .mockReturnValueOnce(Promise.resolve(content2))
+ .mockReturnValueOnce(Promise.resolve(content3));
+
+ const provider = new SnapshotProvider();
+ await provider.previewSnapshot('test-file', 'some test');
+ expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1);
+ expect(mockPanel.onDidDispose).toHaveBeenCalled();
+ expect(mockPanel.webview.html).toMatchInlineSnapshot(`"<test 1> result
"`);
+ expect(mockPanel.title).toEqual(expect.stringContaining('some test'));
+
+ //2nd time showing the content will reuse the panel
+ (vscode.window.createWebviewPanel as jest.Mocked).mockClear();
+ await provider.previewSnapshot('test-file', 'some other test');
+ expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(0);
+ expect(mockPanel.webview.html).toMatchInlineSnapshot(
+ `"<test 2> "some quoted text"
"`
+ );
+ expect(mockPanel.title).toEqual(expect.stringContaining('some other test'));
+
+ //if user close the panel, it will be recreated on the next request
+ const callback = mockPanel.onDidDispose.mock.calls[0][0];
+ callback();
+ await provider.previewSnapshot('test-file', '3rd test');
+ expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1);
+ expect(mockPanel.webview.html).toMatchInlineSnapshot(
+ `"<test 3> 'single quote' & this
"`
+ );
+ expect(mockPanel.title).toEqual(expect.stringContaining('3rd test'));
+ });
+ it('show warning if no content is found', async () => {
+ mockSnapshot.getSnapshotContent.mockReturnValueOnce(undefined);
+ const provider = new SnapshotProvider();
+ await provider.previewSnapshot('test-file', 'some test');
+ expect(vscode.window.showErrorMessage).toHaveBeenCalled();
+ expect(vscode.window.createWebviewPanel).not.toHaveBeenCalled();
+
+ (vscode.window.showErrorMessage as jest.Mocked).mockClear();
+
+ mockSnapshot.getSnapshotContent.mockReturnValueOnce({});
+ await provider.previewSnapshot('test-file', 'some test');
+ expect(vscode.window.showErrorMessage).toHaveBeenCalled();
+ expect(vscode.window.createWebviewPanel).not.toHaveBeenCalled();
+ });
+ it('can show multiple snapshots within a test', async () => {
+ const content = {
+ 'test 1': 'test 1 content',
+ 'test 2': 'test 2 content',
+ };
+ mockSnapshot.getSnapshotContent.mockReturnValueOnce(Promise.resolve(content));
+ const provider = new SnapshotProvider();
+ await provider.previewSnapshot('test-file', 'some test');
+ expect(vscode.window.createWebviewPanel).toHaveBeenCalledTimes(1);
+ expect(mockPanel.onDidDispose).toHaveBeenCalled();
+ expect(mockPanel.webview.html).toMatchInlineSnapshot(
+ `"test 1
test 1 content
test 2
test 2 content
"`
+ );
+ expect(mockPanel.title).toEqual(expect.stringContaining('some test'));
+ });
+ });
+});
diff --git a/tests/extension.test.ts b/tests/extension.test.ts
index a58c9b67..17b32270 100644
--- a/tests/extension.test.ts
+++ b/tests/extension.test.ts
@@ -15,11 +15,6 @@ jest.mock('../src/Coverage', () => ({
CoverageCodeLensProvider: jest.fn().mockReturnValue({}),
}));
-jest.mock('../src/SnapshotCodeLens', () => ({
- registerSnapshotCodeLens: jest.fn(() => []),
- registerSnapshotPreview: jest.fn(() => []),
-}));
-
jest.mock('../src/test-provider/test-item-context-manager', () => ({
tiContextManager: { registerCommands: jest.fn(() => []) },
}));
diff --git a/tests/extensionManager.test.ts b/tests/extensionManager.test.ts
index 1d1ead8e..e24c0c41 100644
--- a/tests/extensionManager.test.ts
+++ b/tests/extensionManager.test.ts
@@ -73,6 +73,7 @@ const makeJestExt = (workspace: vscode.WorkspaceFolder): any => {
toggleAutoRun: jest.fn(),
toggleCoverageOverlay: jest.fn(),
enableLoginShell: jest.fn(),
+ runItemCommand: jest.fn(),
workspace,
};
};
@@ -700,6 +701,7 @@ describe('ExtensionManager', () => {
${'with-workspace.toggle-auto-run'} | ${'toggleAutoRun'}
${'with-workspace.toggle-coverage'} | ${'toggleCoverageOverlay'}
${'with-workspace.enable-login-shell'} | ${'enableLoginShell'}
+ ${'with-workspace.item-command'} | ${'runItemCommand'}
`('extension-based commands "$name"', async ({ name, extFunc }) => {
extensionManager.register();
const expectedName = `${extensionName}.${name}`;
diff --git a/tests/test-helper.ts b/tests/test-helper.ts
index 7913f200..057caa33 100644
--- a/tests/test-helper.ts
+++ b/tests/test-helper.ts
@@ -19,7 +19,7 @@ export const makeLocation = (pos: [number, number]): Location => ({
line: pos[0],
column: pos[1],
});
-export const makePositionRange = (pos: [number, number, number, number]) => ({
+export const makePositionRange = (pos: [number, number, number, number]): any => ({
start: makeLocation([pos[0], pos[1]]),
end: makeLocation([pos[2], pos[3]]),
});
@@ -38,6 +38,16 @@ export const findResultForTest = (results: TestResult[], itBlock: ItBlock): Test
};
// factory method
+export const makeSnapshotBlock = (marker?: string, isInline?: boolean, line?: number): any => {
+ const loc = line ? makePositionRange([line, 0, line, 0]) : EmptyLocationRange;
+ return {
+ marker,
+ isInline: isInline ?? false,
+ node: { name: 'node', loc },
+ parents: [],
+ };
+};
+
export const makeItBlock = (
name?: string,
pos?: [number, number, number, number],
@@ -136,7 +146,7 @@ export const mockProjectWorkspace = (...args: any[]): any => {
export const mockWworkspaceLogging = (): any => ({ create: () => jest.fn() });
-export const mockEvent = () => ({
+export const mockEvent = (): any => ({
event: jest.fn().mockReturnValue({ dispose: jest.fn() }),
fire: jest.fn(),
dispose: jest.fn(),
diff --git a/tests/test-provider/test-helper.ts b/tests/test-provider/test-helper.ts
index 0f83b572..74720060 100644
--- a/tests/test-provider/test-helper.ts
+++ b/tests/test-provider/test-helper.ts
@@ -29,7 +29,7 @@ export const mockExtExplorerContext = (wsName = 'ws-1', override: any = {}): any
loggingFactory: { create: jest.fn().mockReturnValue(jest.fn()) },
session: { scheduleProcess: jest.fn() },
workspace: { name: wsName, uri: { fsPath: `/${wsName}` } },
- testResolveProvider: {
+ testResultProvider: {
events: {
testListUpdated: { event: jest.fn().mockReturnValue({ dispose: jest.fn() }) },
testSuiteChanged: { event: jest.fn().mockReturnValue({ dispose: jest.fn() }) },
@@ -37,6 +37,7 @@ export const mockExtExplorerContext = (wsName = 'ws-1', override: any = {}): any
getTestList: jest.fn().mockReturnValue([]),
isTestFile: jest.fn().mockReturnValue('yes'),
getTestSuiteResult: jest.fn().mockReturnValue({}),
+ previewSnapshot: jest.fn(),
},
debugTests: jest.fn(),
sessionEvents: mockJestExtEvents(),
@@ -70,12 +71,12 @@ export const mockController = (): any => {
return run;
}),
dispose: jest.fn(),
- createRunProfile: jest.fn().mockImplementation((label, kind, runHandler, isDefault, tags) => ({
+ createRunProfile: jest.fn().mockImplementation((label, kind, runHandler, isDefault, tag) => ({
label,
kind,
runHandler,
isDefault,
- tags: tags ?? [],
+ tag,
})),
createTestItem: jest.fn().mockImplementation((id, label, uri) => {
const item: any = {
diff --git a/tests/test-provider/test-item-context-manager.test.ts b/tests/test-provider/test-item-context-manager.test.ts
index 6c90ff2a..9fd7e235 100644
--- a/tests/test-provider/test-item-context-manager.test.ts
+++ b/tests/test-provider/test-item-context-manager.test.ts
@@ -4,74 +4,127 @@ jest.unmock('../../src/appGlobals');
import * as vscode from 'vscode';
import { extensionName } from '../../src/appGlobals';
import { TestItemContextManager } from '../../src/test-provider/test-item-context-manager';
+import { ItemCommand } from '../../src/test-provider/types';
describe('TestItemContextManager', () => {
beforeEach(() => {
jest.resetAllMocks();
});
describe('can set itemContext', () => {
- it.each`
- case | context | withItemKey | withoutItemKey
- ${1} | ${{ key: 'jest.autoRun', value: true, itemIds: ['a'] }} | ${'jest.autoRun.on'} | ${'jest.autoRun.off'}
- ${2} | ${{ key: 'jest.autoRun', value: false, itemIds: ['a'] }} | ${'jest.autoRun.off'} | ${'jest.autoRun.on'}
- ${2} | ${{ key: 'jest.coverage', value: true, itemIds: ['a'] }} | ${'jest.coverage.on'} | ${'jest.coverage.off'}
- ${2} | ${{ key: 'jest.coverage', value: false, itemIds: ['a'] }} | ${'jest.coverage.off'} | ${'jest.coverage.on'}
- `('case $case: setContext for $expectedKey', ({ context, withItemKey, withoutItemKey }) => {
- const workspace: any = { name: 'ws' };
- const manager = new TestItemContextManager();
- manager.setItemContext({ workspace, ...context });
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
- 'setContext',
- withItemKey,
- context.itemIds
- );
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith('setContext', withoutItemKey, []);
+ describe('jest.autoRun and jest.coverage', () => {
+ it.each`
+ case | context | withItemKey | withoutItemKey
+ ${1} | ${{ key: 'jest.autoRun', value: true, itemIds: ['a'] }} | ${'jest.autoRun.on'} | ${'jest.autoRun.off'}
+ ${2} | ${{ key: 'jest.autoRun', value: false, itemIds: ['a'] }} | ${'jest.autoRun.off'} | ${'jest.autoRun.on'}
+ ${3} | ${{ key: 'jest.coverage', value: true, itemIds: ['a'] }} | ${'jest.coverage.on'} | ${'jest.coverage.off'}
+ ${4} | ${{ key: 'jest.coverage', value: false, itemIds: ['a'] }} | ${'jest.coverage.off'} | ${'jest.coverage.on'}
+ `('case $case: setContext for $expectedKey', ({ context, withItemKey, withoutItemKey }) => {
+ const workspace: any = { name: 'ws' };
+ const manager = new TestItemContextManager();
+ manager.setItemContext({ workspace, ...context });
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ withItemKey,
+ context.itemIds
+ );
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ withoutItemKey,
+ []
+ );
+ });
+ it('can manage itemContext for multiple workspaces', () => {
+ const ws1: any = { name: 'ws1' };
+ const ws2: any = { name: 'ws2' };
+ const manager = new TestItemContextManager();
+ manager.setItemContext({
+ workspace: ws1,
+ key: 'jest.autoRun',
+ value: true,
+ itemIds: ['a', 'b'],
+ });
+ manager.setItemContext({
+ workspace: ws2,
+ key: 'jest.autoRun',
+ value: true,
+ itemIds: ['c'],
+ });
+ manager.setItemContext({
+ workspace: ws2,
+ key: 'jest.autoRun',
+ value: false,
+ itemIds: ['d'],
+ });
+ manager.setItemContext({
+ workspace: ws2,
+ key: 'jest.coverage',
+ value: true,
+ itemIds: ['c'],
+ });
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.autoRun.on',
+ ['a', 'b', 'c']
+ );
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.autoRun.off',
+ ['d']
+ );
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.coverage.on',
+ ['c']
+ );
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.coverage.off',
+ []
+ );
+ });
});
- it('can manage itemContext for multiple workspaces', () => {
- const ws1: any = { name: 'ws1' };
- const ws2: any = { name: 'ws2' };
- const manager = new TestItemContextManager();
- manager.setItemContext({
- workspace: ws1,
- key: 'jest.autoRun',
- value: true,
- itemIds: ['a', 'b'],
+ describe('jest.editor-view-snapshot', () => {
+ it('can set context with itemId and onClick action', () => {
+ const workspace: any = { name: 'ws' };
+ const manager = new TestItemContextManager();
+ const context: any = {
+ workspace,
+ key: 'jest.editor-view-snapshot',
+ itemIds: ['a'],
+ onClick: jest.fn(),
+ };
+ manager.setItemContext(context);
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.editor-view-snapshot',
+ context.itemIds
+ );
});
- manager.setItemContext({ workspace: ws2, key: 'jest.autoRun', value: true, itemIds: ['c'] });
- manager.setItemContext({ workspace: ws2, key: 'jest.autoRun', value: false, itemIds: ['d'] });
- manager.setItemContext({
- workspace: ws2,
- key: 'jest.coverage',
- value: true,
- itemIds: ['c'],
+ it('new context will override the olde one', () => {
+ const workspace: any = { name: 'ws' };
+ const manager = new TestItemContextManager();
+ const context1: any = {
+ workspace,
+ key: 'jest.editor-view-snapshot',
+ itemIds: ['a'],
+ onClick: jest.fn(),
+ };
+ const context2 = { ...context1, itemIds: ['b'] };
+ manager.setItemContext(context1);
+ manager.setItemContext(context2);
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ 'setContext',
+ 'jest.editor-view-snapshot',
+ context2.itemIds
+ );
});
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith('setContext', 'jest.autoRun.on', [
- 'a',
- 'b',
- 'c',
- ]);
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
- 'setContext',
- 'jest.autoRun.off',
- ['d']
- );
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
- 'setContext',
- 'jest.coverage.on',
- ['c']
- );
- expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
- 'setContext',
- 'jest.coverage.off',
- []
- );
});
});
describe('can register item menu commands', () => {
it('toggle-autoRun menu commands', () => {
const manager = new TestItemContextManager();
const disposableList = manager.registerCommands();
- expect(disposableList).toHaveLength(4);
+ expect(disposableList.length).toBeGreaterThanOrEqual(4);
const commands = [
`${extensionName}.test-item.auto-run.toggle-on`,
@@ -99,7 +152,7 @@ describe('TestItemContextManager', () => {
it('toggle-coverage menu commands', () => {
const manager = new TestItemContextManager();
const disposableList = manager.registerCommands();
- expect(disposableList).toHaveLength(4);
+ expect(disposableList.length).toBeGreaterThanOrEqual(4);
const commands = [
`${extensionName}.test-item.coverage.toggle-on`,
@@ -124,5 +177,51 @@ describe('TestItemContextManager', () => {
expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith(extCmd, workspace);
});
});
+ describe('snapshot menu commands', () => {
+ it.each`
+ contextId | contextCommand | itemCommand
+ ${'jest.editor-view-snapshot'} | ${'test-item.view-snapshot'} | ${ItemCommand.viewSnapshot}
+ ${'jest.editor-update-snapshot'} | ${'test-item.update-snapshot'} | ${ItemCommand.updateSnapshot}
+ `('$contextId', ({ contextId, contextCommand, itemCommand }) => {
+ const manager = new TestItemContextManager();
+ const disposableList = manager.registerCommands();
+ expect(disposableList.length).toBeGreaterThanOrEqual(6);
+
+ const calls = (vscode.commands.registerCommand as jest.Mocked).mock.calls.filter(
+ (call) => call[0] === `${extensionName}.${contextCommand}`
+ );
+
+ expect(calls).toHaveLength(1);
+
+ // set some itemContext then trigger the menu
+ const workspace: any = { name: 'ws' };
+ const context: any = {
+ workspace,
+ key: contextId,
+ itemIds: ['a'],
+ };
+ manager.setItemContext(context);
+ const callBack = calls[0][1];
+ const testItem = { id: 'a' };
+ callBack(testItem);
+ const extCmd = `${extensionName}.with-workspace.item-command`;
+ expect(vscode.commands.executeCommand).toHaveBeenCalledWith(
+ extCmd,
+ workspace,
+ testItem,
+ itemCommand
+ );
+
+ (vscode.commands.executeCommand as jest.Mocked).mockClear();
+
+ callBack({ id: 'b' });
+ expect(vscode.commands.executeCommand).not.toHaveBeenCalledWith(
+ extCmd,
+ workspace,
+ testItem,
+ itemCommand
+ );
+ });
+ });
});
});
diff --git a/tests/test-provider/test-item-data.test.ts b/tests/test-provider/test-item-data.test.ts
index 60c5df63..dc4f2812 100644
--- a/tests/test-provider/test-item-data.test.ts
+++ b/tests/test-provider/test-item-data.test.ts
@@ -8,6 +8,7 @@ jest.unmock('./test-helper');
jest.unmock('../../src/errors');
import { JestTestRun } from '../../src/test-provider/test-provider-helper';
+import { tiContextManager } from '../../src/test-provider/test-item-context-manager';
jest.mock('path', () => {
let sep = '/';
@@ -43,6 +44,7 @@ import {
import * as path from 'path';
import { mockController, mockExtExplorerContext } from './test-helper';
import * as errors from '../../src/errors';
+import { ItemCommand } from '../../src/test-provider/types';
const mockPathSep = (newSep: string) => {
(path as jest.Mocked).setSep(newSep);
@@ -95,12 +97,12 @@ describe('test-item-data', () => {
message: 'test file failed',
assertionContainer,
};
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue(testSuiteResult);
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue(testSuiteResult);
};
const setupTestEnv = () => {
const file = '/ws-1/tests/a.test.ts';
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce([file]);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce([file]);
const wsRoot = new WorkspaceRoot(context);
const onRunEvent = context.ext.sessionEvents.onRunEvent.event.mock.calls[0][0];
@@ -108,7 +110,7 @@ describe('test-item-data', () => {
prepareTestResult();
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process: { id: 'whatever', request: { type: 'watch-tests' } },
files: [file],
@@ -142,9 +144,23 @@ describe('test-item-data', () => {
return { wsRoot, folder, testFile, testBlock, onRunEvent, scheduleItem, file };
};
+ const createAllTestItems = () => {
+ const wsRoot = new WorkspaceRoot(context);
+ const folder = new FolderData(context, 'dir', wsRoot.item);
+ const uri: any = { fsPath: 'whatever' };
+ const doc = new TestDocumentRoot(context, uri, folder.item);
+ const node: any = { fullName: 'a test', attrs: {}, data: {} };
+ const testItem = new TestData(context, uri, node, doc.item);
+ return { wsRoot, folder, doc, testItem };
+ };
+
beforeEach(() => {
controllerMock = mockController();
- const profiles: any = [{ tag: { id: 'run' } }, { tag: { id: 'debug' } }];
+ const profiles: any = [
+ { tag: { id: 'run' } },
+ { tag: { id: 'debug' } },
+ { tag: { id: 'update-snapshot' } },
+ ];
context = new JestTestProviderContext(mockExtExplorerContext('ws-1'), controllerMock, profiles);
context.output.write = jest.fn((t) => t);
[jestRun, runEndSpy, runMock] = createTestRun();
@@ -153,6 +169,7 @@ describe('test-item-data', () => {
.fn()
.mockImplementation((uri, p) => ({ fsPath: `${uri.fsPath}/${p}` }));
vscode.Uri.file = jest.fn().mockImplementation((f) => ({ fsPath: f }));
+ (tiContextManager.setItemContext as jest.Mocked).mockClear();
});
describe('discover children', () => {
describe('WorkspaceRoot', () => {
@@ -162,7 +179,7 @@ describe('test-item-data', () => {
'/ws-1/src/b.test.ts',
'/ws-1/src/app/app.test.ts',
];
- context.ext.testResolveProvider.getTestList.mockReturnValue(testFiles);
+ context.ext.testResultProvider.getTestList.mockReturnValue(testFiles);
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
@@ -195,7 +212,7 @@ describe('test-item-data', () => {
});
describe('when no testFiles yet', () => {
it('if no testFiles yet, will still turn off canResolveChildren and close the run', () => {
- context.ext.testResolveProvider.getTestList.mockReturnValue([]);
+ context.ext.testResultProvider.getTestList.mockReturnValue([]);
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
expect(wsRoot.item.children.size).toEqual(0);
@@ -204,7 +221,7 @@ describe('test-item-data', () => {
});
it('will not wipe out existing test items', () => {
// first time discover 1 file
- context.ext.testResolveProvider.getTestList.mockReturnValue(['/ws-1/a.test.ts']);
+ context.ext.testResultProvider.getTestList.mockReturnValue(['/ws-1/a.test.ts']);
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
expect(jestRun.isClosed()).toBeTruthy();
@@ -213,7 +230,7 @@ describe('test-item-data', () => {
expect(runEndSpy).toHaveBeenCalledTimes(1);
// 2nd time if no test-file: testItems will not change
- context.ext.testResolveProvider.getTestList.mockReturnValue([]);
+ context.ext.testResultProvider.getTestList.mockReturnValue([]);
[jestRun, runEndSpy] = createTestRun();
wsRoot.discoverTest(jestRun);
expect(jestRun.isClosed()).toBeTruthy();
@@ -226,8 +243,8 @@ describe('test-item-data', () => {
const a1 = helper.makeAssertion('test-1', 'KnownSuccess', [], [1, 0]);
const assertionContainer = buildAssertionContainer([a1]);
const testFiles = ['/ws-1/a.test.ts'];
- context.ext.testResolveProvider.getTestList.mockReturnValue(testFiles);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestList.mockReturnValue(testFiles);
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownSuccess',
assertionContainer,
});
@@ -235,14 +252,14 @@ describe('test-item-data', () => {
wsRoot.discoverTest(jestRun);
const docItem = wsRoot.item.children.get(testFiles[0]);
expect(docItem.children.size).toEqual(0);
- expect(context.ext.testResolveProvider.getTestSuiteResult).toHaveBeenCalled();
+ expect(context.ext.testResultProvider.getTestSuiteResult).toHaveBeenCalled();
});
it('will remove folder item if no test file exist any more', () => {
const a1 = helper.makeAssertion('test-1', 'KnownSuccess', [], [1, 0]);
const assertionContainer = buildAssertionContainer([a1]);
const testFiles = ['/ws-1/tests1/a.test.ts', '/ws-1/tests2/b.test.ts'];
- context.ext.testResolveProvider.getTestList.mockReturnValue(testFiles);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestList.mockReturnValue(testFiles);
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownSuccess',
assertionContainer,
});
@@ -285,15 +302,15 @@ describe('test-item-data', () => {
});
it('register for test result events', () => {
new WorkspaceRoot(context);
- expect(context.ext.testResolveProvider.events.testListUpdated.event).toHaveBeenCalled();
- expect(context.ext.testResolveProvider.events.testSuiteChanged.event).toHaveBeenCalled();
+ expect(context.ext.testResultProvider.events.testListUpdated.event).toHaveBeenCalled();
+ expect(context.ext.testResultProvider.events.testSuiteChanged.event).toHaveBeenCalled();
});
it('unregister events upon dispose', () => {
const wsRoot = new WorkspaceRoot(context);
const listeners = [
- context.ext.testResolveProvider.events.testListUpdated.event.mock.results[0].value,
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.results[0].value,
+ context.ext.testResultProvider.events.testListUpdated.event.mock.results[0].value,
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.results[0].value,
context.ext.sessionEvents.onRunEvent.event.mock.results[0].value,
];
wsRoot.dispose();
@@ -302,12 +319,12 @@ describe('test-item-data', () => {
describe('when testFile list is changed', () => {
it('testListUpdated event will be fired', () => {
const wsRoot = new WorkspaceRoot(context);
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce([]);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce([]);
wsRoot.discoverTest(jestRun);
expect(wsRoot.item.children.size).toBe(0);
// invoke testListUpdated event listener
- context.ext.testResolveProvider.events.testListUpdated.event.mock.calls[0][0]([
+ context.ext.testResultProvider.events.testListUpdated.event.mock.calls[0][0]([
'/ws-1/a.test.ts',
]);
// should have created a new run
@@ -322,7 +339,7 @@ describe('test-item-data', () => {
});
describe('when testSuiteChanged.assertions-updated event filed', () => {
it('all item data will be updated accordingly', () => {
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce([]);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce([]);
context.ext.settings = {
testExplorer: { enabled: true, showInlineError: true },
autoRun: {},
@@ -343,10 +360,10 @@ describe('test-item-data', () => {
message: 'test file failed',
assertionContainer,
};
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue(testSuiteResult);
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue(testSuiteResult);
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process: { id: 'whatever', request: { type: 'watch-tests' } },
files: ['/ws-1/a.test.ts'],
@@ -374,27 +391,27 @@ describe('test-item-data', () => {
});
});
describe('when testSuiteChanged.result-matched event fired', () => {
- it('test data range will be updated accordingly', () => {
+ it('test data range and snapshot context will be updated accordingly', () => {
// assertion should be discovered prior
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce(['/ws-1/a.test.ts']);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce(['/ws-1/a.test.ts']);
const a1 = helper.makeAssertion('test-a', 'KnownFail', ['desc-1'], [1, 0]);
const assertionContainer = buildAssertionContainer([a1]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
- expect(context.ext.testResolveProvider.getTestSuiteResult).toHaveBeenCalledTimes(1);
+ expect(context.ext.testResultProvider.getTestSuiteResult).toHaveBeenCalledTimes(1);
expect(wsRoot.item.children.size).toBe(1);
const docItem = getChildItem(wsRoot.item, 'a.test.ts');
expect(docItem.children.size).toEqual(0);
// after jest test run, result suite should be updated and test block should be populated
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process: { id: 'whatever', request: { type: 'watch-tests' } },
files: ['/ws-1/a.test.ts'],
@@ -405,9 +422,9 @@ describe('test-item-data', () => {
const tItem = getChildItem(dItem, 'test-a');
expect(tItem.range).toEqual({ args: [1, 0, 1, 0] });
- expect(context.ext.testResolveProvider.getTestSuiteResult).toHaveBeenCalled();
+ expect(context.ext.testResultProvider.getTestSuiteResult).toHaveBeenCalled();
controllerMock.createTestRun.mockClear();
- context.ext.testResolveProvider.getTestSuiteResult.mockClear();
+ context.ext.testResultProvider.getTestSuiteResult.mockClear();
// after match, the assertion nodes would have updated range
const descNode = assertionContainer.childContainers[0];
@@ -420,16 +437,18 @@ describe('test-item-data', () => {
start: { line: 2, column: 2 },
end: { line: 10, column: 4 },
};
+ // add snapshot marker
+ testNode.attrs.snapshot = 'inline';
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'result-matched',
file: '/ws-1/a.test.ts',
});
// no run should be created as we are not changing any test item tree
expect(controllerMock.createTestRun).not.toHaveBeenCalled();
- expect(context.ext.testResolveProvider.getTestSuiteResult).not.toHaveBeenCalled();
+ expect(context.ext.testResultProvider.getTestSuiteResult).not.toHaveBeenCalled();
// expect the item's range has picked up the updated nodes
expect(dItem.range).toEqual({
@@ -448,41 +467,75 @@ describe('test-item-data', () => {
testNode.attrs.range.end.column,
],
});
+
+ // snapshot menu context is populated
+ expect(tiContextManager.setItemContext).toHaveBeenCalledTimes(2);
+ expect(tiContextManager.setItemContext).toHaveBeenCalledWith(
+ expect.objectContaining({
+ key: 'jest.editor-update-snapshot',
+ itemIds: [tItem.id],
+ })
+ );
+ expect(tiContextManager.setItemContext).toHaveBeenCalledWith(
+ expect.objectContaining({
+ key: 'jest.editor-view-snapshot',
+ itemIds: [],
+ })
+ );
});
});
describe('when testSuiteChanged.test-parsed event filed', () => {
- it('test items will be added based on parsed test files (test blocks)', () => {
+ it('test items will be added and snapshot context updated accordingly', () => {
// assertion should be discovered prior
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce(['/ws-1/a.test.ts']);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce(['/ws-1/a.test.ts']);
const t1 = helper.makeItBlock('test-1', [1, 1, 5, 1]);
const t2 = helper.makeItBlock('test-2', [6, 1, 7, 1]);
const sourceRoot = helper.makeRoot([t2, t1]);
- const testContainer = buildSourceContainer(sourceRoot);
+ const sourceContainer = buildSourceContainer(sourceRoot);
+ const node1 = sourceContainer.childData.find((child) => child.fullName === 'test-1');
+ const node2 = sourceContainer.childData.find((child) => child.fullName === 'test-2');
+ node1.attrs = { ...node1.attrs, snapshot: 'external' };
+ node2.attrs = { ...node2.attrs, snapshot: 'external', nonLiteralName: true };
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
- expect(context.ext.testResolveProvider.getTestSuiteResult).toHaveBeenCalledTimes(1);
+ expect(context.ext.testResultProvider.getTestSuiteResult).toHaveBeenCalledTimes(1);
expect(wsRoot.item.children.size).toBe(1);
const docItem = getChildItem(wsRoot.item, 'a.test.ts');
expect(docItem.children.size).toEqual(0);
controllerMock.createTestRun.mockClear();
- context.ext.testResolveProvider.getTestSuiteResult.mockClear();
+ context.ext.testResultProvider.getTestSuiteResult.mockClear();
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'test-parsed',
file: '/ws-1/a.test.ts',
- testContainer,
+ sourceContainer,
});
expect(docItem.children.size).toEqual(2);
- let dItem = getChildItem(docItem, 'test-1');
- expect(dItem.range).toEqual({ args: [0, 0, 4, 0] });
- dItem = getChildItem(docItem, 'test-2');
- expect(dItem.range).toEqual({ args: [5, 0, 6, 0] });
+ const dItem1 = getChildItem(docItem, 'test-1');
+ expect(dItem1.range).toEqual({ args: [0, 0, 4, 0] });
+ const dItem2 = getChildItem(docItem, 'test-2');
+ expect(dItem2.range).toEqual({ args: [5, 0, 6, 0] });
- expect(context.ext.testResolveProvider.getTestSuiteResult).not.toHaveBeenCalled();
+ expect(context.ext.testResultProvider.getTestSuiteResult).not.toHaveBeenCalled();
expect(controllerMock.createTestRun).not.toHaveBeenCalled();
+
+ // snapshot menu context is populated for "test-1" only
+ expect(tiContextManager.setItemContext).toHaveBeenCalledTimes(2);
+ expect(tiContextManager.setItemContext).toHaveBeenCalledWith(
+ expect.objectContaining({
+ key: 'jest.editor-view-snapshot',
+ itemIds: [dItem1.id],
+ })
+ );
+ expect(tiContextManager.setItemContext).toHaveBeenCalledWith(
+ expect.objectContaining({
+ key: 'jest.editor-update-snapshot',
+ itemIds: [dItem1.id],
+ })
+ );
});
});
});
@@ -493,18 +546,18 @@ describe('test-item-data', () => {
const t1 = helper.makeItBlock('test-1', [1, 1, 5, 1]);
const t2 = helper.makeItBlock('test-2', [6, 1, 7, 1]);
const sourceRoot = helper.makeRoot([t2, t1]);
- const testContainer = buildSourceContainer(sourceRoot);
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ const sourceContainer = buildSourceContainer(sourceRoot);
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'test-parsed',
file: '/ws-1/a.test.ts',
- testContainer,
+ sourceContainer,
});
expect(wsRoot.item.children.size).toBe(1);
let docItem = getChildItem(wsRoot.item, 'a.test.ts');
expect(docItem.children.size).toEqual(2);
// now call discovery with additional files
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce([
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce([
'/ws-1/a.test.ts',
'/ws-1/b.test.ts',
]);
@@ -524,7 +577,7 @@ describe('test-item-data', () => {
const a1 = helper.makeAssertion('test-1', 'KnownSuccess', [], [1, 0]);
const assertionContainer = buildAssertionContainer([a1]);
const uri: any = { fsPath: '/ws-1/a.test.ts' };
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownSuccess',
assertionContainer,
});
@@ -537,7 +590,7 @@ describe('test-item-data', () => {
expect(runMock.passed).toHaveBeenCalledWith(tData.item, undefined);
});
it('if no test suite result yet, children list is empty', () => {
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue(undefined);
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue(undefined);
const uri: any = { fsPath: '/ws-1/a.test.ts' };
const parentItem: any = controllerMock.createTestItem('ws-1', 'ws-1', uri);
const docRoot = new TestDocumentRoot(context, uri, parentItem);
@@ -634,6 +687,22 @@ describe('test-item-data', () => {
expect(request.run).toBe(jestRun);
expect(request.run.item).toBe(folderData.item);
});
+ describe('can update snapshot based on runProfile', () => {
+ let wsRoot, folder, doc, testItem;
+ beforeEach(() => {
+ ({ wsRoot, folder, doc, testItem } = createAllTestItems());
+ });
+ it('with snapshot profile', () => {
+ [wsRoot, folder, doc, testItem].forEach((testItem) => {
+ testItem.scheduleTest(jestRun, ItemCommand.updateSnapshot);
+ expect(context.ext.session.scheduleProcess).toHaveBeenCalledWith(
+ expect.objectContaining({
+ updateSnapshot: true,
+ })
+ );
+ });
+ });
+ });
});
describe('when test result is ready', () => {
@@ -642,14 +711,14 @@ describe('test-item-data', () => {
let wsRoot;
beforeEach(() => {
jest.clearAllMocks();
- context.ext.testResolveProvider.getTestList.mockReturnValueOnce([file]);
+ context.ext.testResultProvider.getTestList.mockReturnValueOnce([file]);
wsRoot = new WorkspaceRoot(context);
// mocking test results
const a1 = helper.makeAssertion('test-a', 'KnownSuccess', [], [1, 0]);
const a2 = helper.makeAssertion('test-b', 'KnownFail', [], [10, 0], { line: 13 });
const assertionContainer = buildAssertionContainer([a1, a2]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
@@ -663,7 +732,7 @@ describe('test-item-data', () => {
expect(controllerMock.createTestRun).toHaveBeenCalledTimes(1);
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [file],
@@ -696,7 +765,7 @@ describe('test-item-data', () => {
expect(controllerMock.createTestRun).toHaveBeenCalledTimes(0);
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [file],
@@ -729,7 +798,7 @@ describe('test-item-data', () => {
wsRoot.scheduleTest(jestRun);
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [file],
@@ -771,7 +840,7 @@ describe('test-item-data', () => {
'c:\\ws-1\\src\\b.test.ts',
'c:\\ws-1\\src\\app\\app.test.ts',
];
- context.ext.testResolveProvider.getTestList.mockReturnValue(testFiles);
+ context.ext.testResultProvider.getTestList.mockReturnValue(testFiles);
const wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
@@ -806,7 +875,7 @@ describe('test-item-data', () => {
beforeEach(() => {
// establish baseline with 3 test files
testFiles = ['/ws-1/src/a.test.ts', '/ws-1/src/b.test.ts', '/ws-1/src/app/app.test.ts'];
- context.ext.testResolveProvider.getTestList.mockReturnValue(testFiles);
+ context.ext.testResultProvider.getTestList.mockReturnValue(testFiles);
wsRoot = new WorkspaceRoot(context);
wsRoot.discoverTest(jestRun);
});
@@ -815,7 +884,7 @@ describe('test-item-data', () => {
const withNewTestFiles = [...testFiles, '/ws-1/tests/d.test.ts', '/ws-1/src/c.test.ts'];
// trigger event
- context.ext.testResolveProvider.events.testListUpdated.event.mock.calls[0][0](
+ context.ext.testResultProvider.events.testListUpdated.event.mock.calls[0][0](
withNewTestFiles
);
@@ -834,7 +903,7 @@ describe('test-item-data', () => {
const withoutAppFiles = [testFiles[0], testFiles[1]];
// trigger event
- context.ext.testResolveProvider.events.testListUpdated.event.mock.calls[0][0](
+ context.ext.testResultProvider.events.testListUpdated.event.mock.calls[0][0](
withoutAppFiles
);
@@ -854,7 +923,7 @@ describe('test-item-data', () => {
const withRenamed = ['/ws-1/c.test.ts', testFiles[1], testFiles[2]];
// trigger event
- context.ext.testResolveProvider.events.testListUpdated.event.mock.calls[0][0](withRenamed);
+ context.ext.testResultProvider.events.testListUpdated.event.mock.calls[0][0](withRenamed);
//should see the new files in the tree
expect(wsRoot.item.children.size).toEqual(2);
@@ -877,7 +946,7 @@ describe('test-item-data', () => {
const parent: any = controllerMock.createTestItem('ws-1', 'ws-1', { fsPath: '/ws-1' });
a1 = helper.makeAssertion('test-1', 'KnownSuccess', ['desc-1'], [1, 0]);
const assertionContainer = buildAssertionContainer([a1]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValueOnce({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValueOnce({
status: 'KnownSuccess',
assertionContainer,
});
@@ -890,7 +959,7 @@ describe('test-item-data', () => {
const a3 = helper.makeAssertion('test-3', 'KnownSuccess', ['desc-2'], [10, 0]);
const a4 = helper.makeAssertion('test-4', 'KnownTodo', ['desc-2'], [15, 0]);
const assertionContainer = buildAssertionContainer([a1, a2, a3, a4]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
@@ -921,7 +990,7 @@ describe('test-item-data', () => {
it('delete', () => {
// delete the only test -1
const assertionContainer = buildAssertionContainer([]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'Unknown',
assertionContainer,
});
@@ -931,7 +1000,7 @@ describe('test-item-data', () => {
it('rename', () => {
const a2 = helper.makeAssertion('test-2', 'KnownFail', [], [1, 0]);
const assertionContainer = buildAssertionContainer([a2]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
@@ -945,7 +1014,7 @@ describe('test-item-data', () => {
});
it('with syntax error', () => {
const assertionContainer = buildAssertionContainer([]);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
@@ -959,7 +1028,7 @@ describe('test-item-data', () => {
runMock.failed.mockClear();
const assertionContainer = buildAssertionContainer(assertions);
- context.ext.testResolveProvider.getTestSuiteResult.mockReturnValue({
+ context.ext.testResultProvider.getTestSuiteResult.mockReturnValue({
status: 'KnownFail',
assertionContainer,
});
@@ -997,22 +1066,17 @@ describe('test-item-data', () => {
});
});
describe('tags', () => {
- let wsRoot, folder, doc, test;
+ let wsRoot, folder, doc, testItem;
beforeEach(() => {
- wsRoot = new WorkspaceRoot(context);
- folder = new FolderData(context, 'dir', wsRoot.item);
- const uri: any = { fsPath: 'whatever' };
- doc = new TestDocumentRoot(context, uri, folder.item);
- const node: any = { fullName: 'a test', attrs: {}, data: {} };
- test = new TestData(context, uri, node, doc.item);
+ ({ wsRoot, folder, doc, testItem } = createAllTestItems());
});
it('all TestItem supports run tag', () => {
- [wsRoot, folder, doc, test].forEach((itemData) =>
- expect(itemData.item.tags.find((t) => t.id === 'run')).toBeTruthy()
- );
+ [wsRoot, folder, doc, testItem].forEach((itemData) => {
+ expect(itemData.item.tags.find((t) => t.id === 'run')).toBeTruthy();
+ });
});
it('only TestData and TestDocument supports debug tags', () => {
- [doc, test].forEach((itemData) =>
+ [doc, testItem].forEach((itemData) =>
expect(itemData.item.tags.find((t) => t.id === 'debug')).toBeTruthy()
);
[wsRoot, folder].forEach((itemData) =>
@@ -1341,7 +1405,7 @@ describe('test-item-data', () => {
createTestRunSpy.mockClear();
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [env.file],
@@ -1389,7 +1453,7 @@ describe('test-item-data', () => {
createTestRunSpy.mockClear();
// triggers testSuiteChanged event listener
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [env.file],
@@ -1441,7 +1505,7 @@ describe('test-item-data', () => {
createTestRunSpy.mockClear();
// process test results
- context.ext.testResolveProvider.events.testSuiteChanged.event.mock.calls[0][0]({
+ context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'assertions-updated',
process,
files: [env.file],
@@ -1458,4 +1522,61 @@ describe('test-item-data', () => {
});
});
});
+ describe('runItemCommand', () => {
+ let wsRoot, folder, doc, testItem;
+ beforeEach(() => {
+ ({ wsRoot, folder, doc, testItem } = createAllTestItems());
+ });
+ it('can update-snapshot for every TestItemData', () => {
+ const createTestRunSpy = jest.spyOn(context, 'createTestRun');
+ [wsRoot, folder, doc, testItem].forEach((itemData) => {
+ createTestRunSpy.mockClear();
+ context.ext.session.scheduleProcess.mockClear();
+
+ itemData.runItemCommand(ItemCommand.updateSnapshot);
+ expect(createTestRunSpy).toHaveBeenCalledTimes(1);
+ expect(context.ext.session.scheduleProcess).toHaveBeenCalledWith(
+ expect.objectContaining({ updateSnapshot: true })
+ );
+ });
+ });
+ describe('view-snapshot', () => {
+ beforeEach(() => {
+ context.ext.testResultProvider.previewSnapshot.mockReturnValue(Promise.resolve());
+ });
+ it.each`
+ case | index | canView
+ ${'wsRoot'} | ${0} | ${false}
+ ${'folder'} | ${1} | ${false}
+ ${'doc'} | ${2} | ${false}
+ ${'testItem'} | ${3} | ${true}
+ `('$case supports view-snapshot? $canView', async ({ index, canView }) => {
+ testItem.node.attrs = { ...testItem.node.attrs, snapshot: 'external' };
+ const data = [wsRoot, folder, doc, testItem][index];
+ await data.runItemCommand(ItemCommand.viewSnapshot);
+ if (canView) {
+ expect(context.ext.testResultProvider.previewSnapshot).toHaveBeenCalled();
+ } else {
+ expect(context.ext.testResultProvider.previewSnapshot).not.toHaveBeenCalled();
+ }
+ });
+ it.each`
+ snapshotAttr | canView
+ ${'inline'} | ${false}
+ ${'external'} | ${true}
+ ${undefined} | ${false}
+ `(
+ 'testItem: snapshot = $snapshotAttr, canView? $canView',
+ async ({ snapshotAttr, canView }) => {
+ testItem.node.attrs = { ...testItem.node.attrs, snapshot: snapshotAttr };
+ await testItem.runItemCommand(ItemCommand.viewSnapshot);
+ if (canView) {
+ expect(context.ext.testResultProvider.previewSnapshot).toHaveBeenCalled();
+ } else {
+ expect(context.ext.testResultProvider.previewSnapshot).not.toHaveBeenCalled();
+ }
+ }
+ );
+ });
+ });
});
diff --git a/tests/test-provider/test-provider-helper.test.ts b/tests/test-provider/test-provider-helper.test.ts
index d20541d5..24f10065 100644
--- a/tests/test-provider/test-provider-helper.test.ts
+++ b/tests/test-provider/test-provider-helper.test.ts
@@ -63,3 +63,12 @@ describe('JestTestRun', () => {
});
});
});
+describe('JestTestProviderContext', () => {
+ it('when try to getTag not in any profiles, throw error', () => {
+ const whatever: any = {};
+ const profile: any = { tag: { id: 'run' } };
+ const context = new JestTestProviderContext(whatever, whatever, [profile]);
+ expect(context.getTag('run')).toEqual(profile.tag);
+ expect(() => context.getTag('debug')).toThrow();
+ });
+});
diff --git a/tests/test-provider/test-provider.test.ts b/tests/test-provider/test-provider.test.ts
index 105de3f1..1ea05af1 100644
--- a/tests/test-provider/test-provider.test.ts
+++ b/tests/test-provider/test-provider.test.ts
@@ -10,6 +10,7 @@ import { JestTestProviderContext } from '../../src/test-provider/test-provider-h
import { extensionId } from '../../src/appGlobals';
import { mockController, mockExtExplorerContext } from './test-helper';
import { tiContextManager } from '../../src/test-provider/test-item-context-manager';
+import { ItemCommand } from '../../src/test-provider/types';
const throwError = () => {
throw new Error('debug error');
@@ -20,6 +21,7 @@ describe('JestTestProvider', () => {
const data: any = {
discoverTest: jest.fn(),
scheduleTest: jest.fn(),
+ runItemCommand: jest.fn(),
dispose: jest.fn(),
};
if (debuggable) {
@@ -80,43 +82,23 @@ describe('JestTestProvider', () => {
);
expect(controllerMock.createRunProfile).toHaveBeenCalledTimes(2);
[
- [vscode.TestRunProfileKind.Run, 'run'],
- [vscode.TestRunProfileKind.Debug, 'debug'],
- ].forEach(([kind, id]) => {
+ [vscode.TestRunProfileKind.Run, 'run', true],
+ [vscode.TestRunProfileKind.Debug, 'debug', true],
+ ].forEach(([kind, id, isDefault]) => {
expect(controllerMock.createRunProfile).toHaveBeenCalledWith(
expect.anything(),
kind,
expect.anything(),
- true,
+ isDefault,
expect.objectContaining({ id })
);
});
expect(WorkspaceRoot).toHaveBeenCalled();
});
- it.each`
- isWatchMode
- ${true}
- ${false}
- `('will create Profiles regardless isWatchMode=$isWatchMode', ({ isWatchMode }) => {
- extExplorerContextMock.settings.autoRun.isWatch = isWatchMode;
- new JestTestProvider(extExplorerContextMock);
- const kinds = [vscode.TestRunProfileKind.Debug, vscode.TestRunProfileKind.Run];
-
- expect(controllerMock.createRunProfile).toHaveBeenCalledTimes(kinds.length);
- kinds.forEach((kind) => {
- expect(controllerMock.createRunProfile).toHaveBeenCalledWith(
- expect.anything(),
- kind,
- expect.anything(),
- true,
- expect.anything()
- );
- });
- });
});
- describe('can discover tests', () => {
+ describe('can discover tests', () => {
it('should only discover items with canResolveChildren = true', () => {
new JestTestProvider(extExplorerContextMock);
const data = setupTestItemData('whatever', true, workspaceRootMock.context);
@@ -535,7 +517,6 @@ describe('JestTestProvider', () => {
const [run] = d.scheduleTest.mock.calls[0];
expect(run).toEqual(expect.objectContaining({ vscodeRun: controllerMock.lastRunMock() }));
- /* eslint-disable jest/no-conditional-expect */
if (idx === 1) {
expect(run.vscodeRun.errored).toHaveBeenCalledWith(
d.item,
@@ -611,4 +592,9 @@ describe('JestTestProvider', () => {
);
});
});
+ it('supports runItemCommand', () => {
+ const provider = new JestTestProvider(extExplorerContextMock);
+ provider.runItemCommand(workspaceRootMock.item, ItemCommand.updateSnapshot);
+ expect(workspaceRootMock.runItemCommand).toHaveBeenCalled();
+ });
});
diff --git a/yarn.lock b/yarn.lock
index cf04a1a2..df3dc420 100644
--- a/yarn.lock
+++ b/yarn.lock
@@ -2934,10 +2934,10 @@ jest-each@^29.2.1:
jest-util "^29.2.1"
pretty-format "^29.2.1"
-jest-editor-support@^30.2.1:
- version "30.2.1"
- resolved "https://registry.yarnpkg.com/jest-editor-support/-/jest-editor-support-30.2.1.tgz#902911427fe46f052ec29320de8b8da41f832d97"
- integrity sha512-zsWAv6Taoqvci1jSiEVqCEG/IS/+Lwhyu1VJ/skcdlrhjjFrrcV+W3PRPjjI6bgSWMVNi20yPU1CrA88+e56Tg==
+jest-editor-support@^30.3.1:
+ version "30.3.1"
+ resolved "https://registry.yarnpkg.com/jest-editor-support/-/jest-editor-support-30.3.1.tgz#d7c3eb396c0c6d8a99bfb01e4f02dd416280d437"
+ integrity sha512-LcQubLda7j5CzSXkwxJqm4PELUw66e6JHtNMXR0erzz37aiEUOlGpOYgWKoVH7JnNsH7IgJ78jmhCqdB1LLdhw==
dependencies:
"@babel/parser" "^7.15.7"
"@babel/runtime" "^7.15.4"