From 4d0d6da52614ce7f4703474e09ab6bc23303472e Mon Sep 17 00:00:00 2001 From: Karen Grigoryan Date: Sun, 5 Jan 2025 01:53:12 +0100 Subject: [PATCH] refactor: update jest transformer configuration in kbn-test The whole point of this change is to make babel-jest transform options shared and reusable by other packages' jest.config transforms that need to add their own custom stuff. For example here ecs_data_quality_dashboard makes use of that to add @emotion/babel-preset-css-prop that is needed to properly compile emotion css prop in jest tests. --- packages/kbn-test/jest-preset.js | 2 +- .../src/jest/transforms/babel/index.js | 13 ++++++++++ .../{babel.js => babel/transformer_config.js} | 12 ++------- .../ecs_data_quality_dashboard/jest.config.js | 8 ++++++ .../jest/babel-transformer.js | 25 +++++++++++++++++++ 5 files changed, 49 insertions(+), 11 deletions(-) create mode 100644 packages/kbn-test/src/jest/transforms/babel/index.js rename packages/kbn-test/src/jest/transforms/{babel.js => babel/transformer_config.js} (77%) create mode 100644 x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js diff --git a/packages/kbn-test/jest-preset.js b/packages/kbn-test/jest-preset.js index 791ee4a974823..7473191cb764c 100644 --- a/packages/kbn-test/jest-preset.js +++ b/packages/kbn-test/jest-preset.js @@ -98,7 +98,7 @@ module.exports = { // A map from regular expressions to paths to transformers transform: { - '^.+\\.(js|tsx?)$': '/packages/kbn-test/src/jest/transforms/babel.js', + '^.+\\.(js|tsx?)$': '/packages/kbn-test/src/jest/transforms/babel/index.js', '^.+\\.(txt|html)?$': '/packages/kbn-test/src/jest/transforms/raw.js', '^.+\\.peggy?$': '/packages/kbn-test/src/jest/transforms/peggy.js', }, diff --git a/packages/kbn-test/src/jest/transforms/babel/index.js b/packages/kbn-test/src/jest/transforms/babel/index.js new file mode 100644 index 0000000000000..4a65028e7931f --- /dev/null +++ b/packages/kbn-test/src/jest/transforms/babel/index.js @@ -0,0 +1,13 @@ +/* + * 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", the "GNU Affero General Public License v3.0 only", 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", the "GNU Affero General Public + * License v3.0 only", or the "Server Side Public License, v 1". + */ + +const babelJest = require('babel-jest'); +const transformerConfig = require('./transformer_config'); + +module.exports = babelJest.default.createTransformer(transformerConfig); diff --git a/packages/kbn-test/src/jest/transforms/babel.js b/packages/kbn-test/src/jest/transforms/babel/transformer_config.js similarity index 77% rename from packages/kbn-test/src/jest/transforms/babel.js rename to packages/kbn-test/src/jest/transforms/babel/transformer_config.js index d9c54ca16751a..4a4b6260035eb 100644 --- a/packages/kbn-test/src/jest/transforms/babel.js +++ b/packages/kbn-test/src/jest/transforms/babel/transformer_config.js @@ -7,9 +7,7 @@ * License v3.0 only", or the "Server Side Public License, v 1". */ -const babelJest = require('babel-jest'); - -module.exports = babelJest.default.createTransformer({ +module.exports = { presets: [ [ require.resolve('@kbn/babel-preset/node_preset'), @@ -21,11 +19,5 @@ module.exports = babelJest.default.createTransformer({ }, }, ], - [ - require.resolve('@emotion/babel-preset-css-prop'), - { - labelFormat: '[filename]--[local]', - }, - ], ], -}); +}; diff --git a/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest.config.js b/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest.config.js index 773d813a166b9..26f3235f7f4b2 100644 --- a/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest.config.js +++ b/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest.config.js @@ -5,6 +5,9 @@ * 2.0. */ +// eslint-disable-next-line import/no-extraneous-dependencies +const rootConfig = require('@kbn/test/jest-preset'); + module.exports = { coverageDirectory: '/target/kibana-coverage/jest/x-pack/solutions/security/packages/ecs_data_quality_dashboard_impl', @@ -23,4 +26,9 @@ module.exports = { setupFilesAfterEnv: [ '/x-pack/solutions/security/packages/ecs_data_quality_dashboard/setup_tests.ts', ], + transform: { + ...rootConfig.transform, + '^.+\\.(js|tsx?)$': + '/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js', + }, }; diff --git a/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js b/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js new file mode 100644 index 0000000000000..d72b0ff2f1521 --- /dev/null +++ b/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js @@ -0,0 +1,25 @@ +/* + * 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; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +// eslint-disable-next-line import/no-extraneous-dependencies +const babelJest = require('babel-jest'); + +// eslint-disable-next-line import/no-extraneous-dependencies +const rootTransformerConfig = require('@kbn/test/src/jest/transforms/babel/transformer_config'); + +module.exports = babelJest.default.createTransformer({ + ...rootTransformerConfig, + presets: [ + ...rootTransformerConfig.presets, + [ + require.resolve('@emotion/babel-preset-css-prop'), + { + labelFormat: '[filename]--[local]', + }, + ], + ], +});