@openendurance/config-jest 0.0.0-stage → 0.1.1

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.
Files changed (3) hide show
  1. package/README.md +41 -2
  2. package/package.json +40 -5
  3. package/src/index.js +92 -0
package/README.md CHANGED
@@ -1,3 +1,42 @@
1
- # Temporary Holding Version
1
+ # @openendurance/config-jest
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Shared Jest configuration for Open Endurance Initiative projects.
4
+
5
+ This package provides a small default config and a helper that maps TypeScript path aliases into Jest's `moduleNameMapper`, which is the part that usually trips projects up.
6
+
7
+ ## Usage
8
+
9
+ Install the matching test dependencies in the consuming project first:
10
+
11
+ ```sh
12
+ pnpm add -D jest@^30.4.2 ts-jest@^29.4.11 typescript@^6.0.3
13
+ ```
14
+
15
+ ```js
16
+ const { createJestConfig } = require("@openendurance/config-jest");
17
+
18
+ module.exports = createJestConfig({
19
+ tsconfigPath: "<rootDir>/tsconfig.json",
20
+ });
21
+ ```
22
+
23
+ ## What It Does
24
+
25
+ - Sets up `ts-jest` for TypeScript test files
26
+ - Infers Jest aliases from the `compilerOptions.paths` section of the chosen tsconfig file
27
+ - Keeps the default environment simple so consumers can override to `jsdom` or something more specific when needed
28
+
29
+ ## Options
30
+
31
+ | Option | Purpose |
32
+ | ------------------ | ----------------------------------------------------------------------- |
33
+ | `tsconfigPath` | Path to the tsconfig file whose path aliases should be mapped into Jest |
34
+ | `testEnvironment` | Jest environment to use; defaults to `node` |
35
+ | `moduleNameMapper` | Additional or overriding Jest module name mappings |
36
+ | `transform` | Optional custom transform overrides |
37
+
38
+ ## Conventions
39
+
40
+ - Keep the default config minimal and predictable
41
+ - Use the helper when you need TypeScript aliases in Jest
42
+ - Override locally for framework-specific needs like `jsdom`, React Native, or Electron renderer tests
package/package.json CHANGED
@@ -1,6 +1,41 @@
1
1
  {
2
- "name": "@openendurance/config-jest",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "$schema": "https://json.schemastore.org/package",
3
+ "name": "@openendurance/config-jest",
4
+ "version": "0.1.1",
5
+ "license": "Apache-2.0",
6
+ "author": "Kristopher Cargile <k@algorythmic.com>",
7
+ "description": "Shared Jest configuration for Open Endurance Initiative projects.",
8
+ "keywords": [
9
+ "endurance-sports",
10
+ "fitness",
11
+ "healthtech",
12
+ "oei",
13
+ "open-endurance-initiative",
14
+ "open-source",
15
+ "openenduranceinitiative"
16
+ ],
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/openendurance/config-dev.git"
20
+ },
21
+ "publishConfig": {
22
+ "access": "public",
23
+ "provenance": true
24
+ },
25
+ "peerDependencies": {
26
+ "jest": "^30.4.2",
27
+ "ts-jest": "^29.4.11",
28
+ "typescript": "^6.0.3"
29
+ },
30
+ "files": [
31
+ "README.md",
32
+ "src"
33
+ ],
34
+ "main": "./src/index.js",
35
+ "exports": "./src/index.js",
36
+ "scripts": {
37
+ "build": "pnpm run lint",
38
+ "lint": "pnpm exec biome lint README.md src",
39
+ "prepack": "pnpm run build"
40
+ }
41
+ }
package/src/index.js ADDED
@@ -0,0 +1,92 @@
1
+ const fs = require("node:fs");
2
+ const path = require("node:path");
3
+
4
+ function escapeRegex(value) {
5
+ return value.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
6
+ }
7
+
8
+ function globToModulePattern(pattern) {
9
+ const wildcardIndex = pattern.indexOf("*");
10
+ if (wildcardIndex === -1) {
11
+ return `^${escapeRegex(pattern)}$`;
12
+ }
13
+
14
+ const prefix = escapeRegex(pattern.slice(0, wildcardIndex));
15
+ const suffix = escapeRegex(pattern.slice(wildcardIndex + 1));
16
+ return `^${prefix}(.*)${suffix}$`;
17
+ }
18
+
19
+ function resolveTsconfig(tsconfigPath) {
20
+ if (!tsconfigPath) {
21
+ return {};
22
+ }
23
+
24
+ const absolutePath = path.isAbsolute(tsconfigPath)
25
+ ? tsconfigPath
26
+ : path.resolve(process.cwd(), tsconfigPath);
27
+
28
+ if (!fs.existsSync(absolutePath)) {
29
+ return {};
30
+ }
31
+
32
+ const raw = fs.readFileSync(absolutePath, "utf8");
33
+ try {
34
+ return JSON.parse(raw);
35
+ } catch {
36
+ return {};
37
+ }
38
+ }
39
+
40
+ function createModuleNameMapper(paths, rootPrefix = "<rootDir>/") {
41
+ if (!paths || typeof paths !== "object") {
42
+ return {};
43
+ }
44
+
45
+ return Object.entries(paths).reduce((mapper, [key, targets]) => {
46
+ if (!Array.isArray(targets) || targets.length === 0) {
47
+ return mapper;
48
+ }
49
+
50
+ const target = targets[0];
51
+ if (typeof target !== "string") {
52
+ return mapper;
53
+ }
54
+
55
+ const pattern = globToModulePattern(key);
56
+ const replacement = `${rootPrefix}${target.replace(/\*/g, "$1")}`;
57
+ mapper[pattern] = replacement;
58
+ return mapper;
59
+ }, {});
60
+ }
61
+
62
+ function createJestConfig(options = {}) {
63
+ const {
64
+ tsconfigPath = "tsconfig.json",
65
+ testEnvironment = "node",
66
+ moduleNameMapper = {},
67
+ transform = {},
68
+ ...rest
69
+ } = options;
70
+
71
+ const tsconfig = resolveTsconfig(tsconfigPath);
72
+ const compilerOptions = tsconfig.compilerOptions || {};
73
+ const mappedAliases = createModuleNameMapper(compilerOptions.paths);
74
+
75
+ return {
76
+ testEnvironment,
77
+ moduleFileExtensions: ["ts", "tsx", "js", "jsx", "json", "mjs", "cjs"],
78
+ transform: {
79
+ "^.+\\.(ts|tsx)$": ["ts-jest", { tsconfig: tsconfigPath }],
80
+ ...transform,
81
+ },
82
+ moduleNameMapper: {
83
+ ...mappedAliases,
84
+ ...moduleNameMapper,
85
+ },
86
+ ...rest,
87
+ };
88
+ }
89
+
90
+ module.exports = createJestConfig;
91
+ module.exports.createJestConfig = createJestConfig;
92
+ module.exports.createModuleNameMapper = createModuleNameMapper;