Skip to content

Commit

Permalink
Reduce the scope of the integration test globs (#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor-baer authored Mar 5, 2024
1 parent 7751f69 commit 2cc9640
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .changeset/gentle-adults-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sumup/foundry': patch
---

Reduced the scope of the integration test file globs to prevent false positive lint issues in unit test files. Integration tests must be located in the `e2e/` or `tests/` folders in the repo or [workspace](https://docs.npmjs.com/cli/v7/using-npm/workspaces) root directories.
80 changes: 32 additions & 48 deletions src/configs/eslint/__snapshots__/config.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -1150,9 +1149,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -1967,9 +1965,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -2687,9 +2684,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -3426,9 +3422,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -4286,9 +4281,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -5101,9 +5095,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -5813,9 +5806,8 @@ exports[`eslint > with options > should return a config for { language: 'JavaScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -6812,9 +6804,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -8196,9 +8187,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -9537,9 +9527,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -10781,9 +10770,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -12044,9 +12032,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -13428,9 +13415,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down Expand Up @@ -14767,9 +14753,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:cypress/recommended",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
"plugins": [
"cypress",
Expand Down Expand Up @@ -16003,9 +15988,8 @@ exports[`eslint > with options > should return a config for { language: 'TypeScr
"plugin:playwright/playwright-test",
],
"files": [
"**/*spec.*",
"**/e2e/**/*",
"**/tests/**/*",
"e2e/**/*",
"tests/**/*",
],
},
],
Expand Down
29 changes: 28 additions & 1 deletion src/configs/eslint/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { Language, Environment, Framework, Plugin } from '../../types/shared';
import { getAllChoiceCombinations } from '../../lib/choices';
import { getOptions as getOptionsMock } from '../../lib/options';

import { customizeConfig, createConfig } from './config';
import {
customizeConfig,
createConfig,
getFileGlobsForWorkspaces,
} from './config';

vi.mock('process', () => ({
cwd: (): string => '/project/dir',
Expand Down Expand Up @@ -104,6 +108,29 @@ describe('eslint', () => {
});
});

describe('getFileGlobsForWorkspaces', () => {
const files = ['e2e/**/*', '*.spec.*'];

it('should return the file globs unchanged when the workspaces are null', () => {
const workspaces = null;
const actual = getFileGlobsForWorkspaces(workspaces, files);
expect(actual).toEqual(files);
});

it('should return the file globs raw and prefixed with each workspace', () => {
const workspaces = ['packages/*', 'docs'];
const actual = getFileGlobsForWorkspaces(workspaces, files);
expect(actual).toEqual([
'e2e/**/*',
'*.spec.*',
'packages/*/e2e/**/*',
'packages/*/*.spec.*',
'docs/e2e/**/*',
'docs/*.spec.*',
]);
});
});

describe('with options', () => {
const matrix = getAllChoiceCombinations({
language: Language,
Expand Down
46 changes: 37 additions & 9 deletions src/configs/eslint/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,17 @@
*/

import { cwd } from 'process';
import path from 'path';

import { flow, mergeWith, isArray, isObject, isEmpty, uniq } from 'lodash/fp';

import { Language, Environment, Framework, Plugin } from '../../types/shared';
import {
Language,
Environment,
Framework,
Plugin,
Workspaces,
} from '../../types/shared';
import * as logger from '../../lib/logger';
import { getOptions } from '../../lib/options';

Expand Down Expand Up @@ -45,6 +52,20 @@ function customizer(
return undefined;
}

export function getFileGlobsForWorkspaces(
workspaces: Workspaces,
fileGlobs: string[],
) {
if (!workspaces) {
return fileGlobs;
}
return fileGlobs.concat(
workspaces.flatMap((workspace) =>
fileGlobs.map((fileGlob) => path.join(workspace, fileGlob)),
),
);
}

const UNIT_TEST_FILES = [
'**/*.spec.*',
'**/jest*',
Expand All @@ -55,6 +76,8 @@ const UNIT_TEST_FILES = [
'**/__mocks__/**/*',
];

const INTEGRATION_TEST_FILES = ['e2e/**/*', 'tests/**/*'];

const NODE_FILES = ['api/**/*', 'pages/api/**/*', 'src/pages/api/**/*'];

const sharedRules = {
Expand Down Expand Up @@ -148,7 +171,7 @@ const base = {
],
};

function customizeLanguage(language?: Language) {
function customizeLanguage(language: Language) {
const languageMap = {
[Language.JAVASCRIPT]: {
overrides: sharedOverrides,
Expand Down Expand Up @@ -235,7 +258,7 @@ function customizeLanguage(language?: Language) {
};
}

function customizeEnvironments(environments?: Environment[]) {
function customizeEnvironments(environments: Environment[]) {
const environmentMap = {
[Environment.BROWSER]: {
extends: ['plugin:compat/recommended'],
Expand Down Expand Up @@ -297,7 +320,7 @@ function customizeEnvironments(environments?: Environment[]) {
};
}

function customizeFramework(frameworks?: Framework[]) {
function customizeFramework(frameworks: Framework[]) {
const frameworkMap = {
[Framework.REACT]: {
extends: [
Expand Down Expand Up @@ -395,7 +418,12 @@ function customizeFramework(frameworks?: Framework[]) {
};
}

function customizePlugin(plugins?: Plugin[]) {
function customizePlugin(plugins: Plugin[], workspaces: Workspaces) {
const integrationTestFiles = getFileGlobsForWorkspaces(
workspaces,
INTEGRATION_TEST_FILES,
);

const pluginMap = {
[Plugin.NEXT_JS]: {
extends: ['next'],
Expand Down Expand Up @@ -443,7 +471,7 @@ function customizePlugin(plugins?: Plugin[]) {
[Plugin.CYPRESS]: {
overrides: [
{
files: ['**/*spec.*', '**/e2e/**/*', '**/tests/**/*'],
files: integrationTestFiles,
extends: ['plugin:cypress/recommended'],
plugins: ['cypress'],
env: { 'cypress/globals': true },
Expand All @@ -453,7 +481,7 @@ function customizePlugin(plugins?: Plugin[]) {
[Plugin.PLAYWRIGHT]: {
overrides: [
{
files: ['**/*spec.*', '**/e2e/**/*', '**/tests/**/*'],
files: integrationTestFiles,
extends: ['plugin:playwright/playwright-test'],
},
],
Expand All @@ -474,7 +502,7 @@ function customizePlugin(plugins?: Plugin[]) {
};
}

function addCopyrightNotice(openSource?: boolean) {
function addCopyrightNotice(openSource: boolean) {
return (config: ESLintConfig): ESLintConfig => {
if (!openSource) {
return config;
Expand Down Expand Up @@ -524,7 +552,7 @@ export function createConfig(overrides: ESLintConfig = {}): ESLintConfig {
customizeLanguage(options.language),
customizeEnvironments(options.environments),
customizeFramework(options.frameworks),
customizePlugin(options.plugins),
customizePlugin(options.plugins, options.workspaces),
addCopyrightNotice(options.openSource),
applyOverrides(overrides),
)(base);
Expand Down
1 change: 1 addition & 0 deletions src/lib/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ export function getOptions(): Required<Options> {
frameworks: pick(config.frameworks, detectFrameworks),
plugins: pick(config.plugins, detectPlugins),
openSource: pick(config.openSource, detectOpenSource),
workspaces: packageJson.workspaces || null,
};
}

Expand Down
7 changes: 6 additions & 1 deletion src/types/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,15 @@ export enum Plugin {
TESTING_LIBRARY = 'Testing Library',
}

export type Workspaces = string[] | null;

export interface Options {
language?: Language;
environments?: Environment[];
frameworks?: Framework[];
plugins?: Plugin[];
openSource?: boolean;
workspaces?: Workspaces;
}

export interface InitOptions extends Options {
Expand All @@ -87,4 +90,6 @@ export interface ToolOptions {
scripts?: (options: InitOptions) => Script[];
}

export type PackageJson = NormalizedPackageJson;
export type PackageJson = NormalizedPackageJson & {
workspaces?: string[];
};

0 comments on commit 2cc9640

Please sign in to comment.