Skip to content

Commit 5d3c88e

Browse files
committed
Adjusted waitForExpect to allow Jest expectation exceptions to be passed through. Unit test code improvements & refactoring.
1 parent 61ec0cd commit 5d3c88e

10 files changed

Lines changed: 193 additions & 192 deletions

tests/avatarManager.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { waitForExpect } from './helpers/expectations';
2-
31
import * as date from './mocks/date';
42
import * as vscode from './mocks/vscode';
53
jest.mock('vscode', () => vscode, { virtual: true });
@@ -21,6 +19,8 @@ import { Logger } from '../src/logger';
2119
import { GitExecutable } from '../src/utils';
2220
import { EventEmitter } from '../src/utils/event';
2321

22+
import { waitForExpect } from './helpers/expectations';
23+
2424
let onDidChangeConfiguration: EventEmitter<ConfigurationChangeEvent>;
2525
let onDidChangeGitExecutable: EventEmitter<GitExecutable>;
2626
let logger: Logger;

tests/bufferedQueue.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { waitForExpect } from './helpers/expectations';
2-
31
import { BufferedQueue } from '../src/utils/bufferedQueue';
42

3+
import { waitForExpect } from './helpers/expectations';
4+
55
describe('BufferedQueue', () => {
66
it('Should add items to the queue, and then process them once the buffer has expired', async () => {
77
// Setup

tests/commands.test.ts

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { waitForExpect } from './helpers/expectations';
2-
31
import * as date from './mocks/date';
42
import * as vscode from './mocks/vscode';
53
jest.mock('vscode', () => vscode, { virtual: true });
@@ -16,14 +14,17 @@ import { AvatarManager } from '../src/avatarManager';
1614
import { CommandManager } from '../src/commands';
1715
import { DataSource } from '../src/dataSource';
1816
import { DiffSide, encodeDiffDocUri } from '../src/diffDocProvider';
19-
import { DEFAULT_REPO_STATE, ExtensionState } from '../src/extensionState';
17+
import { ExtensionState } from '../src/extensionState';
2018
import { GitGraphView } from '../src/gitGraphView';
2119
import { Logger } from '../src/logger';
2220
import { RepoManager } from '../src/repoManager';
2321
import { GitFileStatus, RepoDropdownOrder } from '../src/types';
2422
import * as utils from '../src/utils';
2523
import { EventEmitter } from '../src/utils/event';
2624

25+
import { waitForExpect } from './helpers/expectations';
26+
import { mockRepoState } from './helpers/utils';
27+
2728
let onDidChangeConfiguration: EventEmitter<ConfigurationChangeEvent>;
2829
let onDidChangeGitExecutable: EventEmitter<utils.GitExecutable>;
2930
let logger: Logger;
@@ -100,7 +101,7 @@ describe('CommandManager', () => {
100101
});
101102

102103
describe('git-graph:codiconsSupported', () => {
103-
it('Should set git-graph:codiconsSupported to TRUE when vscode.version >= 1.42.0', () => {
104+
it('Should set git-graph:codiconsSupported to TRUE when vscode.version >= 1.42.0', async () => {
104105
// Setup
105106
commandManager.dispose();
106107
vscode.mockVscodeVersion('1.42.0');
@@ -111,13 +112,13 @@ describe('CommandManager', () => {
111112
commandManager = new CommandManager(vscode.mocks.extensionContext, avatarManager, dataSource, extensionState, repoManager, { path: '/path/to/git', version: '2.25.0' }, onDidChangeGitExecutable.subscribe, logger);
112113

113114
// Assert
114-
waitForExpect(() => {
115+
await waitForExpect(() => {
115116
expect(spyOnExecuteCommand).toHaveBeenCalledWith('setContext', 'git-graph:codiconsSupported', true);
116117
expect(spyOnLog).toHaveBeenCalledWith('Successfully set Visual Studio Code Context "git-graph:codiconsSupported" to "true"');
117118
});
118119
});
119120

120-
it('Should set git-graph:codiconsSupported to FALSE when vscode.version < 1.42.0', () => {
121+
it('Should set git-graph:codiconsSupported to FALSE when vscode.version < 1.42.0', async () => {
121122
// Setup
122123
commandManager.dispose();
123124
vscode.mockVscodeVersion('1.41.1');
@@ -128,13 +129,13 @@ describe('CommandManager', () => {
128129
commandManager = new CommandManager(vscode.mocks.extensionContext, avatarManager, dataSource, extensionState, repoManager, { path: '/path/to/git', version: '2.25.0' }, onDidChangeGitExecutable.subscribe, logger);
129130

130131
// Assert
131-
waitForExpect(() => {
132+
await waitForExpect(() => {
132133
expect(spyOnExecuteCommand).toHaveBeenCalledWith('setContext', 'git-graph:codiconsSupported', false);
133134
expect(spyOnLog).toHaveBeenCalledWith('Successfully set Visual Studio Code Context "git-graph:codiconsSupported" to "false"');
134135
});
135136
});
136137

137-
it('Should log an error message when vscode.commands.executeCommand rejects', () => {
138+
it('Should log an error message when vscode.commands.executeCommand rejects', async () => {
138139
// Setup
139140
commandManager.dispose();
140141
const spyOnExecuteCommand = jest.spyOn(vscode.commands, 'executeCommand');
@@ -145,13 +146,13 @@ describe('CommandManager', () => {
145146
commandManager = new CommandManager(vscode.mocks.extensionContext, avatarManager, dataSource, extensionState, repoManager, { path: '/path/to/git', version: '2.25.0' }, onDidChangeGitExecutable.subscribe, logger);
146147

147148
// Assert
148-
waitForExpect(() => {
149+
await waitForExpect(() => {
149150
expect(spyOnExecuteCommand).toHaveBeenCalledWith('setContext', 'git-graph:codiconsSupported', true);
150151
expect(spyOnLogError).toHaveBeenCalledWith('Failed to set Visual Studio Code Context "git-graph:codiconsSupported" to "true"');
151152
});
152153
});
153154

154-
it('Should log an error message when an exception is thrown', () => {
155+
it('Should log an error message when an exception is thrown', async () => {
155156
// Setup
156157
commandManager.dispose();
157158
const spyOnExecuteCommand = jest.spyOn(vscode.commands, 'executeCommand');
@@ -166,7 +167,7 @@ describe('CommandManager', () => {
166167
commandManager = new CommandManager(vscode.mocks.extensionContext, avatarManager, dataSource, extensionState, repoManager, { path: '/path/to/git', version: '2.25.0' }, onDidChangeGitExecutable.subscribe, logger);
167168

168169
// Assert
169-
waitForExpect(() => {
170+
await waitForExpect(() => {
170171
expect(spyOnExecuteCommand).toHaveBeenCalledWith('setContext', 'git-graph:codiconsSupported', true);
171172
expect(spyOnLogError).toHaveBeenCalledWith('Unable to set Visual Studio Code Context "git-graph:codiconsSupported"');
172173
});
@@ -356,8 +357,8 @@ describe('CommandManager', () => {
356357
it('Should ignore the selected repository', async () => {
357358
// Setup
358359
const repos = {
359-
'/path/to/repo2': mockRepoState('Custom Name', 1),
360-
'/path/to/repo1': mockRepoState(null, 0)
360+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 1 }),
361+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 })
361362
};
362363
spyOnGetRepos.mockReturnValueOnce(repos);
363364
vscode.window.showQuickPick.mockResolvedValueOnce({
@@ -398,8 +399,8 @@ describe('CommandManager', () => {
398399
it('Should display an error message if the selected repository no longer exists', async () => {
399400
// Setup
400401
spyOnGetRepos.mockReturnValueOnce({
401-
'/path/to/repo1': mockRepoState(null, 0),
402-
'/path/to/repo2': mockRepoState('Custom Name', 0)
402+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
403+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
403404
});
404405
vscode.window.showQuickPick.mockResolvedValueOnce({
405406
label: 'repo1',
@@ -437,8 +438,8 @@ describe('CommandManager', () => {
437438
it('Shouldn\'t attempt to ignore a repository if none was selected', async () => {
438439
// Setup
439440
spyOnGetRepos.mockReturnValueOnce({
440-
'/path/to/repo1': mockRepoState(null, 0),
441-
'/path/to/repo2': mockRepoState('Custom Name', 0)
441+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
442+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
442443
});
443444
vscode.window.showQuickPick.mockResolvedValueOnce(null);
444445

@@ -471,8 +472,8 @@ describe('CommandManager', () => {
471472
it('Should handle if showQuickPick rejects', async () => {
472473
// Setup
473474
spyOnGetRepos.mockReturnValueOnce({
474-
'/path/to/repo1': mockRepoState(null, 0),
475-
'/path/to/repo2': mockRepoState('Custom Name', 0)
475+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
476+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
476477
});
477478
vscode.window.showQuickPick.mockRejectedValueOnce(null);
478479

@@ -582,9 +583,9 @@ describe('CommandManager', () => {
582583
it('Should display a quick pick to select a repository to open in the Git Graph View (with last active repository first)', async () => {
583584
// Setup
584585
const repos = {
585-
'/path/to/repo3': mockRepoState(null, 2),
586-
'/path/to/repo2': mockRepoState('Custom Name', 1),
587-
'/path/to/repo1': mockRepoState(null, 0)
586+
'/path/to/repo3': mockRepoState({ name: null, workspaceFolderIndex: 2 }),
587+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 1 }),
588+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 })
588589
};
589590
spyOnGetRepos.mockReturnValueOnce(repos);
590591
spyOnGetLastActiveRepo.mockReturnValueOnce('/path/to/repo2');
@@ -628,8 +629,8 @@ describe('CommandManager', () => {
628629
it('Should display a quick pick to select a repository to open in the Git Graph View (no last active repository)', async () => {
629630
// Setup
630631
spyOnGetRepos.mockReturnValueOnce({
631-
'/path/to/repo1': mockRepoState(null, 0),
632-
'/path/to/repo2': mockRepoState('Custom Name', 0)
632+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
633+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
633634
});
634635
spyOnGetLastActiveRepo.mockReturnValueOnce(null);
635636
vscode.window.showQuickPick.mockResolvedValueOnce({
@@ -666,8 +667,8 @@ describe('CommandManager', () => {
666667
it('Should display a quick pick to select a repository to open in the Git Graph View (last active repository is unknown)', async () => {
667668
// Setup
668669
spyOnGetRepos.mockReturnValueOnce({
669-
'/path/to/repo1': mockRepoState(null, 0),
670-
'/path/to/repo2': mockRepoState('Custom Name', 0)
670+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
671+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
671672
});
672673
spyOnGetLastActiveRepo.mockReturnValueOnce('/path/to/repo3');
673674
vscode.window.showQuickPick.mockResolvedValueOnce({
@@ -704,8 +705,8 @@ describe('CommandManager', () => {
704705
it('Shouldn\'t open the Git Graph View when no item is selected in the quick pick', async () => {
705706
// Setup
706707
spyOnGetRepos.mockReturnValueOnce({
707-
'/path/to/repo1': mockRepoState(null, 0),
708-
'/path/to/repo2': mockRepoState('Custom Name', 0)
708+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
709+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
709710
});
710711
spyOnGetLastActiveRepo.mockReturnValueOnce('/path/to/repo3');
711712
vscode.window.showQuickPick.mockResolvedValueOnce(null);
@@ -739,8 +740,8 @@ describe('CommandManager', () => {
739740
it('Should display an error message when showQuickPick rejects', async () => {
740741
// Setup
741742
spyOnGetRepos.mockReturnValueOnce({
742-
'/path/to/repo1': mockRepoState(null, 0),
743-
'/path/to/repo2': mockRepoState('Custom Name', 0)
743+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 }),
744+
'/path/to/repo2': mockRepoState({ name: 'Custom Name', workspaceFolderIndex: 0 })
744745
});
745746
spyOnGetLastActiveRepo.mockReturnValueOnce('/path/to/repo2');
746747
vscode.window.showQuickPick.mockRejectedValueOnce(null);
@@ -776,7 +777,7 @@ describe('CommandManager', () => {
776777
it('Should open the Git Graph View immediately when there is only one repository', async () => {
777778
// Setup
778779
spyOnGetRepos.mockReturnValueOnce({
779-
'/path/to/repo1': mockRepoState(null, 0)
780+
'/path/to/repo1': mockRepoState({ name: null, workspaceFolderIndex: 0 })
780781
});
781782

782783
// Run
@@ -833,7 +834,7 @@ describe('CommandManager', () => {
833834
}
834835
});
835836
spyOnGetRepos.mockReturnValueOnce({
836-
'/path/to/repo': mockRepoState(null, 0)
837+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
837838
});
838839
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
839840
vscode.window.showQuickPick.mockImplementationOnce((items: Promise<any[]>, _: any) => items.then((items) => items[0]));
@@ -891,7 +892,7 @@ describe('CommandManager', () => {
891892
}
892893
});
893894
spyOnGetRepos.mockReturnValueOnce({
894-
'/path/to/repo': mockRepoState(null, 0)
895+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
895896
});
896897
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
897898
vscode.window.showQuickPick.mockResolvedValueOnce(null);
@@ -919,7 +920,7 @@ describe('CommandManager', () => {
919920
}
920921
});
921922
spyOnGetRepos.mockReturnValueOnce({
922-
'/path/to/repo': mockRepoState(null, 0)
923+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
923924
});
924925
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
925926
vscode.window.showQuickPick.mockImplementationOnce((items: Promise<any[]>, _: any) => items.then((items) => items[0]));
@@ -949,7 +950,7 @@ describe('CommandManager', () => {
949950
}
950951
});
951952
spyOnGetRepos.mockReturnValueOnce({
952-
'/path/to/repo': mockRepoState(null, 0)
953+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
953954
});
954955
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
955956
vscode.window.showQuickPick.mockImplementationOnce((items: Promise<any[]>, _: any) => items.then((items) => items[0]));
@@ -978,7 +979,7 @@ describe('CommandManager', () => {
978979
}
979980
});
980981
spyOnGetRepos.mockReturnValueOnce({
981-
'/path/to/repo': mockRepoState(null, 0)
982+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
982983
});
983984
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
984985
vscode.window.showQuickPick.mockRejectedValueOnce(null);
@@ -1020,7 +1021,7 @@ describe('CommandManager', () => {
10201021
}
10211022
});
10221023
spyOnGetRepos.mockReturnValueOnce({
1023-
'/path/to/repo': mockRepoState(null, 0)
1024+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
10241025
});
10251026
spyOnGetCommitSubject.mockImplementationOnce((_: string, hash: string) => hash === '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b' ? 'subject-' + hash : null);
10261027
spyOnGetCommitSubject.mockImplementationOnce((_: string, hash: string) => hash === '1a2b3c4d5e6f1a2b3c4d5e6f1a2b3c4d5e6f1a2b' ? 'subject-' + hash : null);
@@ -1077,7 +1078,7 @@ describe('CommandManager', () => {
10771078
}
10781079
});
10791080
spyOnGetRepos.mockReturnValueOnce({
1080-
'/path/to/repo': mockRepoState(null, 0)
1081+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
10811082
});
10821083
spyOnGetCommitSubject.mockImplementationOnce((_: string, hash: string) => 'subject-' + hash);
10831084
spyOnGetCommitSubject.mockImplementationOnce((_: string, hash: string) => 'subject-' + hash);
@@ -1124,7 +1125,7 @@ describe('CommandManager', () => {
11241125
}
11251126
});
11261127
spyOnGetRepos.mockReturnValueOnce({
1127-
'/path/to/repo': mockRepoState(null, 0)
1128+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
11281129
});
11291130
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
11301131
vscode.window.showQuickPick.mockResolvedValueOnce(null);
@@ -1166,7 +1167,7 @@ describe('CommandManager', () => {
11661167
}
11671168
});
11681169
spyOnGetRepos.mockReturnValueOnce({
1169-
'/path/to/repo': mockRepoState(null, 0)
1170+
'/path/to/repo': mockRepoState({ name: null, workspaceFolderIndex: 0 })
11701171
});
11711172
spyOnGetCommitSubject.mockResolvedValueOnce('Commit Subject');
11721173
vscode.window.showQuickPick.mockRejectedValueOnce(null);
@@ -1360,7 +1361,3 @@ describe('CommandManager', () => {
13601361
});
13611362
});
13621363
});
1363-
1364-
function mockRepoState(name: string | null, workspaceFolderIndex: number | null) {
1365-
return Object.assign({}, DEFAULT_REPO_STATE, { name: name, workspaceFolderIndex: workspaceFolderIndex });
1366-
}

tests/config.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { expectRenamedExtensionSettingToHaveBeenCalled } from './helpers/expectations';
2-
31
import * as vscode from './mocks/vscode';
42
jest.mock('vscode', () => vscode, { virtual: true });
53

64
import { getConfig } from '../src/config';
75
import { CommitDetailsViewLocation, CommitOrdering, DateFormatType, DateType, FileViewType, GitResetMode, GraphStyle, GraphUncommittedChangesStyle, RepoDropdownOrder, SquashMessageFormat, TabIconColourTheme, TagType } from '../src/types';
86

7+
import { expectRenamedExtensionSettingToHaveBeenCalled } from './helpers/expectations';
8+
99
const workspaceConfiguration = vscode.mocks.workspaceConfiguration;
1010

1111
type Config = ReturnType<typeof getConfig>;

tests/dataSource.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { waitForExpect } from './helpers/expectations';
2-
31
import * as date from './mocks/date';
42
import { mockSpyOnSpawn } from './mocks/spawn';
53
import * as vscode from './mocks/vscode';
@@ -18,6 +16,8 @@ import { CommitOrdering, GitConfigLocation, GitPushBranchMode, GitResetMode, Git
1816
import * as utils from '../src/utils';
1917
import { EventEmitter } from '../src/utils/event';
2018

19+
import { waitForExpect } from './helpers/expectations';
20+
2121
const workspaceConfiguration = vscode.mocks.workspaceConfiguration;
2222
let onDidChangeConfiguration: EventEmitter<ConfigurationChangeEvent>;
2323
let onDidChangeGitExecutable: EventEmitter<utils.GitExecutable>;

0 commit comments

Comments
 (0)