Skip to content

Commit 2bbb283

Browse files
committed
test:jest improvements to better support our monorepo (#84848)
Signed-off-by: Tyler Smalley <tyler.smalley@elastic.co> # Conflicts: # docs/developer/contributing/development-functional-tests.asciidoc # test/scripts/jenkins_unit.sh # test/scripts/jenkins_xpack.sh # x-pack/README.md
1 parent 7cae069 commit 2bbb283

32 files changed

Lines changed: 281 additions & 192 deletions

File tree

.ci/teamcity/default/jest.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,5 @@ source "$(dirname "${0}")/../util.sh"
66

77
export JOB=kibana-default-jest
88

9-
cd "$XPACK_DIR"
10-
119
checks-reporter-with-killswitch "Jest Unit Tests" \
12-
node scripts/jest --bail --debug
10+
node scripts/jest x-pack --ci --verbose --maxWorkers=5

.ci/teamcity/oss/jest.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ source "$(dirname "${0}")/../util.sh"
77
export JOB=kibana-oss-jest
88

99
checks-reporter-with-killswitch "OSS Jest Unit Tests" \
10-
node scripts/jest --ci --verbose
10+
node scripts/jest --config jest.config.oss.js --ci --verbose --maxWorkers=5

.ci/teamcity/oss/jest_integration.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ source "$(dirname "${0}")/../util.sh"
77
export JOB=kibana-oss-jest-integration
88

99
checks-reporter-with-killswitch "OSS Jest Integration Tests" \
10-
node scripts/jest_integration --verbose
10+
node scripts/jest_integration --ci --verbose

docs/developer/contributing/development-functional-tests.asciidoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ We use functional tests to make sure the {kib} UI works as expected. It replaces
66
[discrete]
77
=== Running functional tests
88

9-
The `FunctionalTestRunner` is very bare bones and gets most of its functionality from its config file, located at link:{kib-repo}tree/{branch}/test/functional/config.js[test/functional/config.js]. If you’re writing a plugin outside the {kib} repo, you will have your own config file.
9+
The `FunctionalTestRunner` is very bare bones and gets most of its functionality from its config file, located at {blob}test/functional/config.js[test/functional/config.js] or {blob}x-pack/test/functional/config.js[x-pack/test/functional/config.js]. If you’re writing a plugin outside the {kib} repo, you will have your own config file.
1010
See <<external-plugin-functional-tests>> for more info.
1111

1212
There are three ways to run the tests depending on your goals:
Lines changed: 62 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
[[development-tests]]
22
== Testing
33

4-
To ensure that your changes will not break other functionality, please run the test suite and build (<<building-kibana>>) before submitting your Pull Request.
5-
64
[discrete]
75
=== Running specific {kib} tests
86

@@ -13,63 +11,57 @@ invoke them:
1311
|===
1412
|Test runner |Test location |Runner command (working directory is {kib}
1513
root)
16-
|Jest |`src/**/*.test.js` `src/**/*.test.ts`
17-
|`yarn test:jest -t regexp [test path]`
14+
|Jest |`**/*.test.{js,mjs,ts,tsx}`
15+
|`yarn test:jest [test path]`
1816

19-
|Jest (integration) |`**/integration_tests/**/*.test.js`
20-
|`yarn test:jest_integration -t regexp [test path]`
17+
|Jest (integration) |`**/integration_tests/**/*.test.{js,mjs,ts,tsx}`
18+
|`yarn test:jest_integration [test path]`
2119

2220
|Mocha
23-
|`src/**/__tests__/**/*.js` `!src/**/public/__tests__/*.js` `packages/kbn-dev-utils/src/**/__tests__/**/*.js` `tasks/**/__tests__/**/*.js`
21+
|`**/__tests__/**/*.js`
2422
|`node scripts/mocha --grep=regexp [test path]`
2523

2624
|Functional
27-
|`test/*integration/**/config.js` `test/*functional/**/config.js` `test/accessibility/config.js`
28-
|`yarn test:ftr:server --config test/[directory]/config.js``yarn test:ftr:runner --config test/[directory]/config.js --grep=regexp`
25+
|`test/**/config.js` `x-pack/test/**/config.js`
26+
|`node scripts/functional_tests_server --config [directory]/config.js``node scripts/functional_test_runner_ --config [directory]/config.js --grep=regexp`
2927
|===
3028

31-
For X-Pack tests located in `x-pack/` see
32-
link:{kib-repo}tree/{branch}/x-pack/README.md#testing[X-Pack Testing]
33-
3429
Test runner arguments: - Where applicable, the optional arguments
35-
`-t=regexp` or `--grep=regexp` will only run tests or test suites
30+
`--grep=regexp` will only run tests or test suites
3631
whose descriptions matches the regular expression. - `[test path]` is
3732
the relative path to the test file.
3833

39-
Examples: - Run the entire elasticsearch_service test suite:
40-
`yarn test:jest src/core/server/elasticsearch/elasticsearch_service.test.ts`
41-
- Run the jest test case whose description matches
42-
`stops both admin and data clients`:
43-
`yarn test:jest -t 'stops both admin and data clients' src/core/server/elasticsearch/elasticsearch_service.test.ts`
44-
- Run the api integration test case whose description matches the given
45-
string: ``` yarn test:ftr:server –config test/api_integration/config.js
46-
yarn test:ftr:runner –config test/api_integration/config
34+
=== Unit Testing
4735

48-
[discrete]
49-
=== Cross-browser compatibility
36+
Kibana primarily uses Jest for unit testing. Each plugin or package defines a `jest.config.js` that extends link:{kib-repo}tree/{branch}/packages/kbn-test/jest-preset.js[a preset] provided by the link:{kib-repo}tree/{branch}/packages/kbn-test[`@kbn/test`] package. Unless you intend to run all unit tests within the project, it's most efficient to provide the Jest configuration file for the plugin or package you're testing.
5037

51-
**Testing IE on OS X**
38+
[source,bash]
39+
----
40+
yarn jest --config src/plugins/dashboard/jest.config.js
41+
----
5242

53-
**Note:** IE11 is not supported from 7.9 onwards.
43+
A script is available to provide a better user experience when testing while navigating throughout the repository. To run the tests within your current working directory, use `yarn test:jest`. Like the Jest CLI, you can also supply a path to determine which tests to run.
44+
45+
[source,bash]
46+
----
47+
kibana/src/plugins/dashboard/server$ yarn test:jest #or
48+
kibana/src/plugins/dashboard$ yarn test:jest server #or
49+
kibana$ yarn test:jest src/plugins/dashboard/server
50+
----
51+
52+
Any additional options supplied to `test:jest` will be passed onto the Jest CLI with the resulting Jest command always being outputted.
53+
54+
[source,bash]
55+
----
56+
kibana/src/plugins/dashboard/server$ yarn test:jest --coverage
57+
58+
# is equivelant to
59+
60+
yarn jest --coverage --verbose --config /home/tyler/elastic/kibana/src/plugins/dashboard/jest.config.js server
61+
----
62+
63+
NOTE: There are still a handful of legacy tests that use the Mocha test runner. For those tests, use `node scripts/mocha --grep=regexp [test path]`. Tests using Mocha are located within `__tests__` directories.
5464

55-
* http://www.vmware.com/products/fusion/fusion-evaluation.html[Download
56-
VMWare Fusion].
57-
* https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/#downloads[Download
58-
IE virtual machines] for VMWare.
59-
* Open VMWare and go to Window > Virtual Machine Library. Unzip the
60-
virtual machine and drag the .vmx file into your Virtual Machine
61-
Library.
62-
* Right-click on the virtual machine you just added to your library and
63-
select "`Snapshots…`", and then click the "`Take`" button in the modal
64-
that opens. You can roll back to this snapshot when the VM expires in 90
65-
days.
66-
* In System Preferences > Sharing, change your computer name to be
67-
something simple, e.g. "`computer`".
68-
* Run {kib} with `yarn start --host=computer.local` (substituting
69-
your computer name).
70-
* Now you can run your VM, open the browser, and navigate to
71-
`http://computer.local:5601` to test {kib}.
72-
* Alternatively you can use browserstack
7365

7466
[discrete]
7567
=== Running browser automation tests
@@ -93,4 +85,30 @@ include::development-functional-tests.asciidoc[leveloffset=+1]
9385

9486
include::development-unit-tests.asciidoc[leveloffset=+1]
9587

96-
include::development-accessibility-tests.asciidoc[leveloffset=+1]
88+
include::development-accessibility-tests.asciidoc[leveloffset=+1]
89+
90+
[discrete]
91+
=== Cross-browser compatibility
92+
93+
**Testing IE on OS X**
94+
95+
**Note:** IE11 is not supported from 7.9 onwards.
96+
97+
* http://www.vmware.com/products/fusion/fusion-evaluation.html[Download
98+
VMWare Fusion].
99+
* https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/#downloads[Download
100+
IE virtual machines] for VMWare.
101+
* Open VMWare and go to Window > Virtual Machine Library. Unzip the
102+
virtual machine and drag the .vmx file into your Virtual Machine
103+
Library.
104+
* Right-click on the virtual machine you just added to your library and
105+
select "`Snapshots…`", and then click the "`Take`" button in the modal
106+
that opens. You can roll back to this snapshot when the VM expires in 90
107+
days.
108+
* In System Preferences > Sharing, change your computer name to be
109+
something simple, e.g. "`computer`".
110+
* Run {kib} with `yarn start --host=computer.local` (substituting
111+
your computer name).
112+
* Now you can run your VM, open the browser, and navigate to
113+
`http://computer.local:5601` to test {kib}.
114+
* Alternatively you can use browserstack

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
"preinstall": "node ./preinstall_check",
4444
"kbn": "node scripts/kbn",
4545
"es": "node scripts/es",
46-
"test": "grunt test",
4746
"test:jest": "node scripts/jest",
4847
"test:jest_integration": "node scripts/jest_integration",
4948
"test:mocha": "node scripts/mocha",
5049
"test:ftr": "node scripts/functional_tests",
5150
"test:ftr:server": "node scripts/functional_tests_server",
5251
"test:ftr:runner": "node scripts/functional_test_runner",
53-
"test:coverage": "grunt test:coverage",
5452
"checkLicenses": "node scripts/check_licenses --dev",
5553
"build": "node scripts/build --all-platforms",
5654
"start": "node scripts/kibana --dev",

