Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Jest JUnit reporter to TS #79919

Merged
merged 5 commits into from
Oct 12, 2020
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
5 changes: 5 additions & 0 deletions packages/kbn-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"devDependencies": {
"@babel/cli": "^7.10.5",
"@jest/types": "^26.5.2",
"@kbn/babel-preset": "1.0.0",
"@kbn/dev-utils": "1.0.0",
"@kbn/utils": "1.0.0",
Expand All @@ -22,14 +23,18 @@
"diff": "^4.0.1"
},
"dependencies": {
"@jest/reporters": "^26.5.2",
"chalk": "^4.1.0",
"dedent": "^0.7.0",
"del": "^5.1.0",
"execa": "^4.0.2",
"exit-hook": "^2.2.0",
"getopts": "^2.2.5",
"glob": "^7.1.2",
"globby": "^8.0.1",
"joi": "^13.5.2",
"lodash": "^4.17.20",
"mustache": "^2.3.2",
"parse-link-header": "^1.0.1",
"rxjs": "^6.5.5",
"strip-ansi": "^6.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ export {

export { runFailedTestsReporterCli } from './failed_tests_reporter';

export { makeJunitReportPath } from './junit_report_path';

export { CI_PARALLEL_PROCESS_PREFIX } from './ci_parallel_process_prefix';

export * from './functional_test_runner';

export * from './jest';
22 changes: 22 additions & 0 deletions packages/kbn-test/src/jest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export * from './junit_reporter';

export * from './report_path';
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

const { resolve } = require('path');
const { REPO_ROOT } = require('@kbn/utils');

module.exports = {
reporters: [
'default',
[
`${REPO_ROOT}/packages/kbn-test/target/jest/junit_reporter`,
{
reportName: 'JUnit Reporter Integration Test',
rootDirectory: resolve(
REPO_ROOT,
'packages/kbn-test/src/jest/integration_tests/__fixtures__'
),
},
],
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

it('fails', () => {
throw new Error('failure');
describe('JUnit Reporter', () => {
it('fails', () => {
throw new Error('failure');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,26 @@ import del from 'del';
import execa from 'execa';
import xml2js from 'xml2js';
import { makeJunitReportPath } from '@kbn/test';
import { REPO_ROOT } from '@kbn/utils';

const MINUTE = 1000 * 60;
const ROOT_DIR = resolve(__dirname, '../../../../');
const FIXTURE_DIR = resolve(__dirname, '__fixtures__');
const TARGET_DIR = resolve(FIXTURE_DIR, 'target');
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'Jest Tests');
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');

afterAll(async () => {
await del(TARGET_DIR);
});

const parseXml = promisify(xml2js.parseString);

it(
'produces a valid junit report for failures',
async () => {
const result = await execa(
process.execPath,
['-r', require.resolve('../../../setup_node_env'), require.resolve('jest/bin/jest')],
'./node_modules/.bin/jest',
['--config', 'packages/kbn-test/src/jest/integration_tests/__fixtures__/jest.config.js'],
{
cwd: FIXTURE_DIR,
cwd: REPO_ROOT,
env: {
CI: 'true',
},
Expand All @@ -57,6 +56,7 @@ it(
await expect(parseXml(readFileSync(XML_PATH, 'utf8'))).resolves.toEqual({
testsuites: {
$: {
failures: '1',
name: 'jest',
skipped: '0',
tests: '1',
Expand All @@ -67,7 +67,7 @@ it(
{
$: {
failures: '1',
file: resolve(ROOT_DIR, 'src/dev/jest/integration_tests/__fixtures__/test.js'),
file: resolve(FIXTURE_DIR, './test.js'),
name: 'test.js',
skipped: '0',
tests: '1',
Expand All @@ -77,8 +77,8 @@ it(
testcase: [
{
$: {
classname: 'Jest Tests.·',
name: 'fails',
classname: 'JUnit Reporter Integration Test.·',
name: 'JUnit Reporter fails',
time: expect.anything(),
},
failure: [expect.stringMatching(/Error: failure\s+at /m)],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,31 @@ import { writeFileSync, mkdirSync } from 'fs';

import xmlBuilder from 'xmlbuilder';

import { escapeCdata, makeJunitReportPath } from '@kbn/test';
import type { Config } from '@jest/types';
import { AggregatedResult, Test, BaseReporter } from '@jest/reporters';

const ROOT_DIR = dirname(require.resolve('../../../package.json'));
import { escapeCdata } from '../mocha/xml';
import { makeJunitReportPath } from './report_path';

interface ReporterOptions {
reportName?: string;
rootDirectory?: string;
}

/**
* Jest reporter that produces JUnit report when running on CI
* @class JestJUnitReporter
*/
export default class JestJUnitReporter {
constructor(globalConfig, options = {}) {
const { reportName = 'Jest Tests', rootDirectory = ROOT_DIR } = options;

this._reportName = reportName;
this._rootDirectory = resolve(rootDirectory);
// eslint-disable-next-line import/no-default-export
export default class JestJUnitReporter extends BaseReporter {
private _reportName: string;
private _rootDirectory: string;

constructor(globalConfig: Config.GlobalConfig, { rootDirectory, reportName }: ReporterOptions) {
super();
this._reportName = reportName || 'Jest Tests';
this._rootDirectory = rootDirectory ? resolve(rootDirectory) : resolve(__dirname, '../..');
}

/**
Expand All @@ -44,7 +55,7 @@ export default class JestJUnitReporter {
* @param {JestResults} results see https://facebook.github.io/jest/docs/en/configuration.html#testresultsprocessor-string
* @return {undefined}
*/
onRunComplete(contexts, results) {
onRunComplete(contexts: Set<Test['context']>, results: AggregatedResult): void {
if (!process.env.CI || process.env.DISABLE_JUNIT_REPORTER || !results.testResults.length) {
return;
}
Expand All @@ -55,18 +66,19 @@ export default class JestJUnitReporter {
'testsuites',
{ encoding: 'utf-8' },
{},
{ skipNullAttributes: true }
{ keepNullAttributes: false }
Copy link
Contributor Author

@tylersmalley tylersmalley Oct 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was incorrect option.

);

const msToIso = (ms) => (ms ? new Date(ms).toISOString().slice(0, -5) : undefined);
const msToSec = (ms) => (ms ? (ms / 1000).toFixed(3) : undefined);
const msToIso = (ms: number | null | undefined) =>
ms ? new Date(ms).toISOString().slice(0, -5) : undefined;
const msToSec = (ms: number | null | undefined) => (ms ? (ms / 1000).toFixed(3) : undefined);

root.att({
name: 'jest',
timestamp: msToIso(results.startTime),
time: msToSec(Date.now() - results.startTime),
tests: results.numTotalTests,
failures: results.numFailingTests,
failures: results.numFailedTests,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was incorrect property.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱

skipped: results.numPendingTests,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { resolve } from 'path';
import { CI_PARALLEL_PROCESS_PREFIX } from './ci_parallel_process_prefix';
import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix';

export function makeJunitReportPath(rootDirectory: string, reportName: string) {
return resolve(
Expand Down
5 changes: 4 additions & 1 deletion src/dev/jest/config.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ export default {
),
reporters: [
'default',
['<rootDir>/src/dev/jest/junit_reporter.js', { reportName: 'Jest Integration Tests' }],
[
'<rootDir>/packages/kbn-test/target/jest/junit_reporter',
{ reportName: 'Jest Integration Tests' },
],
],
setupFilesAfterEnv: ['<rootDir>/src/dev/jest/setup/after_env.integration.js'],
};
2 changes: 1 addition & 1 deletion src/dev/jest/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,5 @@ export default {
'<rootDir>/src/plugins/kibana_react/public/util/test_helpers/react_mount_serializer.ts',
'<rootDir>/node_modules/enzyme-to-json/serializer',
],
reporters: ['default', '<rootDir>/src/dev/jest/junit_reporter.js'],
reporters: ['default', '<rootDir>/packages/kbn-test/target/jest/junit_reporter'],
};
14 changes: 0 additions & 14 deletions src/dev/jest/integration_tests/__fixtures__/package.json

This file was deleted.

2 changes: 1 addition & 1 deletion x-pack/dev-tools/jest/create_jest_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export function createJestConfig({ kibanaDirectory, rootDir, xPackKibanaDirector
reporters: [
'default',
[
`${kibanaDirectory}/src/dev/jest/junit_reporter.js`,
`${kibanaDirectory}/packages/kbn-test/target/jest/junit_reporter`,
{
reportName: 'X-Pack Jest Tests',
},
Expand Down
5 changes: 4 additions & 1 deletion x-pack/test_utils/jest/config.integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default {
),
reporters: [
'default',
['<rootDir>/../src/dev/jest/junit_reporter.js', { reportName: 'Jest Integration Tests' }],
[
'<rootDir>/../packages/kbn-test/target/jest/junit_reporter',
{ reportName: 'Jest Integration Tests' },
],
],
setupFilesAfterEnv: ['<rootDir>/../src/dev/jest/setup/after_env.integration.js'],
};
2 changes: 1 addition & 1 deletion x-pack/test_utils/jest/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ export default {
},
transformIgnorePatterns: ['[/\\\\]node_modules[/\\\\].+\\.js$', 'packages/kbn-pm/dist/index.js'],
snapshotSerializers: ['<rootDir>/../node_modules/enzyme-to-json/serializer'],
reporters: ['default', '<rootDir>/../src/dev/jest/junit_reporter.js'],
reporters: ['default', '<rootDir>/../packages/kbn-test/target/jest/junit_reporter'],
};
Loading