From 2bbb2837f4caabe4e77d4a2b0f394e113647a027 Mon Sep 17 00:00:00 2001 From: Tyler Smalley Date: Mon, 14 Dec 2020 14:07:50 -0800 Subject: [PATCH] test:jest improvements to better support our monorepo (#84848) Signed-off-by: Tyler Smalley # Conflicts: # docs/developer/contributing/development-functional-tests.asciidoc # test/scripts/jenkins_unit.sh # test/scripts/jenkins_xpack.sh # x-pack/README.md --- .ci/teamcity/default/jest.sh | 4 +- .ci/teamcity/oss/jest.sh | 2 +- .ci/teamcity/oss/jest_integration.sh | 2 +- .../development-functional-tests.asciidoc | 2 +- .../contributing/development-tests.asciidoc | 106 ++++++++++------- package.json | 2 - packages/kbn-test/src/index.ts | 2 + packages/kbn-test/src/jest/run.test.ts | 38 ++++++ packages/kbn-test/src/jest/run.ts | 110 ++++++++++++++++++ scripts/jest.js | 25 +--- scripts/jest_integration.js | 25 +--- .../server/saved_objects/migrations/README.md | 8 +- src/plugins/embeddable/README.asciidoc | 2 +- test/scripts/jenkins_unit.sh | 2 +- test/scripts/jenkins_xpack.sh | 11 +- test/scripts/test/jest_integration.sh | 2 +- test/scripts/test/jest_unit.sh | 2 +- test/scripts/test/xpack_jest_unit.sh | 4 +- x-pack/README.md | 37 +----- x-pack/package.json | 2 +- x-pack/plugins/beats_management/readme.md | 8 +- .../plugins/encrypted_saved_objects/README.md | 9 +- x-pack/plugins/enterprise_search/README.md | 6 +- x-pack/plugins/event_log/README.md | 7 +- x-pack/plugins/graph/README.md | 2 +- x-pack/plugins/lens/readme.md | 2 +- x-pack/plugins/maps/README.md | 2 +- x-pack/plugins/ml/readme.md | 12 +- x-pack/plugins/task_manager/README.md | 6 +- x-pack/plugins/transform/readme.md | 12 +- x-pack/plugins/uptime/README.md | 6 +- x-pack/scripts/jest.js | 13 +-- 32 files changed, 281 insertions(+), 192 deletions(-) create mode 100644 packages/kbn-test/src/jest/run.test.ts create mode 100644 packages/kbn-test/src/jest/run.ts diff --git a/.ci/teamcity/default/jest.sh b/.ci/teamcity/default/jest.sh index 93ca7f76f3a21..b900d1b6d6b4e 100755 --- a/.ci/teamcity/default/jest.sh +++ b/.ci/teamcity/default/jest.sh @@ -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 diff --git a/.ci/teamcity/oss/jest.sh b/.ci/teamcity/oss/jest.sh index 3ba9ab0c31c57..0dee07d00d2be 100755 --- a/.ci/teamcity/oss/jest.sh +++ b/.ci/teamcity/oss/jest.sh @@ -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 diff --git a/.ci/teamcity/oss/jest_integration.sh b/.ci/teamcity/oss/jest_integration.sh index 1a23c46c8a2c2..4c51d2ff29888 100755 --- a/.ci/teamcity/oss/jest_integration.sh +++ b/.ci/teamcity/oss/jest_integration.sh @@ -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 diff --git a/docs/developer/contributing/development-functional-tests.asciidoc b/docs/developer/contributing/development-functional-tests.asciidoc index 929006ae954a7..b4560c5042188 100644 --- a/docs/developer/contributing/development-functional-tests.asciidoc +++ b/docs/developer/contributing/development-functional-tests.asciidoc @@ -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 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. +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 <> for more info. There are three ways to run the tests depending on your goals: diff --git a/docs/developer/contributing/development-tests.asciidoc b/docs/developer/contributing/development-tests.asciidoc index 4cf667195153d..647dc8b3f3b26 100644 --- a/docs/developer/contributing/development-tests.asciidoc +++ b/docs/developer/contributing/development-tests.asciidoc @@ -1,8 +1,6 @@ [[development-tests]] == Testing -To ensure that your changes will not break other functionality, please run the test suite and build (<>) before submitting your Pull Request. - [discrete] === Running specific {kib} tests @@ -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 @@ -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] \ No newline at end of file +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 \ No newline at end of file diff --git a/package.json b/package.json index f1ff86782a8b0..8710d0405d20b 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/packages/kbn-test/src/index.ts b/packages/kbn-test/src/index.ts index 54b064f5cd49e..a88820eb281cc 100644 --- a/packages/kbn-test/src/index.ts +++ b/packages/kbn-test/src/index.ts @@ -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'; diff --git a/packages/kbn-test/src/jest/run.test.ts b/packages/kbn-test/src/jest/run.test.ts new file mode 100644 index 0000000000000..5be033baade6a --- /dev/null +++ b/packages/kbn-test/src/jest/run.test.ts @@ -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'); + }); +}); diff --git a/packages/kbn-test/src/jest/run.ts b/packages/kbn-test/src/jest/run.ts new file mode 100644 index 0000000000000..3283b6c8901fa --- /dev/null +++ b/packages/kbn-test/src/jest/run.ts @@ -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); +} diff --git a/scripts/jest.js b/scripts/jest.js index 90f8da10f4c90..cb31d7785898d 100755 --- a/scripts/jest.js +++ b/scripts/jest.js @@ -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(); diff --git a/scripts/jest_integration.js b/scripts/jest_integration.js index f07d28939ef0c..1df79781fe26d 100755 --- a/scripts/jest_integration.js +++ b/scripts/jest_integration.js @@ -17,29 +17,6 @@ * under the License. */ -// # Run Jest integration tests -// -// All args will be forwarded directly to Jest, e.g. to watch tests run: -// -// node scripts/jest_integration --watch -// -// or to build code coverage: -// -// node scripts/jest_integration --coverage -// -// See all cli options in https://facebook.github.io/jest/docs/cli.html - process.argv.push('--runInBand'); -if (process.argv.indexOf('--config') === -1) { - // append correct jest.config if none is provided - var configPath = require('path').resolve(__dirname, '../jest.config.integration.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('jest.config.integration.js'); diff --git a/src/core/server/saved_objects/migrations/README.md b/src/core/server/saved_objects/migrations/README.md index 91249024358ac..7db0b38773a67 100644 --- a/src/core/server/saved_objects/migrations/README.md +++ b/src/core/server/saved_objects/migrations/README.md @@ -206,9 +206,13 @@ There are three core entry points. ## Testing -Run jest tests: +Run Jest tests: -`node scripts/jest --testPathPattern=migrations --watch` +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +``` +yarn test:jest src/core/server/saved_objects/migrations --watch +``` Run integration tests: diff --git a/src/plugins/embeddable/README.asciidoc b/src/plugins/embeddable/README.asciidoc index 10ec2b840ffa7..5e3c5066f46c7 100644 --- a/src/plugins/embeddable/README.asciidoc +++ b/src/plugins/embeddable/README.asciidoc @@ -40,5 +40,5 @@ Run unit tests [source,sh] -- -node scripts/jest embeddable +yarn test:jest src/plugins/embeddable -- diff --git a/test/scripts/jenkins_unit.sh b/test/scripts/jenkins_unit.sh index fa37e8f10d47b..475a2cfb6f3c7 100755 --- a/test/scripts/jenkins_unit.sh +++ b/test/scripts/jenkins_unit.sh @@ -25,4 +25,4 @@ source test/scripts/jenkins_test_setup.sh ./test/scripts/checks/plugins_with_circular_deps.sh ./test/scripts/checks/verify_notice.sh ./test/scripts/checks/test_projects.sh -./test/scripts/checks/test_hardening.sh \ No newline at end of file +./test/scripts/checks/test_hardening.sh diff --git a/test/scripts/jenkins_xpack.sh b/test/scripts/jenkins_xpack.sh index d9a97c76c15ee..00eae190238f9 100755 --- a/test/scripts/jenkins_xpack.sh +++ b/test/scripts/jenkins_xpack.sh @@ -3,13 +3,4 @@ source test/scripts/jenkins_test_setup.sh echo " -> Running jest tests" -cd "$XPACK_DIR" -checks-reporter-with-killswitch "X-Pack Jest" node --max-old-space-size=6144 scripts/jest --ci --verbose -echo "" -echo "" - -# echo " -> Running jest integration tests" -# cd "$XPACK_DIR" -# node scripts/jest_integration --ci --verbose -# echo "" -# echo "" \ No newline at end of file +./test/scripts/test/xpack_jest_unit.sh \ No newline at end of file diff --git a/test/scripts/test/jest_integration.sh b/test/scripts/test/jest_integration.sh index 8791248e9a166..78ed804f88430 100755 --- a/test/scripts/test/jest_integration.sh +++ b/test/scripts/test/jest_integration.sh @@ -3,4 +3,4 @@ source src/dev/ci_setup/setup_env.sh checks-reporter-with-killswitch "Jest Integration Tests" \ - node scripts/jest_integration + node scripts/jest_integration --ci --verbose diff --git a/test/scripts/test/jest_unit.sh b/test/scripts/test/jest_unit.sh index de5e16c2b1366..88c0fe528b88c 100755 --- a/test/scripts/test/jest_unit.sh +++ b/test/scripts/test/jest_unit.sh @@ -3,4 +3,4 @@ source src/dev/ci_setup/setup_env.sh checks-reporter-with-killswitch "Jest Unit Tests" \ - node scripts/jest + node scripts/jest --config jest.config.oss.js --ci --verbose --maxWorkers=5 diff --git a/test/scripts/test/xpack_jest_unit.sh b/test/scripts/test/xpack_jest_unit.sh index 93d70ec355391..33b1c8a2b5183 100755 --- a/test/scripts/test/xpack_jest_unit.sh +++ b/test/scripts/test/xpack_jest_unit.sh @@ -2,5 +2,5 @@ source src/dev/ci_setup/setup_env.sh -cd x-pack -checks-reporter-with-killswitch "X-Pack Jest" node --max-old-space-size=6144 scripts/jest --ci --verbose --maxWorkers=10 +checks-reporter-with-killswitch "X-Pack Jest" \ + node scripts/jest x-pack --ci --verbose --maxWorkers=5 diff --git a/x-pack/README.md b/x-pack/README.md index a9ac7be5d23b2..41ea4cc4e469a 100644 --- a/x-pack/README.md +++ b/x-pack/README.md @@ -15,41 +15,8 @@ Example: `yarn es snapshot --license trial --password changeme` By default, this will also set the password for native realm accounts to the password provided (`changeme` by default). This includes that of the `kibana_system` user which `elasticsearch.username` defaults to in development. If you wish to specify a password for a given native realm account, you can do that like so: `--password.kibana_system=notsecure` # Testing -## Running specific tests -| Test runner | Test location | Runner command (working directory is kibana/x-pack) | -| ------------ | ----------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------- | -| Jest | `x-pack/**/*.test.js`
`x-pack/**/*.test.ts` | `cd x-pack && node scripts/jest -t regexp [test path]` | -| Functional | `x-pack/test/*integration/**/config.js`
`x-pack/test/*functional/config.js` | `node scripts/functional_tests_server --config x-pack/test/[directory]/config.js`
`node scripts/functional_test_runner --config x-pack/test/[directory]/config.js --grep=regexp` | -Examples: - - Run the jest test case whose description matches 'filtering should skip values of null': - `cd x-pack && yarn test:jest -t 'filtering should skip values of null' plugins/ml/public/application/explorer/explorer_charts/explorer_charts_container_service.test.js` - - Run the x-pack api integration test case whose description matches the given string: - `node scripts/functional_tests_server --config x-pack/test/api_integration/config.ts` - `node scripts/functional_test_runner --config x-pack/test/api_integration/config.ts --grep='apis Monitoring Beats list with restarted beat instance should load multiple clusters'` - -In addition to to providing a regular expression argument, specific tests can also be run by appeding `.only` to an `it` or `describe` function block. E.g. `describe(` to `describe.only(`. - -## Running all tests - -You can run unit tests by running: - -``` -yarn test -``` - -If you want to run tests only for a specific plugin (to save some time), you can run: - -``` -yarn test --plugins [,]* # where is "reporting", etc. -``` - -#### Running server unit tests -You can run mocha unit tests by running: - -``` -yarn test:mocha -``` +For information on testing, see [the Elastic functional test development guide](https://www.elastic.co/guide/en/kibana/current/development-functional-tests.html). #### Running functional tests @@ -114,7 +81,7 @@ node scripts/functional_tests --config test/security_api_integration/saml.config Jest integration tests can be used to test behavior with Elasticsearch and the Kibana server. ```sh -node scripts/jest_integration +yarn test:jest_integration ``` An example test exists at [test_utils/jest/integration_tests/example_integration.test.ts](test_utils/jest/integration_tests/example_integration.test.ts) diff --git a/x-pack/package.json b/x-pack/package.json index 90cdad0cc48b9..5046def1e3dd8 100644 --- a/x-pack/package.json +++ b/x-pack/package.json @@ -12,7 +12,7 @@ "build": "../node_modules/.bin/gulp build", "testonly": "echo 'Deprecated, use `yarn test`'", "test": "../node_modules/.bin/gulp test", - "test:jest": "node scripts/jest", + "test:jest": "node ../scripts/jest", "test:mocha": "node scripts/mocha" }, "kibana": { diff --git a/x-pack/plugins/beats_management/readme.md b/x-pack/plugins/beats_management/readme.md index 36db612f7affd..75adf428772e4 100644 --- a/x-pack/plugins/beats_management/readme.md +++ b/x-pack/plugins/beats_management/readme.md @@ -7,7 +7,13 @@ Failure to have auth enabled in Kibana will make for a broken UI. UI-based error ### Unit tests -From `~/kibana/x-pack`, run `node scripts/jest.js plugins/beats --watch`. +Run Jest tests: + +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +``` +yarn test:jest x-pack/plugins/beats --watch +``` ### API tests diff --git a/x-pack/plugins/encrypted_saved_objects/README.md b/x-pack/plugins/encrypted_saved_objects/README.md index 0a5e79a96f02a..99ebf771126d5 100644 --- a/x-pack/plugins/encrypted_saved_objects/README.md +++ b/x-pack/plugins/encrypted_saved_objects/README.md @@ -235,9 +235,12 @@ const migration780 = encryptedSavedObjects.createMigration( ### Unit tests -From `kibana-root-folder/x-pack`, run: -```bash -$ node scripts/jest.js +Run Jest tests: + +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +``` +yarn test:jest x-pack/plugins/encrypted_saved_objects --watch ``` ### API Integration tests diff --git a/x-pack/plugins/enterprise_search/README.md b/x-pack/plugins/enterprise_search/README.md index ba14be5564be1..6722e9b712009 100644 --- a/x-pack/plugins/enterprise_search/README.md +++ b/x-pack/plugins/enterprise_search/README.md @@ -25,10 +25,10 @@ To debug Kea state in-browser, Kea recommends [Redux Devtools](https://kea.js.or ### Unit tests -From `kibana-root-folder/x-pack`, run: +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing -```bash -yarn test:jest plugins/enterprise_search +``` +yarn test:jest x-pack/plugins/enterprise_search --watch ``` ### E2E tests diff --git a/x-pack/plugins/event_log/README.md b/x-pack/plugins/event_log/README.md index 941dedc3d1093..eb7fbc9d590fa 100644 --- a/x-pack/plugins/event_log/README.md +++ b/x-pack/plugins/event_log/README.md @@ -53,9 +53,10 @@ public setup(core: CoreSetup, { eventLog }: PluginSetupDependencies) { ### Unit tests -From `kibana-root-folder/x-pack`, run: -```bash -$ node node scripts/jest plugins/event_log +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +``` +yarn test:jest x-pack/plugins/event_log --watch ``` ### API Integration tests diff --git a/x-pack/plugins/graph/README.md b/x-pack/plugins/graph/README.md index 9cc2617abe94c..99becabf70002 100644 --- a/x-pack/plugins/graph/README.md +++ b/x-pack/plugins/graph/README.md @@ -6,7 +6,7 @@ Graph shows only up in the side bar if your server is running on a platinum or t ## Common commands -* Run tests `node x-pack/scripts/jest.js --watch plugins/graph` +* Run tests `yarn test:jest x-pack/plugins/graph --watch` * Run type check `node scripts/type_check.js --project=x-pack/tsconfig.json` * Run linter `node scripts/eslint.js x-pack/plugins/graph` * Run functional tests (make sure to stop dev server) diff --git a/x-pack/plugins/lens/readme.md b/x-pack/plugins/lens/readme.md index 98bb60827af42..9fa6ad8ee30af 100644 --- a/x-pack/plugins/lens/readme.md +++ b/x-pack/plugins/lens/readme.md @@ -4,7 +4,7 @@ Run all tests from the `x-pack` root directory -- Unit tests: `node scripts/jest --watch lens` +- Unit tests: `yarn test:jest x-pack/plugins/lens` - Functional tests: - Run `node scripts/functional_tests_server` - Run `node ../scripts/functional_test_runner.js --config ./test/functional/config.js --grep="lens app"` diff --git a/x-pack/plugins/maps/README.md b/x-pack/plugins/maps/README.md index aae5a708b680b..729cba26f72ab 100644 --- a/x-pack/plugins/maps/README.md +++ b/x-pack/plugins/maps/README.md @@ -7,7 +7,7 @@ Visualize geo data from Elasticsearch or 3rd party geo-services. Run all tests from the `x-pack` root directory -- Unit tests: `node scripts/jest --watch maps` +- Unit tests: `yarn test:jest x-pack/plugins/maps --watch` - Functional tests: - Run `node scripts/functional_tests_server` - Run `node ../scripts/functional_test_runner.js --config ./test/functional/config.js --grep="maps app"` \ No newline at end of file diff --git a/x-pack/plugins/ml/readme.md b/x-pack/plugins/ml/readme.md index 2369f3d077037..b97e7cbc2ee04 100644 --- a/x-pack/plugins/ml/readme.md +++ b/x-pack/plugins/ml/readme.md @@ -68,30 +68,32 @@ These data sets are now ready be analyzed in ML jobs in Kibana. ### Jest tests -Run the test following jest tests from `kibana/x-pack`. +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +Run the test following jest tests from `kibana/x-pack/plugins/ml`. New snapshots, all plugins: ``` -node scripts/jest +yarn test:jest ``` Update snapshots for the ML plugin: ``` -node scripts/jest plugins/ml -u +yarn test:jest -u ``` Update snapshots for a specific directory only: ``` -node scripts/jest plugins/ml/public/application/settings/filter_lists +yarn test:jest public/application/settings/filter_lists ``` Run tests with verbose output: ``` -node scripts/jest plugins/ml --verbose +yarn test:jest --verbose ``` ### Functional tests diff --git a/x-pack/plugins/task_manager/README.md b/x-pack/plugins/task_manager/README.md index d3c8ecb6c4505..6cd42cda9af6a 100644 --- a/x-pack/plugins/task_manager/README.md +++ b/x-pack/plugins/task_manager/README.md @@ -505,9 +505,11 @@ The task manager's public API is create / delete / list. Updates aren't directly ## Testing - Unit tests: + + Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + ``` - cd x-pack - node scripts/jest --testPathPattern=task_manager --watch + yarn test:jest x-pack/plugins/task_manager --watch ``` - Integration tests: ``` diff --git a/x-pack/plugins/transform/readme.md b/x-pack/plugins/transform/readme.md index 07500876f55c2..a1005c43687e2 100644 --- a/x-pack/plugins/transform/readme.md +++ b/x-pack/plugins/transform/readme.md @@ -67,30 +67,32 @@ These data sets are now ready to be used for creating transforms in Kibana. ### Jest tests -Run the test following jest tests from `kibana/x-pack`. +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +Run the test following jest tests from `kibana/x-pack/plugins/transform. New snapshots, all plugins: ``` -node scripts/jest +yarn test:jest ``` Update snapshots for the transform plugin: ``` -node scripts/jest plugins/transform -u +yarn test:jest -u ``` Update snapshots for a specific directory only: ``` -node scripts/jest x-pack/plugins/transform/public/app/sections +yarn test:jest public/app/sections ``` Run tests with verbose output: ``` -node scripts/jest plugins/transform --verbose +yarn test:jest --verbose ``` ### Functional tests diff --git a/x-pack/plugins/uptime/README.md b/x-pack/plugins/uptime/README.md index b386924314af9..d78c5d9579761 100644 --- a/x-pack/plugins/uptime/README.md +++ b/x-pack/plugins/uptime/README.md @@ -42,7 +42,11 @@ There's also a `rest_api` folder that defines the structure of the RESTful API e ### Unit tests -From `~/kibana/x-pack`, run `node scripts/jest.js`. +Documentation: https://www.elastic.co/guide/en/kibana/current/development-tests.html#_unit_testing + +``` +yarn test:jest x-pack/plugins/uptime +``` ### Functional tests diff --git a/x-pack/scripts/jest.js b/x-pack/scripts/jest.js index 68cfcf082f818..aca7e558301df 100644 --- a/x-pack/scripts/jest.js +++ b/x-pack/scripts/jest.js @@ -4,15 +4,4 @@ * you may not use this file except in compliance with the Elastic License. */ -if (process.argv.indexOf('--config') === -1) { - // append correct jest.config if none is provided - const configPath = require('path').resolve(__dirname, '../jest.config.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();