Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use vitest to make unitest #377

Merged
merged 27 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
4e5dde0
feat: use vitest to make unitest
CoolPlayLin Nov 12, 2023
25332c5
add getCommand unittest
CoolPlayLin Nov 12, 2023
95d644a
add unittest to workflow
CoolPlayLin Nov 12, 2023
b41d844
add verify locale(it fails I don't know why)
CoolPlayLin Nov 18, 2023
4e5284a
fix typo
CoolPlayLin Nov 18, 2023
c340af2
bugfix for key-checking
CoolPlayLin Nov 18, 2023
5d2463a
fix
CoolPlayLin Nov 18, 2023
3f9af05
Revert "fix"
CoolPlayLin Nov 18, 2023
b4e75ba
Squashed commit of the following:
CoolPlayLin Nov 18, 2023
8f0fe1f
Merge branch 'main' into CoolPlayLin/vitest
CoolPlayLin Nov 18, 2023
cc65b9d
fix lock file
CoolPlayLin Nov 18, 2023
3444b9c
🤔
CoolPlayLin Nov 18, 2023
f074859
add test for validate no unnecessary keys
CoolPlayLin Nov 18, 2023
a2f2e90
add locale validate
CoolPlayLin Nov 18, 2023
ec91736
add support for reusable locale
CoolPlayLin Nov 18, 2023
e4459e0
add test for `sortDependencies` & add snapshot
CoolPlayLin Nov 18, 2023
39e61e2
:sparkles: feat: add error message
CoolPlayLin Nov 19, 2023
e7ee1f1
:bug: fix: nothing
CoolPlayLin Nov 19, 2023
837838b
:bug: fix: nothing
CoolPlayLin Nov 19, 2023
d2aebec
:bug: fix: delete unnecessary assertions
CoolPlayLin Nov 25, 2023
431cfda
:hammer: chore: delete locale test
CoolPlayLin Nov 25, 2023
69b980d
:hammer: chore: delete schema
CoolPlayLin Nov 25, 2023
e06e995
:hammer: chore: delete 'test:update-snapshot' in package.json
CoolPlayLin Nov 25, 2023
ad687fb
:bug: fix: resolve suggestions in code reviews
CoolPlayLin Nov 25, 2023
ab7bb79
:bug: fix: delete unncessary change
CoolPlayLin Nov 25, 2023
b4ccf03
:hammer: chore: format test
CoolPlayLin Nov 25, 2023
347cbab
Merge branch 'main' into CoolPlayLin/vitest
CoolPlayLin Nov 25, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
19 changes: 19 additions & 0 deletions __test__/__snapshots__/getCommand.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`should generate correct command > for npm 1`] = `"npm install"`;

exports[`should generate correct command > for npm 2`] = `"npm run dev"`;

exports[`should generate correct command > for npm 3`] = `"npm run build"`;

exports[`should generate correct command > for pnpm 1`] = `"pnpm install"`;

exports[`should generate correct command > for pnpm 2`] = `"pnpm dev"`;

exports[`should generate correct command > for pnpm 3`] = `"pnpm build"`;

exports[`should generate correct command > for yarn 1`] = `"yarn"`;

exports[`should generate correct command > for yarn 2`] = `"yarn dev"`;

exports[`should generate correct command > for yarn 3`] = `"yarn build"`;
31 changes: 31 additions & 0 deletions __test__/getCommand.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { it, describe, expect } from 'vitest'
import getCommand from '../utils/getCommand'

describe("should generate correct command", () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
it('for yarn', () => {
expect(getCommand('yarn', 'install')).toBe('yarn')
expect(getCommand('yarn', 'install')).toMatchSnapshot()
expect(getCommand('yarn', 'dev')).toBe('yarn dev')
expect(getCommand('yarn', 'dev')).toMatchSnapshot()
expect(getCommand('yarn', 'build')).toBe('yarn build')
expect(getCommand('yarn', 'build')).toMatchSnapshot()

CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
})
it('for npm', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
expect(getCommand('npm', 'install')).toBe('npm install')
expect(getCommand('npm', 'install')).toMatchSnapshot()
expect(getCommand('npm', 'dev')).toBe('npm run dev')
expect(getCommand('npm', 'dev')).toMatchSnapshot()
expect(getCommand('npm', 'build')).toBe('npm run build')
expect(getCommand('npm', 'build')).toMatchSnapshot()

CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
})
it('for pnpm', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
expect(getCommand('pnpm', 'install')).toBe('pnpm install')
expect(getCommand('pnpm', 'install')).toMatchSnapshot()
expect(getCommand('pnpm', 'dev')).toBe('pnpm dev')
expect(getCommand('pnpm', 'dev')).toMatchSnapshot()
expect(getCommand('pnpm', 'build')).toBe('pnpm build')
expect(getCommand('pnpm', 'build')).toMatchSnapshot()
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
})
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
})
44 changes: 44 additions & 0 deletions __test__/locale.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, it, expect } from 'vitest'
import { resolve } from 'node:path'
import { readdirSync } from 'node:fs'
import { Language } from '../utils/getLanguage'
import { includeAllKeys, excludeKeys } from './utils'

