Skip to content

Commit 8b6c2cd

Browse files
authored
feat: add unit tests using vitest (#377)
1 parent 5ba8f64 commit 8b6c2cd

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

.github/workflows/ci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ jobs:
2727
env:
2828
CYPRESS_INSTALL_BINARY: 0
2929
- run: pnpm build
30+
- run: pnpm test:unit
3031

3132
# Use cache to share the output across different jobs
3233
# No need to cache node_modules because they are all bundled

__test__/getCommand.spec.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { it, describe, expect } from 'vitest'
2+
import getCommand from '../utils/getCommand'
3+
4+
describe('getCommand', () => {
5+
it('should generate the correct command for yarn', () => {
6+
expect(getCommand('yarn', 'install')).toBe('yarn')
7+
expect(getCommand('yarn', 'dev')).toBe('yarn dev')
8+
expect(getCommand('yarn', 'build')).toBe('yarn build')
9+
})
10+
it('should generate the correct command for npm', () => {
11+
expect(getCommand('npm', 'install')).toBe('npm install')
12+
expect(getCommand('npm', 'dev')).toBe('npm run dev')
13+
expect(getCommand('npm', 'build')).toBe('npm run build')
14+
})
15+
it('should generate the correct command for pnpm', () => {
16+
expect(getCommand('pnpm', 'install')).toBe('pnpm install')
17+
expect(getCommand('pnpm', 'dev')).toBe('pnpm dev')
18+
expect(getCommand('pnpm', 'build')).toBe('pnpm build')
19+
})
20+
})

__test__/sortDependencies.spec.ts

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { it, describe, expect } from 'vitest'
2+
import sortDependencies from '../utils/sortDependencies'
3+
4+
describe('sortDependencies', () => {
5+
it('should sort dependencies and dev dependencies', () => {
6+
const packageJson = {
7+
dependencies: {
8+
vue: '^3.3.4',
9+
'vue-router': '^4.2.5',
10+
pinia: '^2.1.7'
11+
},
12+
devDependencies: {
13+
'@vitejs/plugin-vue-jsx': '^3.0.2',
14+
jsdom: '^22.1.0',
15+
'start-server-and-test': '^2.0.1',
16+
vite: '^4.4.11',
17+
'@vue/test-utils': '^2.4.1',
18+
cypress: '^13.3.1',
19+
eslint: '^8.49.0',
20+
'@vitejs/plugin-vue': '^4.4.0',
21+
'eslint-plugin-cypress': '^2.15.1',
22+
'eslint-plugin-vue': '^9.17.0',
23+
vitest: '^0.34.6'
24+
}
25+
}
26+
expect(sortDependencies(packageJson)).toStrictEqual({
27+
dependencies: {
28+
pinia: '^2.1.7',
29+
vue: '^3.3.4',
30+
'vue-router': '^4.2.5'
31+
},
32+
devDependencies: {
33+
'@vitejs/plugin-vue': '^4.4.0',
34+
'@vitejs/plugin-vue-jsx': '^3.0.2',
35+
'@vue/test-utils': '^2.4.1',
36+
cypress: '^13.3.1',
37+
eslint: '^8.49.0',
38+
'eslint-plugin-cypress': '^2.15.1',
39+
'eslint-plugin-vue': '^9.17.0',
40+
jsdom: '^22.1.0',
41+
'start-server-and-test': '^2.0.1',
42+
vite: '^4.4.11',
43+
vitest: '^0.34.6'
44+
}
45+
})
46+
})
47+
})

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"snapshot": "zx ./scripts/snapshot.mjs",
2222
"pretest": "run-s build snapshot",
2323
"test": "zx ./scripts/test.mjs",
24+
"test:unit": "vitest",
2425
"prepublishOnly": "zx ./scripts/prepublish.mjs"
2526
},
2627
"repository": {
@@ -51,6 +52,7 @@
5152
"npm-run-all2": "^6.1.1",
5253
"prettier": "^3.1.0",
5354
"prompts": "^2.4.2",
55+
"vitest": "^0.34.6",
5456
"zx": "^7.2.3"
5557
},
5658
"lint-staged": {

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vitest.config.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['__test__/**.spec.ts']
6+
}
7+
})

0 commit comments

Comments
 (0)