]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
test: add unit test for eslint configs
authorcexbrayat <cedric@ninja-squad.com>
Wed, 14 Aug 2024 13:53:51 +0000 (15:53 +0200)
committerCédric Exbrayat <cexbrayat@users.noreply.github.com>
Wed, 14 Aug 2024 14:44:57 +0000 (16:44 +0200)
The commit adds a unit test for the eslint configurations and properly formats our tests.

.prettierignore
__test__/locale.spec.ts
__test__/renderEslint.spec.ts [new file with mode: 0644]
scripts/snapshot.mjs
utils/renderEslint.ts

index 688151cd9e1966b6eb5460ad1f1b22a3f1c5dda6..be18eeb0af2e48af0265554a24884f6ab951eafd 100644 (file)
@@ -4,6 +4,8 @@ pnpm-lock.yaml
 # https://github.com/prettier/prettier/issues/7884
 **/*.spec.js
 **/*.spec.ts
+# but let's format our unit tests
+!__test__/**/*.spec.ts
 **/dist
 # https://github.com/prettier/prettier/issues/5246
 **/*.html
index 1ea0a1127ba862747ca59a469519d549b33fa215..6e36d3b9b48daf3d267b07905f53916c0e91e5e4 100644 (file)
@@ -6,23 +6,23 @@ import en from '../locales/en-US.json'
 function getKeys(obj: any, path = '', result: string[] = []) {
   for (let key in obj) {
     if (typeof obj[key] === 'object') {
-      getKeys(obj[key], path ? `${path}.${key}` : key, result);
+      getKeys(obj[key], path ? `${path}.${key}` : key, result)
     } else {
-      result.push(path ? `${path}.${key}` : key);
+      result.push(path ? `${path}.${key}` : key)
     }
   }
-  return result;
+  return result
 }
 
 const localesOtherThanEnglish = readdirSync(resolve(__dirname, '../locales')).filter((file) => {
   return file.endsWith('.json') && !file.startsWith('en-US')
 })
-const defaultKeys = getKeys(en);
+const defaultKeys = getKeys(en)
 
-describe("locale files should include all keys", () => {
+describe('locale files should include all keys', () => {
   localesOtherThanEnglish.forEach((locale) => {
     it(`for ${locale}`, () => {
       expect(getKeys(require(`../locales/${locale}`))).toEqual(defaultKeys)
     })
   })
-})
\ No newline at end of file
+})
diff --git a/__test__/renderEslint.spec.ts b/__test__/renderEslint.spec.ts
new file mode 100644 (file)
index 0000000..5c54e4e
--- /dev/null
@@ -0,0 +1,56 @@
+import { it, describe, expect } from 'vitest'
+import { getAdditionalConfigAndDependencies } from '../utils/renderEslint'
+
+describe('renderEslint', () => {
+  it('should get additional dependencies and config with no test flags', () => {
+    const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
+      needsCypress: false,
+      needsCypressCT: false,
+      needsPlaywright: false
+    })
+    expect(additionalConfig).toStrictEqual({})
+    expect(additionalDependencies).toStrictEqual({})
+  })
+
+  it('should get additional dependencies and config with for cypress', () => {
+    const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
+      needsCypress: true,
+      needsCypressCT: false,
+      needsPlaywright: false
+    })
+    expect(additionalConfig.overrides[0].files).toStrictEqual([
+      'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}',
+      'cypress/support/**/*.{js,ts,jsx,tsx}'
+    ])
+    expect(additionalConfig.overrides[0].extends).toStrictEqual(['plugin:cypress/recommended'])
+    expect(additionalDependencies['eslint-plugin-cypress']).not.toBeUndefined()
+  })
+
+  it('should get additional dependencies and config with for cypress with component testing', () => {
+    const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
+      needsCypress: true,
+      needsCypressCT: true,
+      needsPlaywright: false
+    })
+    expect(additionalConfig.overrides[0].files).toStrictEqual([
+      '**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}',
+      'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}',
+      'cypress/support/**/*.{js,ts,jsx,tsx}'
+    ])
+    expect(additionalConfig.overrides[0].extends).toStrictEqual(['plugin:cypress/recommended'])
+    expect(additionalDependencies['eslint-plugin-cypress']).not.toBeUndefined()
+  })
+
+  it('should get additional dependencies and config with for playwright', () => {
+    const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
+      needsCypress: false,
+      needsCypressCT: false,
+      needsPlaywright: true
+    })
+    expect(additionalConfig.overrides[0].files).toStrictEqual([
+      'e2e/**/*.{test,spec}.{js,ts,jsx,tsx}'
+    ])
+    expect(additionalConfig.overrides[0].extends).toStrictEqual(['plugin:playwright/recommended'])
+    expect(additionalDependencies['eslint-plugin-playwright']).not.toBeUndefined()
+  })
+})
index ea4c4ad188356bb9a423828914c1add573cf009c..b80da8e1e6d9d1423a53dc79dfe0e561ee63cb93 100644 (file)
@@ -54,7 +54,12 @@ function fullCombination(arr) {
 }
 
 let flagCombinations = fullCombination(featureFlags)
