Skip to content

Commit

Permalink
feat(add skip test option): want to skip the pending tests and suites…
Browse files Browse the repository at this point in the history
… in the final report (#299)

* feat(add skip test option): want to skip the pending tests and suites in the final report

we need to have an option in the test config as statusIgnoreFilter: 'pending'. This will skip the
pending suites and tests in the report and it will have a clear report only with failures and passes
tests

#298

* fix test
  • Loading branch information
Hazyzh authored Dec 20, 2023
1 parent b5da42f commit 6482271
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 42 deletions.
29 changes: 23 additions & 6 deletions helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import fs from 'fs-extra';
import os from 'os';
import path from 'path';
import { AggregatedResult } from '@jest/test-result';

let username: string = '';
try {
Expand Down Expand Up @@ -206,6 +207,7 @@ const INLINE_SOURCE = 'inlineSource';
const URL_FOR_TEST_FILES = 'urlForTestFiles';
const DARK_THEME = 'darkTheme';
const INCLUDE_CONSOLE_LOG = 'includeConsoleLog';
const STRIP_SKIPPED_TEST = 'stripSkippedTest';

const constants = {
ENVIRONMENT_CONFIG_MAP: {
Expand All @@ -225,6 +227,7 @@ const constants = {
JEST_HTML_REPORTERS_URL_FOR_TEST_FILES: URL_FOR_TEST_FILES,
JEST_HTML_REPORTERS_DARK_THEME: DARK_THEME,
JEST_HTML_REPORTERS_INCLUDE_CONSOLE_LOG: INCLUDE_CONSOLE_LOG,
JEST_HTML_REPORTERS_STRIP_SKIPPED_TEST: STRIP_SKIPPED_TEST,
},
DEFAULT_OPTIONS: {
[PUBLIC_PATH]: process.cwd(),
Expand All @@ -243,6 +246,7 @@ const constants = {
[URL_FOR_TEST_FILES]: '',
[DARK_THEME]: false,
[INCLUDE_CONSOLE_LOG]: false,
[STRIP_SKIPPED_TEST]: false,
},
};

Expand Down Expand Up @@ -344,19 +348,19 @@ const basisClone = (obj: any, structure: StructureMetaData) => {
if (typeof obj !== 'object') return obj;

if (Array.isArray(obj)) {
return obj.map(item => basisClone(item, structure))
return obj.map((item) => basisClone(item, structure));
}

const { keys } = structure;
const res = {};
keys.forEach(item => {
keys.forEach((item) => {
if (typeof item === 'string') {
res[item] = obj[item];
} else {
const [key, innerStructure] = item;
res[key] = basisClone(obj[key], innerStructure)
res[key] = basisClone(obj[key], innerStructure);
}
})
});
return res;
};

Expand All @@ -366,6 +370,19 @@ export const deepClone = <T>(obj: T): T => {
};

const getSerializableContent = (content: string | object) => {
if (typeof content === 'string') return content;
if (typeof content === 'string') return content;
return JSON.stringify(content, null, 2);
}
};

export const filterSkipTests = (obj: AggregatedResult): AggregatedResult => {
obj.testResults.forEach((item) => {
item.numPendingTests = 0;
item.testResults = item.testResults.filter(
(test) => !(test.status === 'skipped' || test.status === 'pending')
);
});
obj.testResults = obj.testResults.filter(
(i) => !(i.skipped || i.testResults.length === 0)
);
return obj;
};
9 changes: 8 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
replaceRootDirVariable,
resourceDirName,
tempDirPath,
filterSkipTests,
} from './helper';

const localTemplateHTMLPath = path.resolve(__dirname, './dist/index.html');
Expand Down Expand Up @@ -115,14 +116,15 @@ class MyCustomReporter {
}

async onRunComplete(contexts, originalResults) {
const results = deepClone(originalResults) as any;
let results = deepClone(originalResults) as any;
const {
publicPath,
filename,
logoImgPath,
customInfos,
openReport,
failureMessageOnly,
stripSkippedTest,
} = this._options;
const logoImg = logoImgPath ? imgToBase64(logoImgPath) : undefined;
results.config = this._globalConfig;
Expand Down Expand Up @@ -161,6 +163,11 @@ class MyCustomReporter {
}
}

// filter skipped tests if stripSkippedTest
if (stripSkippedTest) {
results = filterSkipTests(results);
}

const data = JSON.stringify(results);
const filePath = path.resolve(publicPath, filename);
// fs.writeFileSync('./src/devMock.json', data);
Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ module.exports = {
filename: 'report.html',
enableMergeData: true,
dataMergeLevel: 2,
// openReport: true,
openReport: true,
inlineSource: true,
darkTheme: true,
stripSkippedTest: true,
},
],
],
Expand Down
67 changes: 33 additions & 34 deletions test/ReporterClass/index.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import {
getOptions,
replaceRootDirVariable,
} from '../../helper';
import { getOptions, replaceRootDirVariable } from '../../helper';

