From 24b40140c49d820e12b73457a22ce1c33314d262 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Mon, 10 Oct 2022 16:08:41 +0100 Subject: [PATCH 1/3] build: add __tests__ and __mocks__ dirs to relevant parts of eslint-factory Signed-off-by: MT Lewis --- .changeset/great-crews-own.md | 14 ++++++++++++++ packages/cli/config/eslint-factory.js | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 .changeset/great-crews-own.md diff --git a/.changeset/great-crews-own.md b/.changeset/great-crews-own.md new file mode 100644 index 0000000000..96633bc857 --- /dev/null +++ b/.changeset/great-crews-own.md @@ -0,0 +1,14 @@ +--- +'@backstage/cli': minor +--- + +Treat files in `__tests__` and `__mocks__` directories as test files for linting +purposes. + +Updates the parts of the eslint configuration generator that specify which files +should be treated as test code to include any files under directories named +`__tests__` or `__mocks__`, to match the default file locations used in Jest. + +cf. +https://jestjs.io/docs/configuration/#testmatch-arraystring +https://jestjs.io/docs/manual-mocks#mocking-user-modules diff --git a/packages/cli/config/eslint-factory.js b/packages/cli/config/eslint-factory.js index 6e2a510c4a..d764edb2b5 100644 --- a/packages/cli/config/eslint-factory.js +++ b/packages/cli/config/eslint-factory.js @@ -96,12 +96,16 @@ function createConfig(dir, extraConfig = {}) { `!${joinPath(dir, 'src/**')}`, joinPath(dir, 'src/**/*.test.*'), joinPath(dir, 'src/**/*.stories.*'), + joinPath(dir, 'src/**/__tests__/**'), + joinPath(dir, 'src/**/__mocks__/**'), joinPath(dir, 'src/setupTests.*'), ] : [ // Legacy config for packages that don't provide a dir '**/*.test.*', '**/*.stories.*', + '**/__tests__/**', + '**/__mocks__/**', '**/src/setupTests.*', '**/dev/**', ], @@ -137,6 +141,8 @@ function createConfig(dir, extraConfig = {}) { // Prevent imports of stories or tests '*.stories*', '*.test*', + '**/__tests__/**', + '**/__mocks__/**', ], }, ], @@ -159,7 +165,14 @@ function createConfig(dir, extraConfig = {}) { }, }, { - files: ['**/*.test.*', '**/*.stories.*', 'src/setupTests.*', '!src/**'], + files: [ + '**/*.test.*', + '**/*.stories.*', + '**/__tests__/**', + '**/__mocks__/**', + 'src/setupTests.*', + '!src/**', + ], rules: { ...testRules, 'no-restricted-syntax': [ From 18469522768181119ca91293ba09a2c4da046151 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Mon, 17 Oct 2022 09:37:51 +0100 Subject: [PATCH 2/3] build: switch eslint-factory to use __testUtils__ instead of __tests__ Signed-off-by: MT Lewis --- .changeset/great-crews-own.md | 14 ++++++++------ packages/cli/config/eslint-factory.js | 8 ++++---- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/.changeset/great-crews-own.md b/.changeset/great-crews-own.md index 96633bc857..676eb7cf3d 100644 --- a/.changeset/great-crews-own.md +++ b/.changeset/great-crews-own.md @@ -2,13 +2,15 @@ '@backstage/cli': minor --- -Treat files in `__tests__` and `__mocks__` directories as test files for linting +Treat files in `__testUtils__` and `__mocks__` directories as test files for linting purposes. Updates the parts of the eslint configuration generator that specify which files -should be treated as test code to include any files under directories named -`__tests__` or `__mocks__`, to match the default file locations used in Jest. +should be treated as test code to include any files under two additional +directories: -cf. -https://jestjs.io/docs/configuration/#testmatch-arraystring -https://jestjs.io/docs/manual-mocks#mocking-user-modules +- `__mocks__`: this is the directory used by Jest[0] for mock code. +- `__testUtils__`: a suggested location for utility code executed only when + running tests. + +[0]: https://jestjs.io/docs/manual-mocks#mocking-user-modules diff --git a/packages/cli/config/eslint-factory.js b/packages/cli/config/eslint-factory.js index d764edb2b5..e213f58e4e 100644 --- a/packages/cli/config/eslint-factory.js +++ b/packages/cli/config/eslint-factory.js @@ -96,7 +96,7 @@ function createConfig(dir, extraConfig = {}) { `!${joinPath(dir, 'src/**')}`, joinPath(dir, 'src/**/*.test.*'), joinPath(dir, 'src/**/*.stories.*'), - joinPath(dir, 'src/**/__tests__/**'), + joinPath(dir, 'src/**/__testUtils__/**'), joinPath(dir, 'src/**/__mocks__/**'), joinPath(dir, 'src/setupTests.*'), ] @@ -104,7 +104,7 @@ function createConfig(dir, extraConfig = {}) { // Legacy config for packages that don't provide a dir '**/*.test.*', '**/*.stories.*', - '**/__tests__/**', + '**/__testUtils__/**', '**/__mocks__/**', '**/src/setupTests.*', '**/dev/**', @@ -141,7 +141,7 @@ function createConfig(dir, extraConfig = {}) { // Prevent imports of stories or tests '*.stories*', '*.test*', - '**/__tests__/**', + '**/__testUtils__/**', '**/__mocks__/**', ], }, @@ -168,7 +168,7 @@ function createConfig(dir, extraConfig = {}) { files: [ '**/*.test.*', '**/*.stories.*', - '**/__tests__/**', + '**/__testUtils__/**', '**/__mocks__/**', 'src/setupTests.*', '!src/**', From 0dd31e8cf2784867f708b059d7da93e1bdd119e8 Mon Sep 17 00:00:00 2001 From: MT Lewis Date: Mon, 17 Oct 2022 09:38:31 +0100 Subject: [PATCH 3/3] chore: switch eslint-factory changeset to 'patch' Signed-off-by: MT Lewis --- .changeset/great-crews-own.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/great-crews-own.md b/.changeset/great-crews-own.md index 676eb7cf3d..dfe4d62da9 100644 --- a/.changeset/great-crews-own.md +++ b/.changeset/great-crews-own.md @@ -1,5 +1,5 @@ --- -'@backstage/cli': minor +'@backstage/cli': patch --- Treat files in `__testUtils__` and `__mocks__` directories as test files for linting