-
Notifications
You must be signed in to change notification settings - Fork 0
/
jest.ts
88 lines (76 loc) · 1.87 KB
/
jest.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import {
PluginType,
enqueueInstallDependency,
enqueueRemoveDependency,
getMonorepoDirectory,
hasPlugin,
removeFile,
warning,
writeFile,
writePackage,
type PluginArgs,
} from "@mokr/core";
import { join } from "path";
const JEST_CONFIG_FILENAME = "jest.config.js";
const TS_JEST_CONFIG = `
/** @type {import("ts-jest").JestConfigWithTsJest} */
export default {
preset: "ts-jest/presets/default-esm",
moduleNameMapper: {
"^(\\\\.{1,2}/.*)\\\\.js$": "$1",
},
};
`;
async function install({ directory }: PluginArgs) {
const monorepoDirectory = await getMonorepoDirectory({ directory });
if (!hasPlugin({ directory, name: "typescript" })) {
// @todo: install jest without ts-jest
} else {
// Install jest with ts-jest
enqueueInstallDependency({
directory,
identifier: ["jest", "ts-jest", "@types/jest"],
dev: true,
});
await writeFile({
path: join(directory, JEST_CONFIG_FILENAME),
contents: TS_JEST_CONFIG,
});
}
await writePackage({
directory,
data: {
scripts: {
test: "jest",
"test:watch": "jest --watch",
},
},
});
if (monorepoDirectory) {
// At monorepo level
await writePackage({
directory: monorepoDirectory,
data: {
scripts: {
test: "yarn workspaces foreach --all --topological --verbose run test",
"test:watch":
"yarn workspaces foreach --all --parallel --interlaced run test:watch",
},
},
});
}
}
async function remove({ directory }: PluginArgs) {
enqueueRemoveDependency({ directory, identifier: ["jest", "ts-jest"] });
try {
await removeFile({ path: join(directory, JEST_CONFIG_FILENAME) });
} catch {}
warning("Please review package.json manually");
}
async function load() {}
export const jest = {
type: PluginType.RepoOrWorkspace,
install,
remove,
load,
};