Skip to content

Commit

Permalink
feat: add unit tests using vitest (vuejs#377)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoolPlayLin authored Nov 25, 2023
1 parent 5ba8f64 commit 8b6c2cd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions __test__/getCommand.spec.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
47 changes: 47 additions & 0 deletions __test__/sortDependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -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'
}
})
})
})
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include: ['__test__/**.spec.ts']
}
})

0 comments on commit 8b6c2cd

Please sign in to comment.