-flagCombinations.push(['default'], ['devtools', 'router', 'pinia'], ['eslint'], ['eslint-with-prettier'])
+flagCombinations.push(
+  ['default'],
+  ['devtools', 'router', 'pinia'],
+  ['eslint'],
+  ['eslint-with-prettier']
+)
 
 // `--with-tests` are equivalent of `--vitest --cypress`
 // Previously it means `--cypress` without `--vitest`.
index ecb74ae6768b171351216e09a39be212dbecf9b5..ca48646a11d342603cf249cdb89aa55aeaed1802 100644 (file)
@@ -15,36 +15,11 @@ export default function renderEslint(
   rootDir,
   { needsTypeScript, needsCypress, needsCypressCT, needsPrettier, needsPlaywright }
 ) {
-  const additionalConfig: Linter.Config = {}
-  const additionalDependencies = {}
-
-  if (needsCypress) {
-    additionalConfig.overrides = [
-      {
-        files: needsCypressCT
-          ? [
-              '**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}',
-              'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}',
-              'cypress/support/**/*.{js,ts,jsx,tsx}'
-            ]
-          : ['cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}', 'cypress/support/**/*.{js,ts,jsx,tsx}'],
-        extends: ['plugin:cypress/recommended']
-      }
-    ]
-
-    additionalDependencies['eslint-plugin-cypress'] = eslintDeps['eslint-plugin-cypress']
-  }
-
-  if (needsPlaywright) {
-    additionalConfig.overrides = [
-      {
-        files: ['e2e/**/*.{test,spec}.{js,ts,jsx,tsx}'],
-        extends: ['plugin:playwright/recommended']
-      }
-    ]
-
-    additionalDependencies['eslint-plugin-playwright'] = eslintDeps['eslint-plugin-playwright']
-  }
+  const { additionalConfig, additionalDependencies } = getAdditionalConfigAndDependencies({
+    needsCypress,
+    needsCypressCT,
+    needsPlaywright
+  })
 
   const { pkg, files } = createESLintConfig({
     vueVersion: '3.x',
@@ -86,3 +61,42 @@ export default function renderEslint(
     fs.writeFileSync(fullPath, content as string, 'utf-8')
   }
 }
+
+// visible for testing
+export function getAdditionalConfigAndDependencies({
+  needsCypress,
+  needsCypressCT,
+  needsPlaywright
+}) {
+  const additionalConfig: Linter.Config = {}
+  const additionalDependencies = {}
+
+  if (needsCypress) {
+    additionalConfig.overrides = [
+      {
+        files: needsCypressCT
+          ? [
+              '**/__tests__/*.{cy,spec}.{js,ts,jsx,tsx}',
+              'cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}',
+              'cypress/support/**/*.{js,ts,jsx,tsx}'
+            ]
+          : ['cypress/e2e/**/*.{cy,spec}.{js,ts,jsx,tsx}', 'cypress/support/**/*.{js,ts,jsx,tsx}'],
+        extends: ['plugin:cypress/recommended']
+      }
+    ]
+
+    additionalDependencies['eslint-plugin-cypress'] = eslintDeps['eslint-plugin-cypress']
+  }
+
+  if (needsPlaywright) {
+    additionalConfig.overrides = [
+      {
+        files: ['e2e/**/*.{test,spec}.{js,ts,jsx,tsx}'],
+        extends: ['plugin:playwright/recommended']
+      }
+    ]
+
+    additionalDependencies['eslint-plugin-playwright'] = eslintDeps['eslint-plugin-playwright']
+  }
+  return { additionalConfig, additionalDependencies }
+}