Skip to content

Commit

Permalink
fix test block not displaying sometimes (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Nov 23, 2022
1 parent 9942dee commit 1b08735
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 57 deletions.
7 changes: 5 additions & 2 deletions src/JestExt/core.ts
Expand Up @@ -326,7 +326,7 @@ export class JestExt {
this.updateTestFileEditor(editor);
}

public triggerUpdateSettings(newSettings?: PluginResourceSettings): Promise<void> {
public async triggerUpdateSettings(newSettings?: PluginResourceSettings): Promise<void> {
const updatedSettings =
newSettings ?? getExtensionResourceSettings(this.extContext.workspace.uri);

Expand All @@ -348,7 +348,10 @@ export class JestExt {

this.extContext = createJestExtContext(this.extContext.workspace, updatedSettings);

return this.startSession(true);
await this.startSession(true);
if (vscode.window.activeTextEditor) {
this.triggerUpdateActiveEditor(vscode.window.activeTextEditor);
}
}

private isSupportedDocument(document: vscode.TextDocument | undefined): boolean {
Expand Down
7 changes: 7 additions & 0 deletions src/TestResults/TestResultProvider.ts
Expand Up @@ -314,6 +314,13 @@ export class TestResultProvider {
message: error,
results: itBlocks.map((t) => match.toMatchResult(t, 'no assertion found', 'match-failed')),
});

// file match failed event so the listeners can display the source blocks instead
this.events.testSuiteChanged.fire({
type: 'result-match-failed',
file: filePath,
sourceContainer: record.testBlocks.sourceContainer,
});
}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/TestResults/test-result-events.ts
Expand Up @@ -14,6 +14,11 @@ export type TestSuitChangeEvent =
type: 'result-matched';
file: string;
}
| {
type: 'result-match-failed';
file: string;
sourceContainer: ContainerNode<ItBlock>;
}
| {
type: 'test-parsed';
file: string;
Expand Down
2 changes: 2 additions & 0 deletions src/test-provider/test-item-data.ts
Expand Up @@ -274,6 +274,8 @@ export class WorkspaceRoot extends TestItemDataBase {
this.updateSnapshotContext(snapshotItems);
break;
}

case 'result-match-failed':
case 'test-parsed': {
const snapshotItems: SnapshotItemCollection = {
viewable: [],
Expand Down
23 changes: 20 additions & 3 deletions tests/TestResults/TestResultProvider.test.ts
Expand Up @@ -97,7 +97,7 @@ const makeData = (

const eventsMock: any = mockJestExtEvents();

const newProviderWithData = (testData: TestData[]): TestResultProvider => {
const newProviderWithData = (testData: TestData[], verbose?: boolean): TestResultProvider => {
mockParse.mockImplementation((file) => {
const data = testData.find((data) => data.file === file);
if (data) {
Expand All @@ -120,7 +120,7 @@ const newProviderWithData = (testData: TestData[]): TestResultProvider => {
assertions: data.assertions,
}))
);
const sut = new TestResultProvider(eventsMock);
const sut = new TestResultProvider(eventsMock, verbose);
// warn up cache
sut.updateTestResults({} as any, {} as any);
return sut;
Expand Down Expand Up @@ -155,6 +155,7 @@ describe('TestResultProvider', () => {
jest.resetAllMocks();
jest.restoreAllMocks();
console.warn = jest.fn();
console.log = jest.fn();
mockTestReconciler.mockReturnValue(mockReconciler);
(vscode.EventEmitter as jest.Mocked<any>) = jest.fn().mockImplementation(helper.mockEvent);
mockSnapshotProvider = {
Expand Down Expand Up @@ -247,14 +248,19 @@ describe('TestResultProvider', () => {
});
});
});
it('unmatched test will file test-parsed event instead', () => {
it('unmatched test will file test-parsed and result-match-failed events instead', () => {
const sut = newProviderWithData([makeData([testBlock], null, filePath)]);
sut.getResults(filePath);
expect(sut.events.testSuiteChanged.fire).toHaveBeenCalledWith({
type: 'test-parsed',
file: filePath,
sourceContainer: expect.anything(),
});
expect(sut.events.testSuiteChanged.fire).toHaveBeenCalledWith({
type: 'result-match-failed',
file: filePath,
sourceContainer: expect.anything(),
});
});

describe('duplicate test names', () => {
Expand Down Expand Up @@ -624,6 +630,17 @@ describe('TestResultProvider', () => {
}
}
);
it('parse error will output log only in verbose mode', () => {
let sut = newProviderWithData([makeData(itBlocks, assertions, 'whatever')], false);
sut.getResults('whatever');
forceParseError();
expect(console.log).not.toHaveBeenCalled();

sut = newProviderWithData([makeData(itBlocks, assertions, 'whatever')], true);
forceParseError();
sut.getResults('whatever');
expect(console.log).toHaveBeenCalled();
});
});
});

Expand Down
111 changes: 59 additions & 52 deletions tests/test-provider/test-item-data.test.ts
Expand Up @@ -484,59 +484,66 @@ describe('test-item-data', () => {
);
});
});
describe('when testSuiteChanged.test-parsed event filed', () => {
it('test items will be added and snapshot context updated accordingly', () => {
// assertion should be discovered prior
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 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.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.testResultProvider.getTestSuiteResult.mockClear();

context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: 'test-parsed',
file: '/ws-1/a.test.ts',
sourceContainer,
});
expect(docItem.children.size).toEqual(2);
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.testResultProvider.getTestSuiteResult).not.toHaveBeenCalled();
expect(controllerMock.createTestRun).not.toHaveBeenCalled();
describe('testSuiteChanged events when not able to show assertions', () => {
it.each`
event
${'test-parsed'}
${'result-match-failed'}
`(
'$event: test items will be added and snapshot context updated accordingly',
({ event }) => {
// assertion should be discovered prior
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 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.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.testResultProvider.getTestSuiteResult.mockClear();

context.ext.testResultProvider.events.testSuiteChanged.event.mock.calls[0][0]({
type: event,
file: '/ws-1/a.test.ts',
sourceContainer,
});
expect(docItem.children.size).toEqual(2);
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.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],
})
);
});
// 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],
})
);
}
);
});
});
it('can preserve parse-result occurred before discover', () => {
Expand Down

0 comments on commit 1b08735

Please sign in to comment.