-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjest.js
105 lines (97 loc) · 3.27 KB
/
jest.js
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
/**
* Generates a config for `jest/no-restricted-matchers` that bans all variations
* of the given base matchers
*
* @param {Record<string, string>} matchers
*
* @return {Record<string, string>}
*/
const banMatchers = matchers => {
return Object.fromEntries(
Object.entries(matchers).flatMap(([matcher, message]) => [
[matcher, message],
[`resolves.${matcher}`, message],
[`resolves.not.${matcher}`, message],
[`rejects.not.${matcher}`, message],
[`not.${matcher}`, message]
])
);
};
/**
* Generates an ESLint config for Jest, based on the Ackama style guide
*
* @return {import('eslint').Linter.FlatConfig[]|import('eslint').Linter.LegacyConfig}
*/
const generateConfig = () => {
/** @type {import('eslint').Linter.RulesRecord} */
const rules = {
'@typescript-eslint/unbound-method': 'off',
'jest/consistent-test-it': 'error',
'jest/expect-expect': [
'error',
// todo: TBD - this will need adjusting for react-testing-library
{ assertFunctionNames: ['expect'] }
],
'jest/no-conditional-expect': 'error',
'jest/no-conditional-in-test': 'error',
'jest/no-deprecated-functions': 'error',
'jest/no-large-snapshots': 'error',
'jest/no-restricted-matchers': [
'error',
banMatchers({
toThrowErrorMatchingSnapshot:
'Use `toThrowErrorMatchingInlineSnapshot()` instead',
toMatchSnapshot: 'Use `toMatchInlineSnapshot()` instead',
toBeTruthy: 'Avoid `toBeTruthy`',
toBeFalsy: 'Avoid `toBeFalsy`'
})
],
'jest/no-test-return-statement': 'error',
'jest/prefer-called-with': 'error',
// you can disable this if you use a `beforeEach` setup script,
'jest/prefer-expect-assertions': 'error',
'jest/prefer-expect-resolves': 'error',
'jest/prefer-hooks-on-top': 'error',
'jest/prefer-lowercase-title': ['error', { ignoreTopLevelDescribe: true }],
'jest/prefer-spy-on': 'error',
'jest/prefer-strict-equal': 'error',
'jest/prefer-todo': 'error',
'jest/require-hook': 'error',
'jest/require-to-throw-message': 'error',
'jest/require-top-level-describe': 'error',
'jest/unbound-method': 'error',
'jest/valid-title': 'error',
'jest/padding-around-after-all-blocks': 'error',
'jest/padding-around-after-each-blocks': 'error',
'jest/padding-around-before-all-blocks': 'error',
'jest/padding-around-before-each-blocks': 'error',
'jest/padding-around-describe-blocks': 'error',
'jest/padding-around-test-blocks': 'error'
};
if (process.env.ESLINT_USE_FLAT_CONFIG !== 'false') {
// eslint-disable-next-line n/global-require
const pluginJest = require('eslint-plugin-jest');
/** @type {import('eslint').Linter.FlatConfig[]} */
const config = [
{
name: 'ackama/jest',
...pluginJest.configs['flat/recommended'],
plugins: { jest: pluginJest },
rules: {
...pluginJest.configs['flat/recommended'].rules,
...pluginJest.configs['flat/style'].rules,
...rules
}
}
];
return config;
}
/** @type {import('eslint').Linter.LegacyConfig} */
const config = {
plugins: ['jest'],
extends: ['plugin:jest/recommended', 'plugin:jest/style'],
rules
};
return config;
};
module.exports = generateConfig();