Skip to content

Commit a0edc1c

Browse files
Merge branch 'master' into be_search_1
2 parents 87d90a0 + 92bb8e7 commit a0edc1c

File tree

15 files changed

+193
-108
lines changed

15 files changed

+193
-108
lines changed

packages/kbn-test/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"devDependencies": {
1313
"@babel/cli": "^7.10.5",
14+
"@jest/types": "^26.5.2",
1415
"@kbn/babel-preset": "1.0.0",
1516
"@kbn/dev-utils": "1.0.0",
1617
"@kbn/utils": "1.0.0",
@@ -22,14 +23,18 @@
2223
"diff": "^4.0.1"
2324
},
2425
"dependencies": {
26+
"@jest/reporters": "^26.5.2",
2527
"chalk": "^4.1.0",
2628
"dedent": "^0.7.0",
2729
"del": "^5.1.0",
30+
"execa": "^4.0.2",
2831
"exit-hook": "^2.2.0",
2932
"getopts": "^2.2.5",
3033
"glob": "^7.1.2",
34+
"globby": "^8.0.1",
3135
"joi": "^13.5.2",
3236
"lodash": "^4.17.20",
37+
"mustache": "^2.3.2",
3338
"parse-link-header": "^1.0.1",
3439
"rxjs": "^6.5.5",
3540
"strip-ansi": "^6.0.0",

packages/kbn-test/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export {
5555

5656
export { runFailedTestsReporterCli } from './failed_tests_reporter';
5757

58-
export { makeJunitReportPath } from './junit_report_path';
59-
6058
export { CI_PARALLEL_PROCESS_PREFIX } from './ci_parallel_process_prefix';
6159

6260
export * from './functional_test_runner';
61+
62+
export * from './jest';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export * from './junit_reporter';
21+
22+
export * from './report_path';
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
const { resolve } = require('path');
21+
const { REPO_ROOT } = require('@kbn/utils');
22+
23+
module.exports = {
24+
reporters: [
25+
'default',
26+
[
27+
`${REPO_ROOT}/packages/kbn-test/target/jest/junit_reporter`,
28+
{
29+
reportName: 'JUnit Reporter Integration Test',
30+
rootDirectory: resolve(
31+
REPO_ROOT,
32+
'packages/kbn-test/src/jest/integration_tests/__fixtures__'
33+
),
34+
},
35+
],
36+
],
37+
};

src/dev/jest/integration_tests/__fixtures__/test.js renamed to packages/kbn-test/src/jest/integration_tests/__fixtures__/test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* under the License.
1818
*/
1919

20-
it('fails', () => {
21-
throw new Error('failure');
20+
describe('JUnit Reporter', () => {
21+
it('fails', () => {
22+
throw new Error('failure');
23+
});
2224
});

src/dev/jest/integration_tests/junit_reporter.test.js renamed to packages/kbn-test/src/jest/integration_tests/junit_reporter.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,26 @@ import del from 'del';
2525
import execa from 'execa';
2626
import xml2js from 'xml2js';
2727
import { makeJunitReportPath } from '@kbn/test';
28+
import { REPO_ROOT } from '@kbn/utils';
2829

2930
const MINUTE = 1000 * 60;
30-
const ROOT_DIR = resolve(__dirname, '../../../../');
3131
const FIXTURE_DIR = resolve(__dirname, '__fixtures__');
3232
const TARGET_DIR = resolve(FIXTURE_DIR, 'target');
33-
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'Jest Tests');
33+
const XML_PATH = makeJunitReportPath(FIXTURE_DIR, 'JUnit Reporter Integration Test');
3434

3535
afterAll(async () => {
3636
await del(TARGET_DIR);
3737
});
3838

3939
const parseXml = promisify(xml2js.parseString);
40-
4140
it(
4241
'produces a valid junit report for failures',
4342
async () => {
4443
const result = await execa(
45-
process.execPath,
46-
['-r', require.resolve('../../../setup_node_env'), require.resolve('jest/bin/jest')],
44+
'./node_modules/.bin/jest',
45+
['--config', 'packages/kbn-test/src/jest/integration_tests/__fixtures__/jest.config.js'],
4746
{
48-
cwd: FIXTURE_DIR,
47+
cwd: REPO_ROOT,
4948
env: {
5049
CI: 'true',
5150
},
@@ -57,6 +56,7 @@ it(
5756
await expect(parseXml(readFileSync(XML_PATH, 'utf8'))).resolves.toEqual({
5857
testsuites: {
5958
$: {
59+
failures: '1',
6060
name: 'jest',
6161
skipped: '0',
6262
tests: '1',
@@ -67,7 +67,7 @@ it(
6767
{
6868
$: {
6969
failures: '1',
70-
file: resolve(ROOT_DIR, 'src/dev/jest/integration_tests/__fixtures__/test.js'),
70+
file: resolve(FIXTURE_DIR, './test.js'),
7171
name: 'test.js',
7272
skipped: '0',
7373
tests: '1',
@@ -77,8 +77,8 @@ it(
7777
testcase: [
7878
{
7979
$: {
80-
classname: 'Jest Tests.·',
81-
name: 'fails',
80+
classname: 'JUnit Reporter Integration Test.·',
81+
name: 'JUnit Reporter fails',
8282
time: expect.anything(),
8383
},
8484
failure: [expect.stringMatching(/Error: failure\s+at /m)],

src/dev/jest/junit_reporter.js renamed to packages/kbn-test/src/jest/junit_reporter.ts

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,31 @@ import { writeFileSync, mkdirSync } from 'fs';
2222

2323
import xmlBuilder from 'xmlbuilder';
2424

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

27-
const ROOT_DIR = dirname(require.resolve('../../../package.json'));
28+
import { escapeCdata } from '../mocha/xml';
29+
import { makeJunitReportPath } from './report_path';
30+
31+
interface ReporterOptions {
32+
reportName?: string;
33+
rootDirectory?: string;
34+
}
2835

2936
/**
3037
* Jest reporter that produces JUnit report when running on CI
3138
* @class JestJUnitReporter
3239
*/
33-
export default class JestJUnitReporter {
34-
constructor(globalConfig, options = {}) {
35-
const { reportName = 'Jest Tests', rootDirectory = ROOT_DIR } = options;
3640

37-
this._reportName = reportName;
38-
this._rootDirectory = resolve(rootDirectory);
41+
// eslint-disable-next-line import/no-default-export
42+
export default class JestJUnitReporter extends BaseReporter {
43+
private _reportName: string;
44+
private _rootDirectory: string;
45+
46+
constructor(globalConfig: Config.GlobalConfig, { rootDirectory, reportName }: ReporterOptions) {
47+
super();
48+
this._reportName = reportName || 'Jest Tests';
49+
this._rootDirectory = rootDirectory ? resolve(rootDirectory) : resolve(__dirname, '../..');
3950
}
4051

4152
/**
@@ -44,7 +55,7 @@ export default class JestJUnitReporter {
4455
* @param {JestResults} results see https://facebook.github.io/jest/docs/en/configuration.html#testresultsprocessor-string
4556
* @return {undefined}
4657
*/
47-
onRunComplete(contexts, results) {
58+
onRunComplete(contexts: Set<Test['context']>, results: AggregatedResult): void {
4859
if (!process.env.CI || process.env.DISABLE_JUNIT_REPORTER || !results.testResults.length) {
4960
return;
5061
}
@@ -55,18 +66,19 @@ export default class JestJUnitReporter {
5566
'testsuites',
5667
{ encoding: 'utf-8' },
5768
{},
58-
{ skipNullAttributes: true }
69+
{ keepNullAttributes: false }
5970
);
6071

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

6476
root.att({
6577
name: 'jest',
6678
timestamp: msToIso(results.startTime),
6779
time: msToSec(Date.now() - results.startTime),
6880
tests: results.numTotalTests,
69-
failures: results.numFailingTests,
81+
failures: results.numFailedTests,
7082
skipped: results.numPendingTests,
7183
});
7284

packages/kbn-test/src/junit_report_path.ts renamed to packages/kbn-test/src/jest/report_path.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
import { resolve } from 'path';
21-
import { CI_PARALLEL_PROCESS_PREFIX } from './ci_parallel_process_prefix';
21+
import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix';
2222

2323
export function makeJunitReportPath(rootDirectory: string, reportName: string) {
2424
return resolve(

src/dev/jest/config.integration.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ export default {
3131
),
3232
reporters: [
3333
'default',
34-
['<rootDir>/src/dev/jest/junit_reporter.js', { reportName: 'Jest Integration Tests' }],
34+
[
35+
'<rootDir>/packages/kbn-test/target/jest/junit_reporter',
36+
{ reportName: 'Jest Integration Tests' },
37+
],
3538
],
3639
setupFilesAfterEnv: ['<rootDir>/src/dev/jest/setup/after_env.integration.js'],
3740
};

src/dev/jest/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,5 +99,5 @@ export default {
9999
'<rootDir>/src/plugins/kibana_react/public/util/test_helpers/react_mount_serializer.ts',
100100
'<rootDir>/node_modules/enzyme-to-json/serializer',
101101
],
102-
reporters: ['default', '<rootDir>/src/dev/jest/junit_reporter.js'],
102+
reporters: ['default', '<rootDir>/packages/kbn-test/target/jest/junit_reporter'],
103103
};

0 commit comments

Comments
 (0)