Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jest] refactor config check #135960

Merged
merged 3 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@
"@istanbuljs/schema": "^0.1.2",
"@jest/console": "^26.6.2",
"@jest/reporters": "^26.6.2",
"@jest/types": "^26",
"@kbn/ambient-storybook-types": "link:bazel-bin/packages/kbn-ambient-storybook-types",
"@kbn/ambient-ui-types": "link:bazel-bin/packages/kbn-ambient-ui-types",
"@kbn/axe-config": "link:bazel-bin/packages/kbn-axe-config",
Expand Down Expand Up @@ -1026,10 +1027,12 @@
"jest-canvas-mock": "^2.3.1",
"jest-circus": "^26.6.3",
"jest-cli": "^26.6.3",
"jest-config": "^26",
"jest-diff": "^26.6.2",
"jest-environment-jsdom": "^26.6.2",
"jest-environment-jsdom-thirteen": "^1.0.1",
"jest-raw-loader": "^1.0.1",
"jest-runtime": "^26",
"jest-silent-reporter": "^0.5.0",
"jest-snapshot": "^26.6.2",
"jest-specific-snapshot": "2.0.0",
Expand Down

This file was deleted.

49 changes: 0 additions & 49 deletions packages/kbn-test/src/jest/configs/find_missing_config_files.ts

This file was deleted.

74 changes: 74 additions & 0 deletions packages/kbn-test/src/jest/configs/get_all_jest_paths.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.
*/

import Fs from 'fs';
import Path from 'path';

import execa from 'execa';
import minimatch from 'minimatch';
import { REPO_ROOT } from '@kbn/utils';

// @ts-expect-error jest-preset is necessarily a JS file
import { testMatch } from '../../../jest-preset';

const UNIT_CONFIG_NAME = 'jest.config.js';
const INTEGRATION_CONFIG_NAME = 'jest.integration.config.js';

export async function getAllJestPaths() {
const proc = await execa('git', ['ls-files', '-comt', '--exclude-standard'], {
cwd: REPO_ROOT,
stdio: ['ignore', 'pipe', 'pipe'],
buffer: true,
});

const testsRe = (testMatch as string[]).map((p) => minimatch.makeRe(p));
const classify = (rel: string) => {
if (testsRe.some((re) => re.test(rel))) {
return 'test' as const;
}

const basename = Path.basename(rel);
return basename === UNIT_CONFIG_NAME || basename === INTEGRATION_CONFIG_NAME
? ('config' as const)
: undefined;
};

const tests = new Set<string>();
const configs = new Set<string>();

for (const line of proc.stdout.split('\n').map((l) => l.trim())) {
if (!line) {
continue;
}

const rel = line.slice(2); // trim the single char status from the line
const type = classify(rel);

if (!type) {
continue;
}

const set = type === 'test' ? tests : configs;
const abs = Path.resolve(REPO_ROOT, rel);

if (line.startsWith('C ')) {
// this line indicates that the previous path is changed in the working tree, so we need to determine if
// it was deleted, and if so, remove it from the set we added it to
if (!Fs.existsSync(abs)) {
set.delete(abs);
}
} else {
set.add(abs);
}
}

return {
tests,
configs,
};
}
32 changes: 0 additions & 32 deletions packages/kbn-test/src/jest/configs/get_all_test_files.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.
*/

import { readConfig } from 'jest-config';
import { createContext } from 'jest-runtime';
import { SearchSource } from 'jest';
import { asyncMapWithLimit } from '@kbn/std';

const EMPTY_ARGV = {
$0: '',
_: [],
};

const NO_WARNINGS_CONSOLE = {
...console,
warn() {
// ignore haste-map warnings
},
};

export interface TestsForConfigPath {
path: string;
testPaths: Set<string>;
}

export async function getTestsForConfigPaths(
configPaths: Iterable<string>
): Promise<TestsForConfigPath[]> {
return await asyncMapWithLimit(configPaths, 60, async (path) => {
const config = await readConfig(EMPTY_ARGV, path);
const searchSource = new SearchSource(
await createContext(config.projectConfig, {
maxWorkers: 1,
watchman: false,
watch: false,
console: NO_WARNINGS_CONSOLE,
})
);

const results = await searchSource.getTestPaths(config.globalConfig, undefined, undefined);

return {
path,
testPaths: new Set(results.tests.map((t) => t.path)),
};
});
}
Loading