[Lint] Officially deprecate Enzyme (#221779)

## Summary

Please see for
[context](https://docs.google.com/document/d/1J9mGfmGukgFS-6jqD1zopH8wIRvsqNhDhQbjQZ9hjEY/edit?tab=t.hzzf0a72f9gd)

This PR tries to discourage usage of enzyme: 

- Mark shared test utils that use enzyme as `@deprecated` 
- Add eslint warning for imports of `enzyme` 
- Add ci stat counter of imports of `enzyme`
This commit is contained in:
Anton Dosov 2025-06-03 12:15:37 +02:00 committed by GitHub
parent b4b49acb68
commit 0dbbf482a5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 175 additions and 11 deletions

View file

@ -34,6 +34,7 @@ import { writeDeprecationDueByTeam } from './mdx/write_deprecations_due_by_team'
import { trimDeletedDocsFromNav } from './trim_deleted_docs_from_nav';
import { getAllDocFileIds } from './mdx/get_all_doc_file_ids';
import { getPathsByPackage } from './get_paths_by_package';
import { countEnzymeImports, EnzymeImportCounts } from './count_enzyme_imports';
function isStringArray(arr: unknown | string[]): arr is string[] {
return Array.isArray(arr) && arr.every((p) => typeof p === 'string');
@ -144,7 +145,9 @@ export function runBuildApiDocsCli() {
const reporter = CiStatsReporter.fromEnv(log);
const allPluginStats: { [key: string]: PluginMetaInfo & ApiStats & EslintDisableCounts } = {};
const allPluginStats: {
[key: string]: PluginMetaInfo & ApiStats & EslintDisableCounts & EnzymeImportCounts;
} = {};
for (const plugin of plugins) {
const id = plugin.id;
@ -162,6 +165,7 @@ export function runBuildApiDocsCli() {
allPluginStats[id] = {
...(await countEslintDisableLines(paths)),
...(await countEnzymeImports(paths)),
...collectApiStatsForPlugin(
pluginApi,
missingApiItems,
@ -283,6 +287,12 @@ export function runBuildApiDocsCli() {
group: 'Total ESLint disabled count',
value: pluginStats.eslintDisableFileCount + pluginStats.eslintDisableLineCount,
},
{
id,
meta: { pluginTeam },
group: 'Enzyme imports',
value: pluginStats.enzymeImportCount,
},
]);
const getLink = (d: ApiDeclaration) =>

View file

@ -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", 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".
*/
import Path from 'path';
import { getRepoFiles } from '@kbn/get-repo-files';
import { countEnzymeImports } from './count_enzyme_imports';
describe('count', () => {
test('number of "enzyme" imports in this file', async () => {
const { enzymeImportCount } = await countEnzymeImports([Path.resolve(__dirname, __filename)]);
expect(enzymeImportCount).toBe(0);
});
test('number of "enzyme" imports in this directory', async () => {
const allFiles = await getRepoFiles([__dirname]);
const { enzymeImportCount } = await countEnzymeImports(Array.from(allFiles, (f) => f.abs));
expect(enzymeImportCount).toBe(1);
});
});

View file

@ -0,0 +1,30 @@
/*
* 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".
*/
import { asyncForEachWithLimit } from '@kbn/std';
import Fs from 'fs';
export interface EnzymeImportCounts {
enzymeImportCount: number;
}
const count = (s: string, r: RegExp) => Array.from(s.matchAll(r)).length;
export async function countEnzymeImports(paths: string[]): Promise<EnzymeImportCounts> {
let enzymeImportCount = 0;
await asyncForEachWithLimit(paths, 100, async (path) => {
const content = await Fs.promises.readFile(path, 'utf8');
enzymeImportCount +=
count(content, /import\s+[^;]*?from\s+['"]enzyme['"]/g) +
count(content, /require\(['"]enzyme['"]\)/g);
});
return { enzymeImportCount };
}

View file

@ -0,0 +1,10 @@
/*
* 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".
*/
export { countEnzymeImports, type EnzymeImportCounts } from './count_enzyme_imports';

View file

@ -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", 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".
*/
import React from 'react';
import { shallow } from 'enzyme';
describe('test_enzyme_import', () => {
it('renders a div', () => {
const wrapper = shallow(React.createElement('div', null, 'Hello World'));
expect(wrapper.is('div')).toBe(true);
});
});

View file

@ -21,5 +21,6 @@ module.exports = {
no_unsafe_console: require('./rules/no_unsafe_console'),
no_unsafe_hash: require('./rules/no_unsafe_hash'),
require_kibana_feature_privileges_naming: require('./rules/require_kibana_feature_privileges_naming'),
no_deprecated_imports: require('./rules/no_deprecated_imports'),
},
};

View file

@ -0,0 +1,19 @@
/*
* 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 Linter = require('eslint').Linter;
const coreRule = new Linter().getRules().get('no-restricted-imports');
/**
* This rule is used to prevent the use of deprecated imports in Kibana code.
* It is a wrapper around the core ESLint rule `no-restricted-imports` with
* a different id to avoid conflicts with the core rule.
*/
module.exports = coreRule;