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

test:jest improvements to better support our monorepo #84848

Merged
merged 4 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 1 addition & 3 deletions .ci/teamcity/default/jest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,5 @@ source "$(dirname "${0}")/../util.sh"

export JOB=kibana-default-jest

cd "$XPACK_DIR"

checks-reporter-with-killswitch "Jest Unit Tests" \
node scripts/jest --bail --debug
node scripts/jest x-pack --ci --verbose --maxWorkers=5
2 changes: 1 addition & 1 deletion .ci/teamcity/oss/jest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ source "$(dirname "${0}")/../util.sh"
export JOB=kibana-oss-jest

checks-reporter-with-killswitch "OSS Jest Unit Tests" \
node scripts/jest --ci --verbose
node scripts/jest --config jest.config.oss.js --ci --verbose --maxWorkers=5
2 changes: 1 addition & 1 deletion .ci/teamcity/oss/jest_integration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ source "$(dirname "${0}")/../util.sh"
export JOB=kibana-oss-jest-integration

checks-reporter-with-killswitch "OSS Jest Integration Tests" \
node scripts/jest_integration --verbose
node scripts/jest_integration --ci --verbose
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ We use functional tests to make sure the {kib} UI works as expected. It replaces
[discrete]
=== Running functional tests

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]. If you’re writing a plugin outside the {kib} repo, you will have your own config file.
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.
See <<external-plugin-functional-tests>> for more info.

There are three ways to run the tests depending on your goals:
Expand Down
106 changes: 62 additions & 44 deletions docs/developer/contributing/development-tests.asciidoc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[[development-tests]]
== Testing

To ensure that your changes will not break other functionality, please run the test suite and build (<<building-kibana>>) before submitting your Pull Request.

[discrete]
=== Running specific {kib} tests

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

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

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

|Functional
|`test/*integration/**/config.js` `test/*functional/**/config.js` `test/accessibility/config.js`
|`yarn test:ftr:server --config test/[directory]/config.js``yarn test:ftr:runner --config test/[directory]/config.js --grep=regexp`
|`test/**/config.js` `x-pack/test/**/config.js`
|`node scripts/functional_tests_server --config [directory]/config.js``node scripts/functional_test_runner_ --config [directory]/config.js --grep=regexp`
|===

For X-Pack tests located in `x-pack/` see
link:{kib-repo}tree/{branch}/x-pack/README.md#testing[X-Pack Testing]

Test runner arguments: - Where applicable, the optional arguments
`-t=regexp` or `--grep=regexp` will only run tests or test suites
`--grep=regexp` will only run tests or test suites
whose descriptions matches the regular expression. - `[test path]` is
the relative path to the test file.

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

[discrete]
=== Cross-browser compatibility
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.

**Testing IE on OS X**
[source,bash]
----
yarn jest --config src/plugins/dashboard/jest.config.js
----

**Note:** IE11 is not supported from 7.9 onwards.
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.

[source,bash]
----
kibana/src/plugins/dashboard/server$ yarn test:jest #or
kibana/src/plugins/dashboard$ yarn test:jest server #or
kibana$ yarn test:jest src/plugins/dashboard/server
----

Any additional options supplied to `test:jest` will be passed onto the Jest CLI with the resulting Jest command always being outputted.

[source,bash]
----
kibana/src/plugins/dashboard/server$ yarn test:jest --coverage
# is equivelant to
yarn jest --coverage --verbose --config /home/tyler/elastic/kibana/src/plugins/dashboard/jest.config.js server
----

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.

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

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

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

include::development-accessibility-tests.asciidoc[leveloffset=+1]
include::development-accessibility-tests.asciidoc[leveloffset=+1]

[discrete]
=== Cross-browser compatibility

**Testing IE on OS X**

**Note:** IE11 is not supported from 7.9 onwards.

* http://www.vmware.com/products/fusion/fusion-evaluation.html[Download
VMWare Fusion].
* https://developer.microsoft.com/en-us/microsoft-edge/tools/vms/#downloads[Download
IE virtual machines] for VMWare.
* Open VMWare and go to Window > Virtual Machine Library. Unzip the
virtual machine and drag the .vmx file into your Virtual Machine
Library.
* Right-click on the virtual machine you just added to your library and
select "`Snapshots…`", and then click the "`Take`" button in the modal
that opens. You can roll back to this snapshot when the VM expires in 90
days.
* In System Preferences > Sharing, change your computer name to be
something simple, e.g. "`computer`".
* Run {kib} with `yarn start --host=computer.local` (substituting
your computer name).
* Now you can run your VM, open the browser, and navigate to
`http://computer.local:5601` to test {kib}.
* Alternatively you can use browserstack
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,12 @@
"preinstall": "node ./preinstall_check",
"kbn": "node scripts/kbn",
"es": "node scripts/es",
"test": "grunt test",
"test:jest": "node scripts/jest",
"test:jest_integration": "node scripts/jest_integration",
"test:mocha": "node scripts/mocha",
"test:ftr": "node scripts/functional_tests",
"test:ftr:server": "node scripts/functional_tests_server",
"test:ftr:runner": "node scripts/functional_test_runner",
"test:coverage": "grunt test:coverage",
"checkLicenses": "node scripts/check_licenses --dev",
"build": "node scripts/build --all-platforms",
"start": "node scripts/kibana --dev",
Expand Down
2 changes: 2 additions & 0 deletions packages/kbn-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ export * from './functional_test_runner';
export { getUrl } from './jest/utils/get_url';

