Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions src/test-provider/test-item-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,32 +59,68 @@ abstract class TestItemDataBase implements TestItemData, JestRunnable, WithUri {
return true;
}

getParentNode(): TestItemDataBase | undefined {
return this.item.parent ? this.context.getData<TestItemDataBase>(this.item.parent) : undefined;
}
/**
* Performs a deep lookup to find a node where the whole node branch contains only
* the resolved names. It will do a deep lookup instead of shallow lookup.
*
* @returns {TestItemDataBase | undefined} The resolved node if found, otherwise undefined.
*/
findResolvedNode(): TestItemDataBase {
if (this.isTestNameResolved()) {
// Check if all parent nodes are resolved
let parentNode = this.getParentNode();
while (parentNode) {
if (!parentNode.isTestNameResolved()) {
return parentNode.findResolvedNode(); // A parent node is unresolved, go up and search again
}
parentNode = parentNode.getParentNode();
}

return this; // All parent nodes are resolved
}
// Move to the parent node if the current node is unresolved
const p = this.getParentNode();
if (p) {
return p.findResolvedNode();
}

throw new Error(
`no resolved node found for ${this.item.id}: this should not have happened. Please file an issue`
);
}

scheduleTest(run: JestTestRun, options?: ScheduleTestOptions): void {
if (!this.isTestNameResolved()) {
const parent = this.item.parent && this.context.getData(this.item.parent);
if (parent) {
try {
const resolvedNode = this.findResolvedNode();
if (resolvedNode !== this) {
// the current node has an unresolved name therefore we need to schedule the test at the resolved parent node
run.end({ reason: 'unresolved parameterized test' });
run.updateRequest(new vscode.TestRunRequest([parent.item]));
return parent.scheduleTest(run, options);
run.updateRequest(new vscode.TestRunRequest([resolvedNode.item]));
return resolvedNode.scheduleTest(run, options);
}
this.context.output.write(`running an unresolved parameterized test might fail`, 'warn');
}

const jestRequest = this.getJestRunRequest(options);
// the current node and its parent chain is resolved, we can safely schedule the test
const jestRequest = this.getJestRunRequest(options);

this.deepItemState(this.item, run.enqueued);
this.deepItemState(this.item, run.enqueued);

const process = this.context.ext.session.scheduleProcess(jestRequest, {
run,
testItem: this.item,
});
if (!process) {
const msg = `failed to schedule test for ${this.item.id}`;
const process = this.context.ext.session.scheduleProcess(jestRequest, {
run,
testItem: this.item,
});
if (process) {
run.addProcess(process);
} else {
throw new Error(`failed to schedule test for ${this.item.id}`);
}
} catch (e) {
const msg = e instanceof Error ? e.message : `failed to schedule test: ${JSON.stringify(e)}`;
run.errored(this.item, new vscode.TestMessage(msg));
run.write(msg, 'error');
run.end({ reason: 'failed to schedule test' });
} else {
run.addProcess(process);
}
}

Expand Down
78 changes: 73 additions & 5 deletions tests/test-provider/test-item-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,11 +868,51 @@ describe('test-item-data', () => {
expect(process.userData.run).toBe(jestRun);
expect(process.userData.testItem.id).toBe(doc.item.id);

expect(jestRun.end).toHaveBeenCalledTimes(2);
expect(jestRun.updateRequest).toHaveBeenCalledTimes(2);
expect(jestRun.end).toHaveBeenCalledTimes(1);
expect(jestRun.updateRequest).toHaveBeenCalledTimes(1);
expect(jestRun.addProcess).toHaveBeenCalledTimes(1);
expect(vscode.TestRunRequest).toHaveBeenLastCalledWith([doc.item]);
});
it('if failed to get parent block, will attempt to run the test anyway', () => {
it('only execute the test if all of its parents are also resolved', () => {
// hierarchy: doc - resolvedDesc1 - unresolvedDesc - resolvedDesc2 - testNode
const { doc } = createAllTestItems();
const resolvedDesc1: any = {
fullName: 'a resolved describe 1',
attrs: { nonLiteralName: false },
childContainers: [],
childData: [],
};
const resolvedDesc2: any = {
fullName: 'a resolved describe 2',
attrs: { nonLiteralName: false },
childContainers: [],
childData: [],
};
const unresolvedDesc: any = {
fullName: 'a $describe',
attrs: { nonLiteralName: true },
childContainers: [],
childData: [],
};
const testNode: any = { fullName: 'a test', attrs: { isGroup: 'yes' }, data: {} };
const resolved1 = new TestData(context, doc.uri, resolvedDesc1, doc.item);
const unresolved = new TestData(context, doc.uri, unresolvedDesc, resolved1.item);
const resolved2 = new TestData(context, doc.uri, resolvedDesc2, unresolved.item);
const testItem = new TestData(context, doc.uri, testNode, resolved2.item);

const jestRun = createTestRun();

testItem.scheduleTest(jestRun);

expect(process.userData.run).toBe(jestRun);
expect(process.userData.testItem.id).toBe(resolved1.item.id);

expect(jestRun.end).toHaveBeenCalledTimes(1);
expect(jestRun.updateRequest).toHaveBeenCalledTimes(1);
expect(jestRun.addProcess).toHaveBeenCalledTimes(1);
expect(vscode.TestRunRequest).toHaveBeenLastCalledWith([resolved1.item]);
});
it('if failed to get a resolved parent block, will not attempt to run the test', () => {
const { doc } = createAllTestItems();

const testNode: any = { fullName: 'a $test', attrs: { nonLiteralName: true }, data: {} };
Expand All @@ -883,8 +923,36 @@ describe('test-item-data', () => {
context.getData = jest.fn().mockReturnValueOnce(undefined);

testItem.scheduleTest(jestRun);
expect(process.userData.run).toBe(jestRun);
expect(process.userData.testItem.id).toBe(testItem.item.id);

expect(jestRun.addProcess).not.toHaveBeenCalled();
expect(jestRun.errored).toHaveBeenCalled();
expect(jestRun.write).toHaveBeenCalledWith(
expect.stringContaining('no resolved node found'),
'error'
);
expect(jestRun.end).toHaveBeenCalledTimes(1);
});
it('can capture unexpected error and end the run cleanly', () => {
const { doc } = createAllTestItems();

const testNode: any = { fullName: 'a $test', attrs: { nonLiteralName: true }, data: {} };
const testItem = new TestData(context, doc.uri, testNode, doc.item);
const jestRun = createTestRun();

// simulate no parent block
context.getData = jest.fn().mockImplementationOnce(() => {
throw { msg: 'dummy error' };
});

testItem.scheduleTest(jestRun);

expect(jestRun.addProcess).not.toHaveBeenCalled();
expect(jestRun.errored).toHaveBeenCalled();
expect(jestRun.write).toHaveBeenCalledWith(
expect.stringContaining('dummy error'),
'error'
);
expect(jestRun.end).toHaveBeenCalledTimes(1);
});
});
describe('can update snapshot', () => {
Expand Down