Skip to content

Commit 03d67c9

Browse files
authored
core(lint): Add custom no-focused-tests and no-skipped-tests rules (#13461)
Adds two simple custom rules to ignore `(it|test|describe).(skip|only)`. These rules now also flag vitest-based `skip` and `only` functions but led to duplications with the two rules from `eslint-plugin-jest`. So this patch also disables the jest versions in favour of the custom rules. To be clear, the custom rules are likely a bit less robust than the jest/vitest version but until we can use the actual vitest plugin, I think it's fine to stay with our custom version.
1 parent 780992d commit 03d67c9

File tree

6 files changed

+71
-20
lines changed

6 files changed

+71
-20
lines changed

packages/core/test/lib/transports/offline.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ describe('makeOfflineTransport', () => {
410410
START_DELAY + 2_000,
411411
);
412412

413-
// eslint-disable-next-line jest/no-disabled-tests
413+
// eslint-disable-next-line @sentry-internal/sdk/no-skipped-tests
414414
it.skip(
415415
'Follows the Retry-After header',
416416
async () => {

packages/eslint-config-sdk/src/base.js

+2-18
Original file line numberDiff line numberDiff line change
@@ -184,24 +184,8 @@ module.exports = {
184184
'@sentry-internal/sdk/no-optional-chaining': 'off',
185185
'@sentry-internal/sdk/no-nullish-coalescing': 'off',
186186
'@typescript-eslint/no-floating-promises': 'off',
187-
},
188-
},
189-
{
190-
// Configuration only for test files (this won't apply to utils or other files in test directories)
191-
plugins: ['jest'],
192-
env: {
193-
jest: true,
194-
},
195-
files: ['test.ts', '*.test.ts', '*.test.tsx', '*.test.js', '*.test.jsx'],
196-
rules: {
197-
// Prevent permanent usage of `it.only`, `fit`, `test.only` etc
198-
// We want to avoid debugging leftovers making their way into the codebase
199-
'jest/no-focused-tests': 'error',
200-
201-
// Prevent permanent usage of `it.skip`, `xit`, `test.skip` etc
202-
// We want to avoid debugging leftovers making their way into the codebase
203-
// If there's a good reason to skip a test (e.g. bad flakiness), just add an ignore comment
204-
'jest/no-disabled-tests': 'error',
187+
'@sentry-internal/sdk/no-focused-tests': 'error',
188+
'@sentry-internal/sdk/no-skipped-tests': 'error',
205189
},
206190
},
207191
{

packages/eslint-plugin-sdk/src/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ module.exports = {
1515
'no-eq-empty': require('./rules/no-eq-empty'),
1616
'no-class-field-initializers': require('./rules/no-class-field-initializers'),
1717
'no-regexp-constructor': require('./rules/no-regexp-constructor'),
18+
'no-focused-tests': require('./rules/no-focused-tests'),
19+
'no-skipped-tests': require('./rules/no-skipped-tests'),
1820
},
1921
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict';
2+
3+
/**
4+
* This rule was created to flag usages of the `.only` function in vitest and jest tests.
5+
* Usually, we don't want to commit focused tests as this causes other tests to be skipped.
6+
*/
7+
module.exports = {
8+
meta: {
9+
docs: {
10+
description: "Do not focus tests via `.only` to ensure we don't commit accidentally skip the other tests.",
11+
},
12+
schema: [],
13+
},
14+
create: function (context) {
15+
return {
16+
CallExpression(node) {
17+
if (
18+
node.callee.type === 'MemberExpression' &&
19+
node.callee.object.type === 'Identifier' &&
20+
['test', 'it', 'describe'].includes(node.callee.object.name) &&
21+
node.callee.property.type === 'Identifier' &&
22+
node.callee.property.name === 'only'
23+
) {
24+
context.report({
25+
node,
26+
message: "Do not focus tests via `.only` to ensure we don't commit accidentally skip the other tests.",
27+
});
28+
}
29+
},
30+
};
31+
},
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
/**
4+
* This rule was created to flag usages of the `.skip` function in vitest and jest tests.
5+
* Usually, we don't want to commit skipped tests as this causes other tests to be skipped.
6+
* Sometimes, skipping is valid (e.g. flaky tests), in which case, we can simply eslint-disable the rule.
7+
*/
8+
module.exports = {
9+
meta: {
10+
docs: {
11+
description: "Do not skip tests via `.skip` to ensure we don't commit accidentally skipped tests.",
12+
},
13+
schema: [],
14+
},
15+
create: function (context) {
16+
return {
17+
CallExpression(node) {
18+
if (
19+
node.callee.type === 'MemberExpression' &&
20+
node.callee.object.type === 'Identifier' &&
21+
['test', 'it', 'describe'].includes(node.callee.object.name) &&
22+
node.callee.property.type === 'Identifier' &&
23+
node.callee.property.name === 'skip'
24+
) {
25+
context.report({
26+
node,
27+
message: "Do not skip tests via `.skip` to ensure we don't commit accidentally skipped tests.",
28+
});
29+
}
30+
},
31+
};
32+
},
33+
};

packages/profiling-node/test/cpu_profiler.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ describe('Profiler bindings', () => {
316316
expect(profile?.measurements?.['memory_footprint']?.values.length).toBeLessThanOrEqual(300);
317317
});
318318

319-
// eslint-disable-next-line jest/no-disabled-tests
319+
// eslint-disable-next-line @sentry-internal/sdk/no-skipped-tests
320320
it.skip('includes deopt reason', async () => {
321321
// https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#52-the-object-being-iterated-is-not-a-simple-enumerable
322322
function iterateOverLargeHashTable() {

0 commit comments

Comments
 (0)