Skip to content

Commit

Permalink
test: Enable some easy jest eslint rules and manually fix them (#82849)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan953 authored Jan 3, 2025
1 parent 57a7e5a commit bb04f69
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 55 deletions.
22 changes: 12 additions & 10 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ const restrictedImportPatterns = [
group: ['sentry/components/devtoolbar/*'],
message: 'Do not depend on toolbar internals',
},
{
group: ['*.spec*'],
message:
'Do not import from test files. This causes tests to be executed multiple times.',
},
];

const restrictedImportPaths = [
Expand Down Expand Up @@ -752,23 +747,30 @@ export default typescript.config([
files: ['**/*.spec.{ts,js,tsx,jsx}', 'tests/js/**/*.{ts,js,tsx,jsx}'],
plugins: jest.configs['flat/recommended'].plugins,
rules: {
...jest.configs['flat/recommended'].rules,
...jest.configs['flat/style'].rules,

// `recommended` set this to warn, we've upgraded to error
'jest/no-disabled-tests': 'error',

// `recommended` set this to warn, we've downgraded to off
// Disabled as we have many tests which render as simple validations
'jest/expect-expect': 'off',

// Disabled as we have some comment out tests that cannot be
// uncommented due to typescript errors.
'jest/no-commented-out-tests': 'off',
'jest/no-commented-out-tests': 'off', // TODO(ryan953): Fix violations then delete this line

// Disabled as we do sometimes have conditional expects
'jest/no-conditional-expect': 'off',

// Useful for exporting some test utilities
'jest/no-export': 'off',
'jest/no-conditional-expect': 'off', // TODO(ryan953): Fix violations then delete this line

// We don't recommend snapshots, but if there are any keep it small
'jest/no-large-snapshots': ['error', {maxSize: 2000}],

'jest/no-alias-methods': 'off', // TODO(ryan953): Fix violations then delete this line
'jest/prefer-to-be': 'off', // TODO(ryan953): Fix violations then delete this line
'jest/prefer-to-contain': 'off', // TODO(ryan953): Fix violations then delete this line
'jest/prefer-to-have-length': 'off', // TODO(ryan953): Fix violations then delete this line
},
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
getBrowserContextData,
} from 'sentry/components/events/contexts/knownContext/browser';

export const MOCK_BROWSER_CONTEXT: BrowserContext = {
const MOCK_BROWSER_CONTEXT: BrowserContext = {
version: '83.0.4103',
type: 'browser',
name: '',
Expand All @@ -17,7 +17,7 @@ export const MOCK_BROWSER_CONTEXT: BrowserContext = {
unknown_key: 123,
};

export const MOCK_REDACTION = {
const MOCK_REDACTION = {
name: {
'': {
chunks: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {render, screen} from 'sentry-test/reactTestingLibrary';
import ContextCard from 'sentry/components/events/contexts/contextCard';
import {getUnityContextData} from 'sentry/components/events/contexts/platformContext/unity';

export const MOCK_UNITY_CONTEXT = {
const MOCK_UNITY_CONTEXT = {
type: 'unity' as const,
copy_texture_support: 'Basic, Copy3D, DifferentTypes, TextureToRT, RTToTexture',
editor_version: '2022.1.23f1',
Expand All @@ -17,7 +17,7 @@ export const MOCK_UNITY_CONTEXT = {
unknown_key: 123,
};

export const MOCK_REDACTION = {
const MOCK_REDACTION = {
install_mode: {
'': {
rem: [['organization:0', 'x']],
Expand Down
31 changes: 0 additions & 31 deletions static/app/utils/profiling/profile/profile.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,39 +1,8 @@
import type {CallTreeNode} from 'sentry/utils/profiling/callTreeNode';
import {Frame} from 'sentry/utils/profiling/frame';
import {Profile} from 'sentry/utils/profiling/profile/profile';

import {c, f, makeTestingBoilerplate} from './testUtils';

// Since it's easy to make mistakes or accidentally assign parents to the wrong nodes, this utility fn
// will format the stack samples as a tree string so it's more human friendly.
export const _logExpectedStack = (samples: Profile['samples']): string => {
const head = `
Samples follow a top-down chronological order\n\n`;

const tail = `\n
----------------------->
stack top -> stack bottom`;

const final: string[] = [];

const visit = (node: CallTreeNode, str: string[]) => {
str.push(`${node.frame.name}`);

if (node.parent) {
visit(node.parent, str);
}
};

for (const stackTop of samples) {
const str = [];
visit(stackTop, str);

final.push(str.join(' -> '));
}

return `${head}${final.join('\n')}${tail}`;
};

describe('Profile', () => {
it('Empty profile duration is not infinity', () => {
const profile = Profile.Empty;
Expand Down
4 changes: 2 additions & 2 deletions static/app/utils/tokenizeSearch.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ describe('utils/tokenizeSearch', function () {
];

for (const {name, string, object} of cases) {
it(name, () => expect(new MutableSearch(string)).toEqual(object));
it(`${name}`, () => expect(new MutableSearch(string)).toEqual(object));
}
});

Expand Down Expand Up @@ -504,7 +504,7 @@ describe('utils/tokenizeSearch', function () {
];

for (const {name, string, object} of cases) {
it(name, () => expect(object.formatString()).toEqual(string));
it(`${name}`, () => expect(object.formatString()).toEqual(string));
}
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {QueryClientProvider} from 'sentry/utils/queryClient';
import ReleaseContext from './releaseContext';
import {defaultRow, mockedCommit, mockedUser1, mockedUser2} from './testUtils';

export const mockedReleaseWithHealth = ReleaseFixture({
const mockedReleaseWithHealth = ReleaseFixture({
id: '1',
shortVersion: '[email protected]',
version: '[email protected]',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type StatusCounts = [
unknown: number,
];

export function generateEnvMapping(name: string, counts: StatusCounts) {
function generateEnvMapping(name: string, counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
[name]: {in_progress, ok, missed, timeout, error, unknown},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type StatusCounts = [
unknown: number,
];

export function generateEnvMapping(name: string, counts: StatusCounts) {
function generateEnvMapping(name: string, counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
[name]: {in_progress, ok, missed, timeout, error, unknown},
};
}

export function generateStats(counts: StatusCounts) {
function generateStats(counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
in_progress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ type StatusCounts = [
unknown: number,
];

export function generateEnvMapping(name: string, counts: StatusCounts) {
function generateEnvMapping(name: string, counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
[name]: {in_progress, ok, missed, timeout, error, unknown},
};
}

export function generateStats(counts: StatusCounts) {
function generateStats(counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
in_progress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type StatusCounts = [
unknown: number,
];

export function generateEnvMapping(name: string, counts: StatusCounts) {
function generateEnvMapping(name: string, counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
[name]: {in_progress, ok, missed, timeout, error, unknown},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type StatusCounts = [
unknown: number,
];

export function generateStats(counts: StatusCounts) {
function generateStats(counts: StatusCounts) {
const [in_progress, ok, missed, timeout, error, unknown] = counts;
return {
in_progress,
Expand Down
1 change: 1 addition & 0 deletions tests/js/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ConfigFixture} from 'sentry-fixture/config';

import {resetMockDate} from 'sentry-test/utils';

// eslint-disable-next-line jest/no-mocks-import
import type {Client} from 'sentry/__mocks__/api';
// eslint-disable-next-line no-restricted-imports
import {DEFAULT_LOCALE_DATA, setLocale} from 'sentry/locale';
Expand Down

0 comments on commit bb04f69

Please sign in to comment.