packages/kbn-test/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,5 @@ export * from './functional_test_runner';
6262
export { getUrl } from './jest/utils/get_url';
6363

6464
export { runCheckJestConfigsCli } from './jest/run_check_jest_configs_cli';
65+
66+
export { runJest } from './jest/run';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
import { commonBasePath } from './run';
21+
22+
describe('commonBasePath', () => {
23+
it('returns a common path', () => {
24+
expect(commonBasePath(['foo/bar/baz', 'foo/bar/quux', 'foo/bar'])).toBe('foo/bar');
25+
});
26+
27+
it('handles an empty array', () => {
28+
expect(commonBasePath([])).toBe('');
29+
});
30+
31+
it('handles no common path', () => {
32+
expect(commonBasePath(['foo', 'bar'])).toBe('');
33+
});
34+
35+
it('matches full paths', () => {
36+
expect(commonBasePath(['foo/bar', 'foo/bar_baz'])).toBe('foo');
37+
});
38+
});

packages/kbn-test/src/jest/run.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
// Run Jest tests
21+
//
22+
// Provides Jest with `--config` to the first jest.config.js file found in the current
23+
// directory, or while going up in the directory chain. If the current working directory
24+
// is nested under the config path, a pattern will be provided to Jest to only run the
25+
// tests within that directory.
26+
//
27+
// Any additional options passed will be forwarded to Jest.
28+
//
29+
// See all cli options in https://facebook.github.io/jest/docs/cli.html
30+
31+
import { resolve, relative, sep as osSep } from 'path';
32+
import { existsSync } from 'fs';
33+
import { run } from 'jest';
34+
import { buildArgv } from 'jest-cli/build/cli';
35+
import { ToolingLog } from '@kbn/dev-utils';
36+
37+
// yarn test:jest src/core/server/saved_objects
38+
// yarn test:jest src/core/public/core_system.test.ts
39+
// :kibana/src/core/server/saved_objects yarn test:jest
40+
41+
export function runJest(configName = 'jest.config.js') {
42+
const argv = buildArgv(process.argv);
43+
44+
const log = new ToolingLog({
45+
level: argv.verbose ? 'verbose' : 'info',
46+
writeTo: process.stdout,
47+
});
48+
49+
if (!argv.config) {
50+
const cwd = process.env.INIT_CWD || process.cwd();
51+
const testFiles = argv._.splice(2).map((p) => resolve(cwd, p));
52+
const commonTestFiles = commonBasePath(testFiles);
53+
const testFilesProvided = testFiles.length > 0;
54+
55+
log.verbose('cwd:', cwd);
56+
log.verbose('testFiles:', testFiles.join(', '));
57+
log.verbose('commonTestFiles:', commonTestFiles);
58+
59+
let configPath;
60+
61+
// sets the working directory to the cwd or the common
62+
// base directory of the provided test files
63+
let wd = testFilesProvided ? commonTestFiles : cwd;
64+
65+
configPath = resolve(wd, configName);
66+
67+
while (!existsSync(configPath)) {
68+
wd = resolve(wd, '..');
69+
configPath = resolve(wd, configName);
70+
}
71+
72+
log.verbose(`no config provided, found ${configPath}`);
73+
process.argv.push('--config', configPath);
74+
75+
if (!testFilesProvided) {
76+
log.verbose(`no test files provided, setting to current directory`);
77+
process.argv.push(relative(wd, cwd));
78+
}
79+
80+
log.info('yarn jest', process.argv.slice(2).join(' '));
81+
}
82+
83+
if (process.env.NODE_ENV == null) {
84+
process.env.NODE_ENV = 'test';
85+
}
86+
87+
run();
88+
}
89+
90+
/**
91+
* Finds the common basePath by sorting the array
92+
* and comparing the first and last element
93+
*/
94+
export function commonBasePath(paths: string[] = [], sep = osSep) {
95+
if (paths.length === 0) return '';
96+
97+
paths = paths.concat().sort();
98+
99+
const first = paths[0].split(sep);
100+
const last = paths[paths.length - 1].split(sep);
101+
102+
const length = first.length;
103+
let i = 0;
104+
105+
while (i < length && first[i] === last[i]) {
106+
i++;
107+
}
108+
109+
return first.slice(0, i).join(sep);
110+
}

scripts/jest.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,4 @@
1717
* under the License.
1818
*/
1919

20-
// # Run Jest tests
21-
//
22-
// All args will be forwarded directly to Jest, e.g. to watch tests run:
23-
//
24-
// node scripts/jest --watch
25-
//
26-
// or to build code coverage:
27-
//
28-
// node scripts/jest --coverage
29-
//
30-
// See all cli options in https://facebook.github.io/jest/docs/cli.html
31-
32-
if (process.argv.indexOf('--config') === -1) {
33-
// append correct jest.config if none is provided
34-
var configPath = require('path').resolve(__dirname, '../jest.config.oss.js');
35-
process.argv.push('--config', configPath);
36-
console.log('Running Jest with --config', configPath);
37-
}
38-
39-
if (process.env.NODE_ENV == null) {
40-
process.env.NODE_ENV = 'test';
41-
}
42-
43-
require('jest').run();
20+
require('@kbn/test').runJest();

0 commit comments

Comments
 (0)