@knapsack-pro/jest 9.0.0 → 10.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,9 +2,11 @@
2
2
  import pkg from '@knapsack-pro/jest/package.json' with { type: 'json' };
3
3
  import { KnapsackProCore, KnapsackProLogger, } from '@knapsack-pro/core';
4
4
  import { v4 as uuidv4 } from 'uuid';
5
+ import { relative } from 'path';
5
6
  import { EnvConfig } from './env-config.js';
6
- import { TestFilesFinder } from './test-files-finder.js';
7
+ import { PathsFinder } from './paths-finder.js';
7
8
  import { JestCLI } from './jest-cli.js';
9
+ import { normalizePaths } from './utils.js';
8
10
  const jestImport = await import('jest');
9
11
  const jest = jestImport.default ?? jestImport;
10
12
  const jestCLIOptions = JestCLI.argvToOptions();
@@ -12,34 +14,34 @@ const knapsackProLogger = new KnapsackProLogger();
12
14
  knapsackProLogger.debug(`Jest CLI options:\n${KnapsackProLogger.objectInspect(jestCLIOptions)}`);
13
15
  EnvConfig.loadEnvironmentVariables();
14
16
  const projectPath = process.cwd();
15
- const testFilesToExecute = () => TestFilesFinder.allTestFiles();
16
- const knapsackPro = new KnapsackProCore(pkg.name, pkg.version, testFilesToExecute);
17
- const onSuccess = async (queueTestFiles) => {
18
- const testFilePaths = queueTestFiles.map((testFile) => testFile.path);
17
+ const knapsackPro = new KnapsackProCore(pkg.name, pkg.version, PathsFinder.allPaths);
18
+ const onSuccess = async (scheduledPaths) => {
19
19
  const jestCLICoverage = EnvConfig.coverageDirectory
20
20
  ? { coverageDirectory: `${EnvConfig.coverageDirectory}/${uuidv4()}` }
21
21
  : {};
22
- const { results: { success: isTestSuiteGreen, testResults }, } = await jest.runCLI({
22
+ const { results } = await jest.runCLI({
23
23
  ...jestCLIOptions,
24
24
  ...jestCLICoverage,
25
25
  runTestsByPath: true,
26
- _: testFilePaths,
26
+ _: scheduledPaths,
27
27
  $0: 'jest',
28
28
  }, [projectPath]);
29
- const recordedTestFiles = testResults.map(({ testFilePath, perfStats: { start, end }, }) => {
29
+ const recordedPaths = {};
30
+ const failedPaths = new Set();
31
+ results.testResults.forEach(({ testFilePath, perfStats, testResults }) => {
30
32
  const path = process.platform === 'win32'
31
- ? testFilePath.replace(`${projectPath}\\`, '').replace(/\\/g, '/')
32
- : testFilePath.replace(`${projectPath}/`, '');
33
- const timeExecutionMiliseconds = end - start;
34
- const timeExecution = timeExecutionMiliseconds > 0 ? timeExecutionMiliseconds / 1000 : 0.0;
35
- return {
36
- path,
37
- time_execution: timeExecution,
38
- };
33
+ ? relative(projectPath, testFilePath).replace(/\\/g, '/')
34
+ : relative(projectPath, testFilePath);
35
+ const time = perfStats.end - perfStats.start;
36
+ recordedPaths[path] = time > 0 ? time / 1000 : 0.0;
37
+ if (testResults.some((assertionResult) => assertionResult.status === 'failed')) {
38
+ failedPaths.add(path);
39
+ }
39
40
  });
40
41
  return {
41
- recordedTestFiles,
42
- isTestSuiteGreen,
42
+ recordedPaths: normalizePaths(scheduledPaths, recordedPaths),
43
+ isTestSuiteGreen: results.success,
44
+ failedPaths,
43
45
  };
44
46
  };
45
47
  const onError = () => { };
@@ -3,26 +3,25 @@ import { minimatch } from 'minimatch';
3
3
  import { KnapsackProLogger } from '@knapsack-pro/core';
4
4
  import { EnvConfig } from './env-config.js';
5
5
  import * as Urls from './urls.js';
6
- export class TestFilesFinder {
7
- static allTestFiles() {
8
- const testFiles = glob
6
+ export class PathsFinder {
7
+ static allPaths() {
8
+ const paths = glob
9
9
  .sync(EnvConfig.testFilePattern)
10
- .filter((testFilePath) => {
10
+ .filter((path) => {
11
11
  if (EnvConfig.testFileExcludePattern) {
12
- return !minimatch(testFilePath, EnvConfig.testFileExcludePattern, {
12
+ return !minimatch(path, EnvConfig.testFileExcludePattern, {
13
13
  matchBase: true,
14
14
  });
15
15
  }
16
16
  return true;
17
17
  })
18
- .filter((testFilePath) => !testFilePath.match(/node_modules/))
19
- .map((testFilePath) => ({ path: testFilePath }));
20
- if (testFiles.length === 0) {
18
+ .filter((path) => !path.match(/node_modules/));
19
+ if (paths.length === 0) {
21
20
  const knapsackProLogger = new KnapsackProLogger();
22
21
  const errorMessage = `Test files cannot be found.\nPlease set KNAPSACK_PRO_TEST_FILE_PATTERN matching your test directory structure.\nLearn more: ${Urls.NO_TESTS_FOUND}`;
23
22
  knapsackProLogger.error(errorMessage);
24
23
  throw errorMessage;
25
24
  }
26
- return testFiles;
25
+ return paths;
27
26
  }
28
27
  }
package/lib/utils.js ADDED
@@ -0,0 +1,5 @@
1
+ export const normalizePaths = (scheduledPaths, recordedPaths) => {
2
+ return Object.entries(recordedPaths).reduce((acc, [path, time]) => {
3
+ return { ...acc, [path]: (acc[path] ?? 0) + time };
4
+ }, scheduledPaths.reduce((acc, path) => ({ ...acc, [path]: 0 }), {}));
5
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@knapsack-pro/jest",
3
- "version": "9.0.0",
3
+ "version": "10.0.0",
4
4
  "description": "Knapsack Pro Jest splits Jest tests across CI nodes and makes sure that tests will run in optimal time on each CI node.",
5
5
  "type": "module",
6
6
  "keywords": [
@@ -30,7 +30,9 @@
30
30
  ],
31
31
  "license": "MIT",
32
32
  "scripts": {
33
- "build": "rm -rf lib && tsc && chmod u+x lib/knapsack-pro-jest.js"
33
+ "build": "rm -rf lib && tsc && chmod u+x lib/knapsack-pro-jest.js",
34
+ "test": "vitest --run",
35
+ "test:watch": "vitest"
34
36
  },
35
37
  "files": [
36
38
  "/lib"
@@ -50,19 +52,20 @@
50
52
  "./package.json": "./package.json"
51
53
  },
52
54
  "dependencies": {
53
- "@knapsack-pro/core": "^9.0.0",
54
- "glob": "^11.0.3",
55
- "minimatch": "^10.1.1",
55
+ "@knapsack-pro/core": "^11.0.0",
56
+ "glob": "^13.0.6",
57
+ "minimatch": "^10.2.5",
56
58
  "minimist": "^1.2.8",
57
- "uuid": "^13.0.0"
59
+ "uuid": "^14.0.1"
58
60
  },
59
61
  "peerDependencies": {
60
62
  "jest": ">=23.0.0"
61
63
  },
62
64
  "devDependencies": {
63
65
  "@types/jest": "^30.0.0",
64
- "@types/node": "^24.9.2",
65
- "jest": "^30.2.0",
66
- "typescript": "^5.9.3"
66
+ "@types/node": "^26.1.1",
67
+ "jest": "^30.4.2",
68
+ "typescript": "^7.0.2",
69
+ "vitest": "^4.1.10"
67
70
  }
68
71
  }