From 1a1a191eb76c8034d66a4e832f596fc63438975c Mon Sep 17 00:00:00 2001 From: Spencer Date: Wed, 23 Feb 2022 09:36:30 -0600 Subject: [PATCH] [jest] add *_node presets (#126192) --- packages/kbn-es/jest.config.js | 2 +- packages/kbn-es/jest.integration.config.js | 2 +- packages/kbn-optimizer/jest.config.js | 2 +- .../kbn-optimizer/jest.integration.config.js | 2 +- packages/kbn-plugin-generator/jest.config.js | 2 +- .../jest.integration.config.js | 2 +- .../jest.integration.config.js | 2 +- packages/kbn-test/BUILD.bazel | 2 + packages/kbn-test/jest-preset.js | 5 +- packages/kbn-test/jest.config.js | 2 +- packages/kbn-test/jest.integration.config.js | 2 +- .../kbn-test/jest_integration/jest-preset.js | 3 +- .../jest_integration_node/jest-preset.js | 46 +++++++++++++++++++ packages/kbn-test/jest_node/jest-preset.js | 16 +++++++ packages/kbn-test/src/jest/setup/mocks.eui.js | 18 ++++++++ .../{mocks.js => mocks.moment_timezone.js} | 9 ---- .../{polyfills.js => polyfills.jsdom.js} | 0 src/dev/precommit_hook/casing_check_config.js | 2 +- 18 files changed, 97 insertions(+), 22 deletions(-) create mode 100644 packages/kbn-test/jest_integration_node/jest-preset.js create mode 100644 packages/kbn-test/jest_node/jest-preset.js create mode 100644 packages/kbn-test/src/jest/setup/mocks.eui.js rename packages/kbn-test/src/jest/setup/{mocks.js => mocks.moment_timezone.js} (80%) rename packages/kbn-test/src/jest/setup/{polyfills.js => polyfills.jsdom.js} (100%) diff --git a/packages/kbn-es/jest.config.js b/packages/kbn-es/jest.config.js index 7ca76c8104a649..02aa1ca63facf6 100644 --- a/packages/kbn-es/jest.config.js +++ b/packages/kbn-es/jest.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test', + preset: '@kbn/test/jest_node', rootDir: '../..', roots: ['/packages/kbn-es'], }; diff --git a/packages/kbn-es/jest.integration.config.js b/packages/kbn-es/jest.integration.config.js index 58ed5614f26be2..664cee8512fc1c 100644 --- a/packages/kbn-es/jest.integration.config.js +++ b/packages/kbn-es/jest.integration.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test/jest_integration', + preset: '@kbn/test/jest_integration_node', rootDir: '../..', roots: ['/packages/kbn-es'], }; diff --git a/packages/kbn-optimizer/jest.config.js b/packages/kbn-optimizer/jest.config.js index 91218bf815992f..c7e938a072660f 100644 --- a/packages/kbn-optimizer/jest.config.js +++ b/packages/kbn-optimizer/jest.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test', + preset: '@kbn/test/jest_node', rootDir: '../..', roots: ['/packages/kbn-optimizer'], }; diff --git a/packages/kbn-optimizer/jest.integration.config.js b/packages/kbn-optimizer/jest.integration.config.js index 7357f8f6a34b06..a51f4c588af8c1 100644 --- a/packages/kbn-optimizer/jest.integration.config.js +++ b/packages/kbn-optimizer/jest.integration.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test/jest_integration', + preset: '@kbn/test/jest_integration_node', rootDir: '../..', roots: ['/packages/kbn-optimizer'], }; diff --git a/packages/kbn-plugin-generator/jest.config.js b/packages/kbn-plugin-generator/jest.config.js index 34f1be77b48933..f27dfa6ca42db1 100644 --- a/packages/kbn-plugin-generator/jest.config.js +++ b/packages/kbn-plugin-generator/jest.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test', + preset: '@kbn/test/jest_node', rootDir: '../..', roots: ['/packages/kbn-plugin-generator'], }; diff --git a/packages/kbn-plugin-generator/jest.integration.config.js b/packages/kbn-plugin-generator/jest.integration.config.js index 0eac4b764101a5..3031e2c7bd03ab 100644 --- a/packages/kbn-plugin-generator/jest.integration.config.js +++ b/packages/kbn-plugin-generator/jest.integration.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test/jest_integration', + preset: '@kbn/test/jest_integration_node', rootDir: '../..', roots: ['/packages/kbn-plugin-generator'], }; diff --git a/packages/kbn-plugin-helpers/jest.integration.config.js b/packages/kbn-plugin-helpers/jest.integration.config.js index 069989abc01e39..eb7de8ae1596e6 100644 --- a/packages/kbn-plugin-helpers/jest.integration.config.js +++ b/packages/kbn-plugin-helpers/jest.integration.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test/jest_integration', + preset: '@kbn/test/jest_integration_node', rootDir: '../..', roots: ['/packages/kbn-plugin-helpers'], }; diff --git a/packages/kbn-test/BUILD.bazel b/packages/kbn-test/BUILD.bazel index 33d299058168db..233aeab6250b19 100644 --- a/packages/kbn-test/BUILD.bazel +++ b/packages/kbn-test/BUILD.bazel @@ -30,6 +30,8 @@ filegroup( NPM_MODULE_EXTRA_FILES = [ "jest-preset.js", "jest_integration/jest-preset.js", + "jest_integration_node/jest-preset.js", + "jest_node/jest-preset.js", "jest.config.js", "README.md", "package.json", diff --git a/packages/kbn-test/jest-preset.js b/packages/kbn-test/jest-preset.js index 3c9b222e98df5e..9dad901f5eb272 100644 --- a/packages/kbn-test/jest-preset.js +++ b/packages/kbn-test/jest-preset.js @@ -66,14 +66,15 @@ module.exports = { // The paths to modules that run some code to configure or set up the testing environment before each test setupFiles: [ '/node_modules/@kbn/test/target_node/jest/setup/babel_polyfill.js', - '/node_modules/@kbn/test/target_node/jest/setup/polyfills.js', + '/node_modules/@kbn/test/target_node/jest/setup/polyfills.jsdom.js', '/node_modules/@kbn/test/target_node/jest/setup/enzyme.js', ], // A list of paths to modules that run some code to configure or set up the testing framework before each test setupFilesAfterEnv: [ '/node_modules/@kbn/test/target_node/jest/setup/setup_test.js', - '/node_modules/@kbn/test/target_node/jest/setup/mocks.js', + '/node_modules/@kbn/test/target_node/jest/setup/mocks.moment_timezone.js', + '/node_modules/@kbn/test/target_node/jest/setup/mocks.eui.js', '/node_modules/@kbn/test/target_node/jest/setup/react_testing_library.js', ], diff --git a/packages/kbn-test/jest.config.js b/packages/kbn-test/jest.config.js index 6a4847174dde0b..ad5513c7ef31c9 100644 --- a/packages/kbn-test/jest.config.js +++ b/packages/kbn-test/jest.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test', + preset: '@kbn/test/jest_node', rootDir: '../..', roots: ['/packages/kbn-test'], }; diff --git a/packages/kbn-test/jest.integration.config.js b/packages/kbn-test/jest.integration.config.js index 091a7a73de484c..0141c86e8fa59b 100644 --- a/packages/kbn-test/jest.integration.config.js +++ b/packages/kbn-test/jest.integration.config.js @@ -7,7 +7,7 @@ */ module.exports = { - preset: '@kbn/test/jest_integration', + preset: '@kbn/test/jest_integration_node', rootDir: '../..', roots: ['/packages/kbn-test'], }; diff --git a/packages/kbn-test/jest_integration/jest-preset.js b/packages/kbn-test/jest_integration/jest-preset.js index 1d665a4e6a16c2..f5593e3f57fb6f 100644 --- a/packages/kbn-test/jest_integration/jest-preset.js +++ b/packages/kbn-test/jest_integration/jest-preset.js @@ -16,7 +16,8 @@ module.exports = { ), setupFilesAfterEnv: [ '/node_modules/@kbn/test/target_node/jest/setup/after_env.integration.js', - '/node_modules/@kbn/test/target_node/jest/setup/mocks.js', + '/node_modules/@kbn/test/target_node/jest/setup/mocks.moment_timezone.js', + '/node_modules/@kbn/test/target_node/jest/setup/mocks.eui.js', ], reporters: [ 'default', diff --git a/packages/kbn-test/jest_integration_node/jest-preset.js b/packages/kbn-test/jest_integration_node/jest-preset.js new file mode 100644 index 00000000000000..50f097f0386d31 --- /dev/null +++ b/packages/kbn-test/jest_integration_node/jest-preset.js @@ -0,0 +1,46 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +const preset = require('../jest-preset'); + +/** @typedef {import("@jest/types").Config.InitialOptions} JestConfig */ +/** @type {JestConfig} */ +module.exports = { + ...preset, + testMatch: ['**/integration_tests**/*.test.{js,mjs,ts,tsx}'], + testPathIgnorePatterns: preset.testPathIgnorePatterns.filter( + (pattern) => !pattern.includes('integration_tests') + ), + setupFilesAfterEnv: [ + '/node_modules/@kbn/test/target_node/jest/setup/after_env.integration.js', + '/node_modules/@kbn/test/target_node/jest/setup/mocks.moment_timezone.js', + ], + reporters: [ + 'default', + [ + '@kbn/test/target_node/jest/junit_reporter', + { + rootDirectory: '.', + reportName: 'Jest Integration Tests', + }, + ], + [ + '@kbn/test/target_node/jest/ci_stats_jest_reporter', + { + testGroupType: 'Jest Integration Tests', + }, + ], + ], + coverageReporters: !!process.env.CI + ? [['json', { file: 'jest-integration.json' }]] + : ['html', 'text'], + + testEnvironment: 'node', + snapshotSerializers: [], + setupFiles: ['/node_modules/@kbn/test/target_node/jest/setup/babel_polyfill.js'], +}; diff --git a/packages/kbn-test/jest_node/jest-preset.js b/packages/kbn-test/jest_node/jest-preset.js new file mode 100644 index 00000000000000..9a0e9a16321e48 --- /dev/null +++ b/packages/kbn-test/jest_node/jest-preset.js @@ -0,0 +1,16 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +const preset = require('../jest-preset'); + +module.exports = { + ...preset, + testEnvironment: 'node', + snapshotSerializers: [], + setupFiles: ['/node_modules/@kbn/test/target_node/jest/setup/babel_polyfill.js'], +}; diff --git a/packages/kbn-test/src/jest/setup/mocks.eui.js b/packages/kbn-test/src/jest/setup/mocks.eui.js new file mode 100644 index 00000000000000..caf7f8df6eb5f0 --- /dev/null +++ b/packages/kbn-test/src/jest/setup/mocks.eui.js @@ -0,0 +1,18 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +/* eslint-env jest */ + +jest.mock('@elastic/eui/lib/services/react', () => { + // `enqueueStateChange` is an EUI method to batch queued functions that trigger React `setState` calls. + // This is for performance, but when used in certain Jest scernarios it can be nondeterministic. + // Jest tests are never concerned about the state prior to batch completion, so we bypass batching entirely. + return { + enqueueStateChange: (fn) => fn(), + }; +}); diff --git a/packages/kbn-test/src/jest/setup/mocks.js b/packages/kbn-test/src/jest/setup/mocks.moment_timezone.js similarity index 80% rename from packages/kbn-test/src/jest/setup/mocks.js rename to packages/kbn-test/src/jest/setup/mocks.moment_timezone.js index df6e51b782f210..bfe1e58f60b121 100644 --- a/packages/kbn-test/src/jest/setup/mocks.js +++ b/packages/kbn-test/src/jest/setup/mocks.moment_timezone.js @@ -33,12 +33,3 @@ jest.mock('moment-timezone', () => { moment.tz.setDefault('America/New_York'); return moment; }); - -jest.mock('@elastic/eui/lib/services/react', () => { - // `enqueueStateChange` is an EUI method to batch queued functions that trigger React `setState` calls. - // This is for performance, but when used in certain Jest scernarios it can be nondeterministic. - // Jest tests are never concerned about the state prior to batch completion, so we bypass batching entirely. - return { - enqueueStateChange: (fn) => fn(), - }; -}); diff --git a/packages/kbn-test/src/jest/setup/polyfills.js b/packages/kbn-test/src/jest/setup/polyfills.jsdom.js similarity index 100% rename from packages/kbn-test/src/jest/setup/polyfills.js rename to packages/kbn-test/src/jest/setup/polyfills.jsdom.js diff --git a/src/dev/precommit_hook/casing_check_config.js b/src/dev/precommit_hook/casing_check_config.js index e5581631f6baf7..860886811da547 100644 --- a/src/dev/precommit_hook/casing_check_config.js +++ b/src/dev/precommit_hook/casing_check_config.js @@ -39,7 +39,7 @@ export const IGNORE_FILE_GLOBS = [ 'vars/*', '.ci/pipeline-library/**/*', 'packages/kbn-test/jest-preset.js', - 'packages/kbn-test/jest_integration/jest-preset.js', + 'packages/kbn-test/*/jest-preset.js', 'test/package/Vagrantfile', '**/test/**/fixtures/**/*',