]> git.ipfire.org Git - thirdparty/vuejs/create-vue.git/commitdiff
feat: add unit tests using vitest (#377)
authorCoolPlayLin <coolplaylin@qq.com>
Sat, 25 Nov 2023 09:53:40 +0000 (17:53 +0800)
committerGitHub <noreply@github.com>
Sat, 25 Nov 2023 09:53:40 +0000 (10:53 +0100)
.github/workflows/ci.yml
__test__/getCommand.spec.ts [new file with mode: 0644]
__test__/sortDependencies.spec.ts [new file with mode: 0644]
package.json
pnpm-lock.yaml
vitest.config.ts [new file with mode: 0644]

index 5444e9c33e9240bcbb01a9c1aaba476be98bc68f..97f4f29e277d4e0a52b08f667bb69c6e3058119d 100644 (file)
@@ -27,6 +27,7 @@ jobs:
         env:
           CYPRESS_INSTALL_BINARY: 0
       - run: pnpm build
+      - run: pnpm test:unit
 
       # Use cache to share the output across different jobs
       # No need to cache node_modules because they are all bundled
diff --git a/__test__/getCommand.spec.ts b/__test__/getCommand.spec.ts
new file mode 100644 (file)
index 0000000..63c86a9
--- /dev/null
@@ -0,0 +1,20 @@
+import { it, describe, expect } from 'vitest'
+import getCommand from '../utils/getCommand'
+
+describe('getCommand', () => {
+  it('should generate the correct command for yarn', () => {
+    expect(getCommand('yarn', 'install')).toBe('yarn')
+    expect(getCommand('yarn', 'dev')).toBe('yarn dev')
+    expect(getCommand('yarn', 'build')).toBe('yarn build')
+  })
+  it('should generate the correct command for npm', () => {
+    expect(getCommand('npm', 'install')).toBe('npm install')
+    expect(getCommand('npm', 'dev')).toBe('npm run dev')
+    expect(getCommand('npm', 'build')).toBe('npm run build')
+  })
+  it('should generate the correct command for pnpm', () => {
+    expect(getCommand('pnpm', 'install')).toBe('pnpm install')
+    expect(getCommand('pnpm', 'dev')).toBe('pnpm dev')
+    expect(getCommand('pnpm', 'build')).toBe('pnpm build')
+  })
+})
diff --git a/__test__/sortDependencies.spec.ts b/__test__/sortDependencies.spec.ts
new file mode 100644 (file)
index 0000000..29becf4
--- /dev/null
@@ -0,0 +1,47 @@
+import { it, describe, expect } from 'vitest'
+import sortDependencies from '../utils/sortDependencies'
+
+describe('sortDependencies', () => {
+  it('should sort dependencies and dev dependencies', () => {
+    const packageJson = {
+      dependencies: {
+        vue: '^3.3.4',
+        'vue-router': '^4.2.5',
+        pinia: '^2.1.7'
+      },
+      devDependencies: {
+        '@vitejs/plugin-vue-jsx': '^3.0.2',
+        jsdom: '^22.1.0',
+        'start-server-and-test': '^2.0.1',
+        vite: '^4.4.11',
+        '@vue/test-utils': '^2.4.1',
+        cypress: '^13.3.1',
+        eslint: '^8.49.0',
+        '@vitejs/plugin-vue': '^4.4.0',
+        'eslint-plugin-cypress': '^2.15.1',
+        'eslint-plugin-vue': '^9.17.0',
+        vitest: '^0.34.6'
+      }
+    }
+    expect(sortDependencies(packageJson)).toStrictEqual({
+      dependencies: {
+        pinia: '^2.1.7',
+        vue: '^3.3.4',
+        'vue-router': '^4.2.5'
+      },
+      devDependencies: {
+        '@vitejs/plugin-vue': '^4.4.0',
+        '@vitejs/plugin-vue-jsx': '^3.0.2',
+        '@vue/test-utils': '^2.4.1',
+        cypress: '^13.3.1',
+        eslint: '^8.49.0',
+        'eslint-plugin-cypress': '^2.15.1',
+        'eslint-plugin-vue': '^9.17.0',
+        jsdom: '^22.1.0',
+        'start-server-and-test': '^2.0.1',
+        vite: '^4.4.11',
+        vitest: '^0.34.6'
+      }
+    })
+  })
+})
index 77944b7cf4efe768c0fcb2d772d50646d5fbbc8b..eacecba3ade4ae03f6e0c58d5e53dea0d1630694 100644 (file)
@@ -21,6 +21,7 @@
     "snapshot": "zx ./scripts/snapshot.mjs",
     "pretest": "run-s build snapshot",
     "test": "zx ./scripts/test.mjs",
+    "test:unit": "vitest",
     "prepublishOnly": "zx ./scripts/prepublish.mjs"
   },
   "repository": {
@@ -51,6 +52,7 @@
     "npm-run-all2": "^6.1.1",
     "prettier": "^3.1.0",
     "prompts": "^2.4.2",
+    "vitest": "^0.34.6",
     "zx": "^7.2.3"
   },
   "lint-staged": {
index 6a9d6d9725a951a39fdfbd5fd01a04bf6313b4ce..ca5f384d87e7bf1e94c93a089b3ab916d5bed4cc 100644 (file)
@@ -56,6 +56,9 @@ importers:
       prompts:
         specifier: ^2.4.2
         version: 2.4.2
+      vitest:
+        specifier: ^0.34.6
+        version: 0.34.6(jsdom@22.1.0)
       zx:
         specifier: ^7.2.3
         version: 7.2.3
diff --git a/vitest.config.ts b/vitest.config.ts
new file mode 100644 (file)
index 0000000..6a1eda4
--- /dev/null
@@ -0,0 +1,7 @@
+import { defineConfig } from 'vitest/config'
+
+export default defineConfig({
+  test: {
+    include: ['__test__/**.spec.ts']
+  }
+})