export { runCheckJestConfigsCli } from './jest/run_check_jest_configs_cli';

export { runJest } from './jest/run';
38 changes: 38 additions & 0 deletions packages/kbn-test/src/jest/run.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.
*/

import { commonBasePath } from './run';

describe('commonBasePath', () => {
it('returns a common path', () => {
expect(commonBasePath(['foo/bar/baz', 'foo/bar/quux', 'foo/bar'])).toBe('foo/bar');
});

it('handles an empty array', () => {
expect(commonBasePath([])).toBe('');
});

it('handles no common path', () => {
expect(commonBasePath(['foo', 'bar'])).toBe('');
});

it('matches full paths', () => {
expect(commonBasePath(['foo/bar', 'foo/bar_baz'])).toBe('foo');
});
});
110 changes: 110 additions & 0 deletions packages/kbn-test/src/jest/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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.
*/

// Run Jest tests
//
// Provides Jest with `--config` to the first jest.config.js file found in the current
// directory, or while going up in the directory chain. If the current working directory
// is nested under the config path, a pattern will be provided to Jest to only run the
// tests within that directory.
//
// Any additional options passed will be forwarded to Jest.
//
// See all cli options in https://facebook.github.io/jest/docs/cli.html

import { resolve, relative, sep as osSep } from 'path';
import { existsSync } from 'fs';
import { run } from 'jest';
import { buildArgv } from 'jest-cli/build/cli';
import { ToolingLog } from '@kbn/dev-utils';

// yarn test:jest src/core/server/saved_objects
// yarn test:jest src/core/public/core_system.test.ts
// :kibana/src/core/server/saved_objects yarn test:jest

export function runJest(configName = 'jest.config.js') {
const argv = buildArgv(process.argv);

const log = new ToolingLog({
level: argv.verbose ? 'verbose' : 'info',
writeTo: process.stdout,
});

if (!argv.config) {
const cwd = process.env.INIT_CWD || process.cwd();
const testFiles = argv._.splice(2).map((p) => resolve(cwd, p));
const commonTestFiles = commonBasePath(testFiles);
const testFilesProvided = testFiles.length > 0;

log.verbose('cwd:', cwd);
log.verbose('testFiles:', testFiles.join(', '));
log.verbose('commonTestFiles:', commonTestFiles);

let configPath;

// sets the working directory to the cwd or the common
// base directory of the provided test files
let wd = testFilesProvided ? commonTestFiles : cwd;

configPath = resolve(wd, configName);

while (!existsSync(configPath)) {
wd = resolve(wd, '..');
configPath = resolve(wd, configName);
}

log.verbose(`no config provided, found ${configPath}`);
process.argv.push('--config', configPath);

if (!testFilesProvided) {
log.verbose(`no test files provided, setting to current directory`);
process.argv.push(relative(wd, cwd));
}

log.info('yarn jest', process.argv.slice(2).join(' '));
}

if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}

run();
}

/**
* Finds the common basePath by sorting the array
* and comparing the first and last element
*/
export function commonBasePath(paths: string[] = [], sep = osSep) {
if (paths.length === 0) return '';

paths = paths.concat().sort();

const first = paths[0].split(sep);
const last = paths[paths.length - 1].split(sep);

const length = first.length;
let i = 0;

while (i < length && first[i] === last[i]) {
i++;
}

return first.slice(0, i).join(sep);
}
25 changes: 1 addition & 24 deletions scripts/jest.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,4 @@
* under the License.
*/

// # Run Jest tests
//
// All args will be forwarded directly to Jest, e.g. to watch tests run:
//
// node scripts/jest --watch
//
// or to build code coverage:
//
// node scripts/jest --coverage
//
// See all cli options in https://facebook.github.io/jest/docs/cli.html

if (process.argv.indexOf('--config') === -1) {
// append correct jest.config if none is provided
var configPath = require('path').resolve(__dirname, '../jest.config.oss.js');
process.argv.push('--config', configPath);
console.log('Running Jest with --config', configPath);
}

if (process.env.NODE_ENV == null) {
process.env.NODE_ENV = 'test';
}

require('jest').run();
require('@kbn/test').runJest();
Loading