const locales = readdirSync(resolve(__dirname, '../locales')).filter((file) => {
return file.includes('.json')
})

describe('should match name regex', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
/**
*
* both can match normal locale or reusable locale
*
* @example normal locale: en-US
* @example reusable locale: zh-Hant
*/
const regex = /^[a-zA-Z]{2}(-[a-zA-Z]{2})*.json$|^[a-zA-Z]{2}(-[a-zA-z]{4})*.json$/

locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(locale).toMatch(regex)
})
})
})

describe('should include full keys', () => {
const structure = require('../schema/locale.json') as Language
locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(includeAllKeys(require(`../locales/${locale}`), structure)).toBeTruthy()
})
})
})

describe("should not include extra keys", () => {
const structure = require('../schema/locale.json') as Language
locales.forEach((locale) => {
it(`for ${locale}`, () => {
expect(excludeKeys(require(`../locales/${locale}`), structure)).toBeTruthy()
})
})
})
80 changes: 80 additions & 0 deletions __test__/sortDependencies.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { it, describe, expect } from 'vitest'
import sortDependencies from '../utils/sortDependencies'

describe('should output correct sorted value', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
it('#1', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
const packageJson = {
"devDependencies": {
"@vitejs/plugin-vue": "^4.4.0",
"@vitejs/plugin-vue-jsx": "^3.0.2",
"eslint": "^8.49.0",
"eslint-plugin-cypress": "^2.15.1",
"vite": "^4.4.11",
"vitest": "^0.34.6",
"@vue/test-utils": "^2.4.1",
"cypress": "^13.3.1",
"eslint-plugin-vue": "^9.17.0",
"jsdom": "^22.1.0",
"start-server-and-test": "^2.0.1",
}
}
expect(sortDependencies(packageJson)).toStrictEqual({
"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"
}
}
)
})
it('#2', () => {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
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"
}
})
})
})
48 changes: 48 additions & 0 deletions __test__/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether missed some keys
*/
export function includeAllKeys(obj: Object, schema: Object) {
for (let key in schema) {
if (!obj.hasOwnProperty(key)) {
console.log(`key '${key}' lost`)
return false
}
if (schema[key] !== null) {
if (typeof schema[key] === 'string') {
if (typeof obj[key] !== schema[key]) {
console.error(`the type of ${key} is incorrect`)
return false
}
} else if (typeof schema[key] === 'object') {
if (!includeAllKeys(obj[key], schema[key])) {
return false
}
}
}
}
return true
}

/**
*
* @param obj object that needs to be validated
* @param schema template for validation
* @returns whether include extra keys
*/
export function excludeKeys(obj: Object, schema: Object) {
for (let key in obj) {
if (!schema.hasOwnProperty(key)) {
console.error(`unexpected key: ${key}`)
return false
}
if (schema[key] !== null && typeof schema[key] === 'object') {
if (!excludeKeys(obj[key], schema[key])) {
return false
}
}
}
return true
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"snapshot": "zx ./scripts/snapshot.mjs",
"pretest": "run-s build snapshot",
"test": "zx ./scripts/test.mjs",
"test:unit": "vitest",
"test:update-snapshot": "vitest run -u",
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
"prepublishOnly": "zx ./scripts/prepublish.mjs"
},
"repository": {
Expand Down Expand Up @@ -51,6 +53,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.

68 changes: 68 additions & 0 deletions schema/locale.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"projectName": {
"message": "string"
},
"shouldOverwrite": {
"dirForPrompts": {
"current": "string",
"target": "string"
},
"message": "string"
},
"packageName": {
"message": "string",
"invalidMessage": "string"
},
"needsTypeScript": {
"message": "string"
},
"needsJsx": {
"message": "string"
},
"needsRouter": {
"message": "string"
},
"needsPinia": {
"message": "string"
},
"needsVitest": {
"message": "string"
},
"needsE2eTesting": {
"message": "string",
"hint": "string",
"selectOptions": {
"negative": {
"title": "string"
},
"cypress": {
"title": "string",
"desc": "string"
},
"nightwatch": {
"title": "string",
"desc": "string"
},
"playwright": {
"title": "string"
}
}
},
"needsEslint": {
"message": "string"
},
"needsPrettier": {
"message": "string"
},
"errors": {
"operationCancelled": "string"
},
"defaultToggleOptions": {
"active": "string",
"inactive": "string"
},
"infos": {
"scaffolding": "string",
"done": "string"
}
}
2 changes: 1 addition & 1 deletion utils/getLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ interface LanguageItem {
}
}

interface Language {
export interface Language {
CoolPlayLin marked this conversation as resolved.
Show resolved Hide resolved
projectName: LanguageItem
shouldOverwrite: LanguageItem
packageName: LanguageItem
Expand Down
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']
}
})