Skip to content
This repository has been archived by the owner on Aug 28, 2022. It is now read-only.

Commit

Permalink
feat(project-wide): added Jest specific categories, and improved erro…
Browse files Browse the repository at this point in the history
…r catching
  • Loading branch information
ryparker committed Jul 8, 2020
1 parent 57ec33c commit 5c4ae36
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 68 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@
"@jest/reporters": "^26.1.0",
"@jest/types": "^26.1.0",
"allure-js-commons": "^2.0.0-beta.7",
"ansi_up": "^4.0.4",
"crypto": "^1.0.1",
"jest-circus": "^26.1.0",
"jest-environment-node": "^26.1.0",
"lodash": "^4.17.19",
"prettier": "^2.0.5",
"pretty-format": "^26.1.0",
"strip-ansi": "^6.0.0"
Expand All @@ -57,6 +59,7 @@
"@semantic-release/git": "^9.0.0",
"@types/allure-js-commons": "^0.0.1",
"@types/jest": "^26.0.4",
"@types/lodash": "^4.14.157",
"@types/node": "^14.0.19",
"commitizen": "^4.1.2",
"cz-conventional-changelog": "^3.2.0",
Expand Down Expand Up @@ -86,4 +89,4 @@
"collect",
"analyze"
]
}
}
17 changes: 13 additions & 4 deletions src/allure-node-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ export default class AllureNodeEnvironment extends NodeEnvironment {
constructor(config: Config.ProjectConfig, context: EnvironmentContext) {
super(config);

const allureConfig: IAllureConfig = {resultsDir: config.testEnvironmentOptions.resultsDir ?? 'allure-results'};
const allureConfig: IAllureConfig = {
resultsDir: config.testEnvironmentOptions.resultsDir ?? 'allure-results'
};

this.docblockPragmas = context.docblockPragmas;
this.testPath = context.testPath ? context.testPath.replace(config.rootDir, '') : '';
Expand Down Expand Up @@ -96,17 +98,24 @@ export default class AllureNodeEnvironment extends NodeEnvironment {
// Console.log('TEST_FN_FAILURE ERROR:', event.error);
// console.log('TEST_FN_FAILURE TEST.ERRORS:', event.test.errors);
// console.log('TEST_FN_FAILURE TEST.ASYNCERROR:', event.test.asyncError);

this.reporter.failTestCase(event.test, state, this.testPath, event.error ?? event.test.asyncError);
break;
case 'test_done':
/**
* This is more reliable for error collection. Some failures will only appear
* in this event. E.g. Snapshot test failures. Capturing errors from both
* test_done and test_fn_failure causes the test to be overriden, loosing all
* test context (steps).
*
* A workaround might be to refactor the AllureReporter class by separating
* the endTestCase() from the (status*)TestCase() methods.
*/

if (event.test.errors.length > 0) {
this.reporter.failTestCase(event.test, state, this.testPath, event.test.errors[0]);
} else {
this.reporter.passTestCase(event.test, state, this.testPath);
}

// Console.log('TEST_DONE TEST.ASYNCERROR:', event.test.asyncError);
break;
case 'run_describe_finish':
this.reporter.endSuite();
Expand Down
123 changes: 95 additions & 28 deletions src/allure-reporter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import JestAllureInterface from './jest-allure-interface';
import type * as jest from '@jest/types';
import stripAnsi = require('strip-ansi');
import _ = require('lodash');
import prettier = require('prettier/standalone');
import parser = require('prettier/parser-typescript');
import {
Expand All @@ -16,6 +17,8 @@ import {
StatusDetails
} from 'allure-js-commons';
import {createHash} from 'crypto';
// Import AnsiUp from 'ansi_up';
// const ansiUp = new AnsiUp();

export default class AllureReporter {
public currentExecutable: ExecutableItemWrapper | null = null;
Expand All @@ -26,6 +29,64 @@ export default class AllureReporter {

constructor(private readonly allureRuntime: AllureRuntime, jiraUrl: string) {
this.jiraUrl = jiraUrl;

this.allureRuntime.writeCategoriesDefinitions([
{
name: 'Response status failures',
description: 'Unexpected API response status code.',
messageRegex: '.*toHaveStatusCode.*',
matchedStatuses: [
Status.FAILED
]
},
{
name: 'Response time failures',
description: 'API responses that took longer than expected.',
messageRegex: '.*toHaveResponseTimeBelow.*',
matchedStatuses: [
Status.FAILED
]
},
{
name: 'JSON schema failures',
description: 'An object did not validate against an expected JSON schema.',
messageRegex: '.*toMatchSchema.*',
matchedStatuses: [
Status.FAILED
]
},
{
name: 'Snapshot failures',
description: 'Snapshot does not match the expected snapshot.',
messageRegex: '.*toMatchSnapshot.*',
matchedStatuses: [
Status.FAILED
]
},
{
name: 'Updated JSON schemas',
description: 'Tests that have updated a JSON schema.',
messageRegex: '.*updated.*schema.*updated.*',
matchedStatuses: [
Status.PASSED
]
},
{
name: 'Updated snapshots',
description: 'Tests that have updated a snapshot.',
messageRegex: '.*updated.*snapshots.*updated.*',
matchedStatuses: [
Status.PASSED
]
},
{
name: 'Skipped tests',
description: 'Tests that were skipped in this run.',
matchedStatuses: [
Status.SKIPPED
]
}
]);
}

public getImplementation(): JestAllureInterface {
Expand Down Expand Up @@ -85,6 +146,7 @@ export default class AllureReporter {
this.currentExecutable.stage = Stage.FINISHED;

if (error) {
// Console.log('endHook -> handleError');
const {status, message, trace} = this.handleError(error);

this.currentExecutable.status = status;
Expand All @@ -108,8 +170,9 @@ export default class AllureReporter {
.digest('hex');
this.currentTest.stage = Stage.RUNNING;

const testFunc = test.fn ? prettier.format(test.fn.toString(), {parser: 'typescript', plugins: [parser]}) : '';
this.currentTest.description = `### Test\n\`\`\`TS\n${testFunc}\n\`\`\`\n`;
const testFunc = test.fn ? prettier.format(test.fn.toString(), {parser: 'typescript', plugins: [parser]}) : 'Error parsing function';

this.currentTest.description = `### Test\n\`\`\`typescript\n${testFunc}\n\`\`\`\n`;

if (state.parentProcess?.env?.JEST_WORKER_ID) {
this.currentTest.addLabel(LabelName.THREAD, state.parentProcess.env.JEST_WORKER_ID);
Expand Down Expand Up @@ -143,7 +206,7 @@ export default class AllureReporter {

public pendingTestCase(test: jest.Circus.TestEntry, state: jest.Circus.State, testPath: string): void {
this.startCase(test, state, testPath);
this.endTest(Status.SKIPPED, {message: `Test ${test.mode as string}`});
this.endTest(Status.SKIPPED, {message: `Test is marked: "${test.mode as string}"`});
}

public failTestCase(
Expand Down Expand Up @@ -204,38 +267,47 @@ export default class AllureReporter {
}

private handleError(error: Error | any) {
if (error.matcherResult) {
console.log('error.matcherResult:', error.matcherResult);
} else {
console.log('error:', error);
if (Array.isArray(error)) {
// Test_done event (consistently?) sends an array of error arrays.
error = _.flattenDeep(error)[0];
}

const status = error.matcherResult ? Status.FAILED : Status.BROKEN;
let status = Status.BROKEN;
let message = error.name;
let trace = error.message;

const isSnapshotFailure = error.matcherResult?.name === 'toMatchSnapshot';
if (error.matcherResult) {
status = Status.FAILED;
const matcherMessage = error.matcherResult.message();

let message: string;
let trace: string;
const [line1, line2, ...restOfMessage] = matcherMessage.split('\n');

if (isSnapshotFailure) {
const [matcherHint, ...snapshotDiff] = stripAnsi(error.matcherResult.message()).split('@@');
message = [line1, line2].join('\n');
trace = restOfMessage.join('\n');
}

message = matcherHint;
trace = snapshotDiff.join('');
} else {
message = stripAnsi(error.message);
trace = stripAnsi(error.stack ?? '').replace(message, '');
if (!trace) {
trace = error.stack;
}

if (!message && trace) {
message = trace;
trace = error.stack?.replace(message, 'No stack trace provided');
}

if (!message && status) {
message = status;
trace = '';
if (trace?.includes(message)) {
trace = trace?.replace(message, '');
}

if (!message) {
message = 'Unformatted error. Expand for more details.';
trace = error;
}

return {
status,
message,
trace
message: stripAnsi(message),
trace: stripAnsi(trace)
};
}

Expand All @@ -249,9 +321,4 @@ export default class AllureReporter {

return testPath;
}

// Private formatErrors(error: Error) {
// error.message = stripAnsi(test.failure.message)
// error['stack-trace'] = stripAnsi(test.failure['stack-trace'])
// }
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"noImplicitReturns": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"types": ["node", "jest", "allure-js-commons"],
"types": ["node", "jest", "allure-js-commons", "lodash"],
"outDir": "dist/"
},
"include": ["src/**/*"],
Expand Down
51 changes: 17 additions & 34 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -883,6 +883,11 @@
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.157":
version "4.14.157"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.157.tgz#fdac1c52448861dfde1a2e1515dbc46e54926dc8"
integrity sha512-Ft5BNFmv2pHDgxV5JDsndOWTRJ+56zte0ZpYLowp03tW+K+t8u8YMOzAnpuqPgzX6WO1XpDIUm7u04M8vdDiVQ==

"@types/minimatch@*":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@types/minimatch/-/minimatch-3.0.3.tgz#3dca0e3f33b200fc7d1139c0cd96c1268cadfd9d"
Expand Down Expand Up @@ -1175,6 +1180,11 @@ ansi-styles@^4.0.0, ansi-styles@^4.1.0:
"@types/color-name" "^1.1.1"
color-convert "^2.0.1"

ansi_up@^4.0.4:
version "4.0.4"
resolved "https://registry.yarnpkg.com/ansi_up/-/ansi_up-4.0.4.tgz#5b8c35f0b02e4476f3f18cf89c3bf48d15d054f6"
integrity sha512-vRxC8q6QY918MbehO869biJW4tiunJdjOhi5fpY6NLOliBQlZhOkKgABJKJqH+JZfb/WfjvjN1chLWI6tODerw==

ansicolors@~0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/ansicolors/-/ansicolors-0.3.2.tgz#665597de86a9ffe3aa9bfbe6cae5c6ea426b4979"
Expand Down Expand Up @@ -2454,7 +2464,7 @@ debug@^3.1.0:
dependencies:
ms "^2.1.1"

debuglog@*, debuglog@^1.0.1:
debuglog@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492"
integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI=
Expand Down Expand Up @@ -4223,7 +4233,7 @@ import-modules@^2.0.0:
resolved "https://registry.yarnpkg.com/import-modules/-/import-modules-2.0.0.tgz#9c1e13b4e7a15682f70a6e3fa29534e4540cfc5d"
integrity sha512-iczM/v9drffdNnABOKwj0f9G3cFDon99VcG1mxeBsdqnbd+vnQ5c2uAiCHNQITqFTOPaEvwg3VjoWCur0uHLEw==

imurmurhash@*, imurmurhash@^0.1.4:
imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
integrity sha1-khi5srkoojixPcT7a21XbyMUU+o=
Expand Down Expand Up @@ -5623,11 +5633,6 @@ lockfile@^1.0.4:
dependencies:
signal-exit "^3.0.2"

lodash._baseindexof@*:
version "3.1.0"
resolved "https://registry.yarnpkg.com/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c"
integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw=

lodash._baseuniq@~4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8"
Expand All @@ -5636,33 +5641,11 @@ lodash._baseuniq@~4.6.0:
lodash._createset "~4.0.0"
lodash._root "~3.0.0"

lodash._bindcallback@*:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e"
integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4=

lodash._cacheindexof@*:
version "3.0.2"
resolved "https://registry.yarnpkg.com/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92"
integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI=

lodash._createcache@*:
version "3.1.2"
resolved "https://registry.yarnpkg.com/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093"
integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM=
dependencies:
lodash._getnative "^3.0.0"

lodash._createset@~4.0.0:
version "4.0.3"
resolved "https://registry.yarnpkg.com/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26"
integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY=

lodash._getnative@*, lodash._getnative@^3.0.0:
version "3.9.1"
resolved "https://registry.yarnpkg.com/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5"
integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U=

lodash._root@~3.0.0:
version "3.0.1"
resolved "https://registry.yarnpkg.com/lodash._root/-/lodash._root-3.0.1.tgz#fba1c4524c19ee9a5f8136b4609f017cf4ded692"
Expand Down Expand Up @@ -5708,11 +5691,6 @@ lodash.map@^4.5.1:
resolved "https://registry.yarnpkg.com/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3"
integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM=

lodash.restparam@*:
version "3.6.1"
resolved "https://registry.yarnpkg.com/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805"
integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU=

lodash.sortby@^4.7.0:
version "4.7.0"
resolved "https://registry.yarnpkg.com/lodash.sortby/-/lodash.sortby-4.7.0.tgz#edd14c824e2cc9c1e0b0a1b42bb5210516a42438"
Expand Down Expand Up @@ -5753,6 +5731,11 @@ lodash@4.17.15, lodash@^4.13.1, lodash@^4.17.12, lodash@^4.17.13, lodash@^4.17.1
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.15.tgz#b447f6670a0455bbfeedd11392eff330ea097548"
integrity sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A==

lodash@^4.17.19:
version "4.17.19"
resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.19.tgz#e48ddedbe30b3321783c5b4301fbd353bc1e4a4b"
integrity sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==

log-symbols@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/log-symbols/-/log-symbols-3.0.0.tgz#f3a08516a5dea893336a7dee14d18a1cfdab77c4"
Expand Down

0 comments on commit 5c4ae36

Please sign in to comment.