-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.mjs
More file actions
198 lines (185 loc) · 6.35 KB
/
Copy pathjest.mjs
File metadata and controls
198 lines (185 loc) · 6.35 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
* @file Settings for Jest based unit tests.
*/
import { createRequire } from 'node:module';
import { isMainThread } from 'node:worker_threads';
import jestPlugin from 'eslint-plugin-jest';
import globals from 'globals';
const jestVersion = await getJestVersion();
export default [
{
files: ['**/__tests__/**/*.[jt]s', '**/?(*.)+(spec|test).[jt]s'],
languageOptions: {
globals: {
...globals.jest
}
},
settings: {
jest: {
version: jestVersion
}
},
plugins: {
jest: jestPlugin
},
rules: {
/**
* eslint
*
* @see https://eslint.org/docs/rules/
*/
'max-lines': 'off',
'max-lines-per-function': 'off',
/**
* typescript-eslint
*
* @see https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules
*/
'@typescript-eslint/ban-ts-comment': ['error', {
'ts-expect-error': false
}],
/**
* eslint-plugin-import
*
* @see https://github.com/import-js/eslint-plugin-import
*/
'import/dynamic-import-chunkname': 'off',
'import/no-unassigned-import': 'error',
/**
* eslint-plugin-jest
*
* @see https://github.com/jest-community/eslint-plugin-jest/tree/master/docs/rules
*/
'jest/consistent-test-it': 'error',
'jest/expect-expect': 'error',
'jest/prefer-lowercase-title': ['error', { ignore: ['describe'] }],
'jest/max-expects': 'error',
'jest/max-nested-describe': 'error',
'jest/no-alias-methods': 'error',
'jest/no-commented-out-tests': 'error',
'jest/no-conditional-expect': 'error',
'jest/no-conditional-in-test': 'error',
'jest/no-confusing-set-timeout': 'error',
'jest/no-deprecated-functions': 'error',
'jest/no-disabled-tests': 'error',
'jest/no-done-callback': 'error',
'jest/no-duplicate-hooks': 'error',
'jest/no-error-equal': 'error',
'jest/no-export': 'error',
'jest/no-focused-tests': 'error',
'jest/no-hooks': ['error', { allow: ['beforeEach', 'afterEach', 'afterAll'] }],
'jest/no-identical-title': 'error',
'jest/no-interpolation-in-snapshots': 'error',
'jest/no-jasmine-globals': 'error',
'jest/no-large-snapshots': 'error',
'jest/no-mocks-import': 'error',
'jest/no-restricted-jest-methods': 'off', // Right now, there are no specific restrictions
'jest/no-restricted-matchers': ['error', {
resolves: 'Use `expect(await promise)` instead.',
toBeFalsy: 'Avoid `toBeFalsy`',
toBeTruthy: 'Avoid `toBeTruthy`',
toMatchSnapshot: 'Use `toMatchInlineSnapshot()` instead',
toThrowErrorMatchingSnapshot: 'Use `toThrowErrorMatchingInlineSnapshot()` instead'
}],
'jest/no-standalone-expect': 'error',
'jest/no-test-prefixes': 'error',
'jest/no-test-return-statement': 'error',
'jest/no-unneeded-async-expect-function': 'error',
'jest/no-unnecessary-assertion': 'error',
'jest/padding-around-after-all-blocks': 'off', // Covered by jest/padding-around-all
'jest/padding-around-after-each-blocks': 'off', // Covered by jest/padding-around-all
'jest/padding-around-all': 'error',
'jest/padding-around-before-all-blocks': 'off', // Covered by jest/padding-around-all
'jest/padding-around-before-each-blocks': 'off', // Covered by jest/padding-around-all
'jest/padding-around-describe-blocks': 'off', // Covered by jest/padding-around-all
'jest/padding-around-expect-groups': 'off', // Covered by jest/padding-around-all
'jest/padding-around-test-blocks': 'off', // Covered by jest/padding-around-all
'jest/prefer-called-with': 'error',
'jest/prefer-expect-assertions': ['error', { onlyFunctionsWithAsyncKeyword: true }],
'jest/prefer-expect-resolves': 'off', // We prefer `expect(await promise)` enforced by 'jest/no-restricted-matchers'
'jest/prefer-hooks-in-order': 'error',
'jest/prefer-hooks-on-top': 'error',
'jest/prefer-comparison-matcher': 'error',
'jest/prefer-each': 'error',
'jest/prefer-ending-with-an-expect': 'error',
'jest/prefer-equality-matcher': 'error',
'jest/prefer-importing-jest-globals': 'error',
'jest/prefer-jest-mocked': 'error',
'jest/prefer-mock-promise-shorthand': 'error',
'jest/prefer-mock-return-shorthand': 'error',
'jest/prefer-snapshot-hint': 'error',
'jest/prefer-spy-on': 'error',
'jest/prefer-strict-equal': 'error',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-been-called': 'error',
'jest/prefer-to-have-been-called-times': 'error',
'jest/prefer-to-have-length': 'error',
'jest/prefer-todo': 'error',
'jest/require-hook': 'error',
'jest/require-top-level-describe': 'error',
'jest/require-to-throw-message': 'error',
'jest/unbound-method': 'error',
'jest/valid-describe-callback': 'error',
'jest/valid-expect-in-promise': 'error',
'jest/valid-expect': 'error',
'jest/valid-expect-with-promise': 'error',
'jest/valid-mock-module-path': 'error',
'jest/valid-title': 'error'
}
},
{
files: ['**/__tests__/**/*.ts', '**/?(*.)+(spec|test).ts'],
languageOptions: {
globals: {
...globals.jest
}
},
settings: {
jest: {
version: jestVersion
}
},
plugins: {
jest: jestPlugin
},
rules: {
/**
* eslint-plugin-jest
*
* @see https://github.com/jest-community/eslint-plugin-jest/tree/master/docs/rules
*/
'jest/no-untyped-mock-factory': 'error'
}
}
];
/**
* Detects and retrieves the major version number of Jest installed in the project.
*
* This function attempts to dynamically import Jest using Node.js's module resolution
* system. It extracts just the major version number (e.g., "29" from "29.3.1") and
* logs the detected version to stdout. If Jest cannot be found or an error occurs,
* it returns the fallback value "detect".
*
* @returns {Promise<string>} A promise that resolves to the major version number of Jest
*/
async function getJestVersion () {
try {
const require = createRequire(import.meta.url);
const fileUrl = new URL('file:');
fileUrl.pathname = require.resolve('jest', { paths: [process.cwd()] });
const jestModule = await import(fileUrl.toString());
const jest = ('default' in jestModule ? jestModule.default : jestModule);
const [version] = jest.getVersion().split('.', 1);
if (isMainThread) {
process.stdout.write(`Detected Jest version: ${version}\n\n`);
}
return version;
}
catch {
if (isMainThread) {
process.stderr.write('No Jest version detected\n\n');
}
return 'detect';
}
}