describe("get options ", () => {
describe('get options ', () => {
const OLD_ENV = process.env;
beforeEach(() => {
jest.resetModules();
Expand All @@ -14,74 +11,76 @@ describe("get options ", () => {
process.env = OLD_ENV; // restore old env
});

test("should return default option with no config", () => {
test('env should return default option with no config', () => {
const defaultOption = {
publicPath: process.cwd(),
filename: "jest_html_reporters.html",
filename: 'jest_html_reporters.html',
expand: false,
pageTitle: "",
pageTitle: '',
logoImgPath: undefined,
hideIcon: false,
customInfos: undefined,
failureMessageOnly: 0,
openReport: false,
testCommand: "",
testCommand: '',
enableMergeData: false,
dataMergeLevel: 1,
inlineSource: false,
urlForTestFiles: "",
urlForTestFiles: '',
darkTheme: false,
includeConsoleLog: false,
stripSkippedTest: false,
};
expect(getOptions()).toEqual(defaultOption);
});

test("env option should works for option", () => {
test('env option should works for option', () => {
const envOptions = {
publicPath: "./test",
filename: "file.html",
publicPath: './test',
filename: 'file.html',
expand: true,
pageTitle: "hello",
logoImgPath: "./logo.svg",
pageTitle: 'hello',
logoImgPath: './logo.svg',
hideIcon: true,
customInfos: "{a: 1}",
testCommand: "yarn test",
customInfos: '{a: 1}',
testCommand: 'yarn test',
openReport: false,
failureMessageOnly: 0,
enableMergeData: false,
dataMergeLevel: 1,
inlineSource: true,
urlForTestFiles: "https://github.com/Hazyzh/jest-html-reporters/tree/master",
urlForTestFiles:
'https://github.com/Hazyzh/jest-html-reporters/tree/master',
darkTheme: false,
includeConsoleLog: false,
stripSkippedTest: false,
};
process.env = {
JEST_HTML_REPORTERS_PUBLIC_PATH: "./test",
JEST_HTML_REPORTERS_FILE_NAME: "file.html",
JEST_HTML_REPORTERS_PUBLIC_PATH: './test',
JEST_HTML_REPORTERS_FILE_NAME: 'file.html',
JEST_HTML_REPORTERS_EXPAND: true,
JEST_HTML_REPORTERS_PAGE_TITLE: "hello",
JEST_HTML_REPORTERS_LOGO_IMG_PATH: "./logo.svg",
JEST_HTML_REPORTERS_PAGE_TITLE: 'hello',
JEST_HTML_REPORTERS_LOGO_IMG_PATH: './logo.svg',
JEST_HTML_REPORTERS_HIDE_ICON: true,
JEST_HTML_REPORTERS_CUSTOM_INFOS: "{a: 1}",
JEST_HTML_REPORTERS_TEST_COMMAND: "yarn test",
JEST_HTML_REPORTERS_CUSTOM_INFOS: '{a: 1}',
JEST_HTML_REPORTERS_TEST_COMMAND: 'yarn test',
JEST_HTML_REPORTERS_INLINE_SOURCE: true,
JEST_HTML_REPORTERS_URL_FOR_TEST_FILES: "https://github.com/Hazyzh/jest-html-reporters/tree/master",
JEST_HTML_REPORTERS_URL_FOR_TEST_FILES:
'https://github.com/Hazyzh/jest-html-reporters/tree/master',
};
expect(getOptions()).toEqual(envOptions);
});
});

describe('test replaceRootDirVariable function', () => {
test.each`
publicPath | rootDirPath | result
${'<rootDir>/html-report'} | ${'/Users/harry'} | ${'/Users/harry/html-report'}
${'./html-report'} | ${'/Users/harry'} | ${'./html-report'}
${'/html-report/<rootDir>'} | ${'/Users/harry'} | ${'/html-report/<rootDir>'}
${'<rootDir>/html-report<rootDir>/html-report'} | ${'/Users/harry'} | ${'/Users/harry/html-report<rootDir>/html-report'}
`('should return correct value', (
{ publicPath, rootDirPath, result }
) => {
publicPath | rootDirPath | result
${'<rootDir>/html-report'} | ${'/Users/harry'} | ${'/Users/harry/html-report'}
${'./html-report'} | ${'/Users/harry'} | ${'./html-report'}
${'/html-report/<rootDir>'} | ${'/Users/harry'} | ${'/html-report/<rootDir>'}
${'<rootDir>/html-report<rootDir>/html-report'} | ${'/Users/harry'} | ${'/Users/harry/html-report<rootDir>/html-report'}
`('should return correct value', ({ publicPath, rootDirPath, result }) => {
const execResult = replaceRootDirVariable(publicPath, rootDirPath);
expect(execResult).toBe(result);
})
});
});

0 comments on commit 6482271

Please sign in to comment.