@html-validate/jest-config 3.5.2 → 3.6.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.
package/README.md CHANGED
@@ -40,3 +40,10 @@ declare module "*.html" {
40
40
  export default value;
41
41
  }
42
42
  ```
43
+
44
+ ### `tsconfig.json`
45
+
46
+ This preset searches the project folder for typescript configuration in the following order:
47
+
48
+ 1. `tsconfig.jest.json`
49
+ 2. `tsconfig.json`
package/jest-preset.js CHANGED
@@ -1,11 +1,13 @@
1
- const path = require("path");
2
1
  const { version: tsjestVersion } = require("ts-jest/package.json");
2
+ const { findTsConfig } = require("./lib");
3
3
 
4
+ const cwd = process.cwd();
4
5
  const tsjestMajor = parseInt(tsjestVersion.split(".")[0], 10);
5
6
  const useLegacyTsJestConfig = tsjestMajor < 29;
6
7
 
7
8
  const tsjestOptions = {
8
9
  stringifyContentPathRegex: "\\.html?$",
10
+ tsConfig: findTsConfig(cwd),
9
11
  };
10
12
 
11
13
  const preset = {
@@ -0,0 +1,51 @@
1
+ const fs = require("fs");
2
+ const path = require("path");
3
+
4
+ const search = ["tsconfig.jest.json", "tsconfig.json"];
5
+
6
+ /**
7
+ * @param {string} dir
8
+ * @returns {string | undefined}
9
+ */
10
+ function findMatch(dir) {
11
+ return search.find((it) => {
12
+ const filePath = path.join(dir, it);
13
+ return fs.existsSync(filePath);
14
+ });
15
+ }
16
+
17
+ /**
18
+ * Traverses directory structure looking for typescript configuration file.
19
+ *
20
+ * In order it searches for:
21
+ *
22
+ * - `tsconfig.jest.json`
23
+ * - `tsconfig.json`
24
+ *
25
+ * @param {string} cwd
26
+ * @returns {string | undefined} Absolute path to tsconfig or `undefined` if no match was found.
27
+ */
28
+ function findTsConfig(cwd) {
29
+ let current = cwd;
30
+
31
+ // eslint-disable-next-line no-constant-condition
32
+ while (true) {
33
+ const match = findMatch(current);
34
+ if (match) {
35
+ return path.join(current, match);
36
+ }
37
+
38
+ /* get the parent directory */
39
+ const child = current;
40
+ current = path.dirname(current);
41
+
42
+ /* stop if this is the root directory */
43
+ if (current === child) {
44
+ break;
45
+ }
46
+ }
47
+
48
+ return undefined;
49
+ }
50
+
51
+ module.exports = { findTsConfig };
@@ -0,0 +1,48 @@
1
+ const { findTsConfig } = require("./find-ts-config");
2
+ const fs = require("fs");
3
+ const path = require("path");
4
+
5
+ jest.mock("fs");
6
+
7
+ function mockFilesystem(files) {
8
+ jest.spyOn(fs, "existsSync").mockImplementation((filePath) => {
9
+ if (filePath.includes("node_modules") || path.dirname(filePath) === __dirname) {
10
+ return true;
11
+ }
12
+ return files.includes(filePath);
13
+ });
14
+ }
15
+
16
+ afterEach(() => {
17
+ jest.restoreAllMocks();
18
+ });
19
+
20
+ it("should find tsconfig.jest.json", () => {
21
+ expect.assertions(1);
22
+ mockFilesystem(["/path/to/project/tsconfig.jest.json"]);
23
+ expect(findTsConfig("/path/to/project")).toBe("/path/to/project/tsconfig.jest.json");
24
+ });
25
+
26
+ it("should find tsconfig.json", () => {
27
+ expect.assertions(1);
28
+ mockFilesystem(["/path/to/project/tsconfig.json"]);
29
+ expect(findTsConfig("/path/to/project")).toBe("/path/to/project/tsconfig.json");
30
+ });
31
+
32
+ it("should prefer tsconfig.jest.json over tsconfig.json", () => {
33
+ expect.assertions(1);
34
+ mockFilesystem(["/path/to/project/tsconfig.jest.json", "/path/to/project/tsconfig.json"]);
35
+ expect(findTsConfig("/path/to/project")).toBe("/path/to/project/tsconfig.jest.json");
36
+ });
37
+
38
+ it("should find from parent directory", () => {
39
+ expect.assertions(1);
40
+ mockFilesystem(["/path/to/project/tsconfig.jest.json"]);
41
+ expect(findTsConfig("/path/to/project/deep/path")).toBe("/path/to/project/tsconfig.jest.json");
42
+ });
43
+
44
+ it("should return undefined if no config file was found", () => {
45
+ expect.assertions(1);
46
+ mockFilesystem([]);
47
+ expect(findTsConfig("/path/to/project")).toBeUndefined();
48
+ });
package/lib/index.js ADDED
@@ -0,0 +1,3 @@
1
+ const { findTsConfig } = require("./find-ts-config");
2
+
3
+ module.exports = { findTsConfig };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@html-validate/jest-config",
3
- "version": "3.5.2",
3
+ "version": "3.6.1",
4
4
  "description": "Jest sharable preset used by the various HTML-validate packages",
5
5
  "keywords": [
6
6
  "jest"
@@ -18,6 +18,7 @@
18
18
  "main": "jest-preset.js",
19
19
  "files": [
20
20
  "dist",
21
+ "lib",
21
22
  "jest-preset.js"
22
23
  ],
23
24
  "dependencies": {