-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use jest-editor-support to show messages etc (#23)
* feat: Use jest-editor-support to show messages etc * feat: Add some failing tests
- Loading branch information
Showing
13 changed files
with
1,729 additions
and
1,191 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import jest from "./dist"; | ||
import jest from './dist' | ||
|
||
jest(); | ||
jest() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
2,238 changes: 1,118 additions & 1,120 deletions
2,238
src/__tests__/__fixtures__/failing-tests.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
it.skip('Fails 1', () => { | ||
expect(1).toEqual(2) | ||
}) | ||
|
||
it.skip('Fails 2', () => { | ||
expect(' asdasd').toBeLessThanOrEqual(2) | ||
}) | ||
|
||
it.skip('Fails 4', () => { | ||
expect({ a: 'asda'}).toBeFalsy() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,119 @@ | ||
import * as fs from 'fs' | ||
import * as path from 'path' | ||
|
||
import { TestFailureFormatter } from './TestFailureFormatter' | ||
import * as stripANSI from 'strip-ansi' | ||
|
||
export interface IPluginConfig { | ||
testResultsJsonPath?: string | ||
} | ||
import { DangerDSLType } from '../node_modules/danger/distribution/dsl/DangerDSL' | ||
import { IInsideFileTestResults, IJestTestOldResults, IJestTestResults } from './types' | ||
declare var danger: DangerDSLType | ||
|
||
interface IJestTestResults { | ||
success: boolean | ||
export interface IPluginConfig { | ||
testResultsJsonPath: string | ||
} | ||
|
||
export default function jest(config: IPluginConfig = {}) { | ||
const { | ||
testResultsJsonPath = 'test-results.json', | ||
} = config | ||
|
||
export default function jest(config: Partial<IPluginConfig> = {}) { | ||
const { testResultsJsonPath = 'test-results.json' } = config | ||
try { | ||
const jsonFileContents = fs.readFileSync(testResultsJsonPath, 'utf8') | ||
const testResults: IJestTestResults = JSON.parse(jsonFileContents) | ||
if (testResults.success) { | ||
message(':white_check_mark: Jest tests passed') | ||
} else { | ||
const formattedFailures = new TestFailureFormatter(testResults).format() | ||
fail(`:no_good_man: Jest tests failed: | ||
const jsonResults: IJestTestResults = JSON.parse(jsonFileContents) | ||
// console.log(jsonResults) | ||
if (jsonResults.success) { | ||
// tslint:disable-next-line:no-console | ||
console.log('Jest tests passed :+1:') | ||
return | ||
} | ||
// console.log(1) | ||
|
||
\`\`\` | ||
${formattedFailures} | ||
\`\`\``) | ||
const isModernFormatResults = jsonResults.testResults[0].testResults | ||
if (isModernFormatResults) { | ||
presentErrorsForNewStyleResults(jsonResults) | ||
} else { | ||
presentErrorsForOldStyleResults(jsonResults as any) | ||
} | ||
} catch (e) { | ||
// tslint:disable-next-line:no-console | ||
console.error(e) | ||
warn('Could not read test results. Danger cannot pass or fail the build.') | ||
fail('[danger-plugin-jest] Could not read test results. Danger cannot pass or fail the build.') | ||
} | ||
} | ||
|
||
const presentErrorsForOldStyleResults = (jsonResults: IJestTestOldResults) => { | ||
const failing = jsonResults.testResults.filter((tr) => tr.status === 'failed') | ||
|
||
failing.forEach((results) => { | ||
const relativeFilePath = path.relative(process.cwd(), results.name) | ||
const failedAssertions = results.assertionResults.filter((r) => r.status === 'failed') | ||
const failMessage = fileToFailString(relativeFilePath, failedAssertions as any) | ||
fail(failMessage) | ||
}) | ||
} | ||
|
||
const presentErrorsForNewStyleResults = (jsonResults: IJestTestResults) => { | ||
const failing = jsonResults.testResults.filter((tr) => tr.numFailingTests > 0) | ||
|
||
failing.forEach((results) => { | ||
const relativeFilePath = path.relative(process.cwd(), results.testFilePath) | ||
const failedAssertions = results.testResults.filter((r) => r.status === 'failed') | ||
const failMessage = fileToFailString(relativeFilePath, failedAssertions) | ||
fail(failMessage) | ||
}) | ||
} | ||
|
||
// e.g. https://github.com/orta/danger-plugin-jest/blob/master/src/__tests__/fails.test.ts | ||
const linkToTest = (file: string, msg: string, title: string) => { | ||
const line = lineOfError(msg, file) | ||
const repo = danger.github.pr.head.repo | ||
const urlParts = [ | ||
'https://github.com', | ||
repo.full_name, | ||
'blob', | ||
danger.github.pr.head.ref, | ||
`${file}${'line' ? '#L' + line : ''}`, | ||
] | ||
return `<a href='${urlParts.join('/')}'>${title}</a>` | ||
} | ||
|
||
const assertionFailString = (file: string, status: IInsideFileTestResults): string => ` | ||
<li> | ||
${linkToTest(file, status.failureMessages && status.failureMessages[0], status.title)} | ||
<br/> | ||
${sanitizeShortErrorMessage(status.failureMessages && stripANSI(status.failureMessages[0]))} | ||
<details> | ||
<summary>Full message</summary> | ||
<pre><code> | ||
${status.failureMessages && stripANSI(status.failureMessages.join('\n'))} | ||
</code></pre> | ||
</details> | ||
</li> | ||
<br/> | ||
` | ||
|
||
const fileToFailString = (path: string, failedAssertions: IInsideFileTestResults[]): string => ` | ||
<b>🃏 FAIL</b> in ${danger.github.utils.fileLinks([path])} | ||
${failedAssertions.map((a) => assertionFailString(path, a)).join('\n\n')} | ||
` | ||
|
||
const lineOfError = (msg: string, filePath: string): number | null => { | ||
const filename = path.basename(filePath) | ||
const restOfTrace = msg.split(filename, 2)[1] | ||
return restOfTrace ? parseInt(restOfTrace.split(':')[1], 10) : null | ||
} | ||
|
||
const sanitizeShortErrorMessage = (msg: string): string => { | ||
if (msg.includes('does not match stored snapshot')) { | ||
return 'Snapshot has changed' | ||
} | ||
|
||
return msg | ||
.split(' at', 1)[0] | ||
.trim() | ||
.split('\n') | ||
.splice(2) | ||
.join('') | ||
.replace(/\s\s+/g, ' ') | ||
.replace('Received:', ', received:') | ||
.replace('., received', ', received') | ||
.split('Difference:')[0] | ||
} |
Oops, something went wrong.