Skip to content

Commit

Permalink
refactor: update jest transformer configuration in kbn-test
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kapral18 committed Jan 5, 2025
1 parent fb54be4 commit 4d0d6da
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-test/jest-preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ module.exports = {

// A map from regular expressions to paths to transformers
transform: {
'^.+\\.(js|tsx?)$': '<rootDir>/packages/kbn-test/src/jest/transforms/babel.js',
'^.+\\.(js|tsx?)$': '<rootDir>/packages/kbn-test/src/jest/transforms/babel/index.js',
'^.+\\.(txt|html)?$': '<rootDir>/packages/kbn-test/src/jest/transforms/raw.js',
'^.+\\.peggy?$': '<rootDir>/packages/kbn-test/src/jest/transforms/peggy.js',
},
Expand Down
13 changes: 13 additions & 0 deletions packages/kbn-test/src/jest/transforms/babel/index.js
Original file line number Diff line number Diff line change
@@ -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);
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -21,11 +19,5 @@ module.exports = babelJest.default.createTransformer({
},
},
],
[
require.resolve('@emotion/babel-preset-css-prop'),
{
labelFormat: '[filename]--[local]',
},
],
],
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* 2.0.
*/

// eslint-disable-next-line import/no-extraneous-dependencies
const rootConfig = require('@kbn/test/jest-preset');

module.exports = {
coverageDirectory:
'<rootDir>/target/kibana-coverage/jest/x-pack/solutions/security/packages/ecs_data_quality_dashboard_impl',
Expand All @@ -23,4 +26,9 @@ module.exports = {
setupFilesAfterEnv: [
'<rootDir>/x-pack/solutions/security/packages/ecs_data_quality_dashboard/setup_tests.ts',
],
transform: {
...rootConfig.transform,
'^.+\\.(js|tsx?)$':
'<rootDir>/x-pack/solutions/security/packages/ecs_data_quality_dashboard/jest/babel-transformer.js',
},
};
Original file line number Diff line number Diff line change
@@ -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]',
},
],
],
});

0 comments on commit 4d0d6da

Please sign in to comment.