Skip to content

Commit

Permalink
if match failed, all test should be marked as unknown
Browse files Browse the repository at this point in the history
  • Loading branch information
connectdotz committed Feb 5, 2021
1 parent 4c6f07a commit 024d7a0
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
57 changes: 40 additions & 17 deletions src/TestResults/TestResultProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { TestReconciler, JestTotalResults, TestFileAssertionStatus } from 'jest-editor-support';
import {
TestReconciler,
JestTotalResults,
TestFileAssertionStatus,
IParseResults,
} from 'jest-editor-support';
import { TestReconciliationState } from './TestReconciliationState';
import { TestResult, TestResultStatusInfo } from './TestResult';
import { parseTest } from '../TestParser';
Expand Down Expand Up @@ -72,30 +77,48 @@ export class TestResultProvider {
return consolidated;
}

private matchResults(filePath: string, { root, itBlocks }: IParseResults): TestResult[] {
try {
const assertions = this.reconciler.assertionsForTestFile(filePath);
if (assertions && assertions.length > 0) {
return this.groupByRange(
match.matchTestAssertions(filePath, root, assertions, this.verbose)
);
}
} catch (e) {
console.warn(`failed to match test results for ${filePath}:`, e);
}
// no need to do groupByRange as the source block will not have blocks under the same location
return itBlocks.map((t) =>
match.toMatchResult(t, 'no assertion found', 'no-matched-assertion')
);
}
private parseFile(filePath: string): IParseResults | undefined {
try {
// TODO this would parse any file, whether it is a test or not, because we don't know which file is actually included in jest test run! Should optimize this to only run for test files included in jest run
return parseTest(filePath);
} catch (e) {
// it is possible to have parse error espeically during development phase where the code might not even compiled
if (this.verbose) {
console.warn(`failed to parse file ${filePath}:`, e);
}
}
}
getResults(filePath: string): TestResult[] {
if (this.resultsByFilePath[filePath]) {
return this.resultsByFilePath[filePath];
}

let matchResult: TestResult[] = [];

let matchResults: TestResult[] = [];
try {
// TODO this would parse any file, whether it is a test or not, because we don't know which file is actually included in jest test run! Should optimize this to only run for test files included in jest run
const { root, itBlocks } = parseTest(filePath);
const assertions = this.reconciler.assertionsForTestFile(filePath);
if (assertions?.length > 0) {
matchResult = match.matchTestAssertions(filePath, root, assertions, this.verbose);
} else {
matchResult = itBlocks.map((t) =>
match.toMatchResult(t, 'no assertion found', 'no-matched-assertion')
);
}
const parseResult = this.parseFile(filePath);
matchResults = parseResult ? this.matchResults(filePath, parseResult) : matchResults;
} catch (e) {
console.warn(`failed to get test result for ${filePath}:`, e);
console.warn(`failed to get test results for ${filePath}:`, e);
}
const testResults = this.groupByRange(matchResult);
this.resultsByFilePath[filePath] = testResults;
return testResults;

this.resultsByFilePath[filePath] = matchResults;
return matchResults;
}

getSortedResults(filePath: string): SortedTestResults {
Expand Down
30 changes: 30 additions & 0 deletions tests/TestResults/TestResultProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { TestReconciliationState } from '../../src/TestResults';
import { parseTest } from '../../src/TestParser';
import * as helper from '../test-helper';
import { ItBlock } from 'jest-editor-support';
import * as match from '../../src/TestResults/match-by-context';

const mockParseTest = (itBlocks: ItBlock[]) => {
((parseTest as unknown) as jest.Mock<{}>).mockReturnValueOnce({
Expand Down Expand Up @@ -518,6 +519,35 @@ describe('TestResultProvider', () => {
}
);
});
it('when test file parse failed, no results is returned and only report when in debug mode', () => {
(parseTest as jest.Mocked<any>).mockImplementation(() => {
throw new Error('parse failed');
});

let sut = new TestResultProvider();
let results = sut.getResults('whatever');
expect(results).toHaveLength(0);
expect(console.warn).not.toHaveBeenCalled();

sut = new TestResultProvider(true);
results = sut.getResults('whatever');
expect(results).toHaveLength(0);
expect(console.warn).toHaveBeenCalled();
});
it('when match logic failed, all tests should be marked as Unknown', () => {
const mockMatchByContext = jest.spyOn(match, 'matchTestAssertions').mockImplementation(() => {
throw new Error('match failed');
});
const tBlock = helper.makeItBlock('a test', [8, 0, 20, 20]);
mockParseTest([tBlock]);
const aBlock = helper.makeAssertion('a test', 'KnownSuccess', [], [8, 20]);
assertionsForTestFile.mockReturnValueOnce([aBlock]);
const sut = new TestResultProvider();
const results = sut.getResults('whatever');
expect(results.map((r) => [r.name, r.status])).toEqual([['a test', 'Unknown']]);
expect(console.warn).toHaveBeenCalled();
mockMatchByContext.mockRestore();
});
});

describe('getSortedResults()', () => {
Expand Down

0 comments on commit 024d7a0

Please sign in to comment.