Skip to content

Commit

Permalink
test: replace jest with vitest (#156)
Browse files Browse the repository at this point in the history
* test: replace jest with vitest

* chore: remove built-in modules prefix

* chore: version bump to v2.0.0-beta.0
  • Loading branch information
zce authored Jul 26, 2023
1 parent 18dc1be commit a41af44
Show file tree
Hide file tree
Showing 31 changed files with 204 additions and 286 deletions.
17 changes: 9 additions & 8 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,29 @@ name: CI
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
test:
strategy:
matrix:
node-version: [14, 16, 17, 18]
os: [ubuntu-latest, windows-latest]
node: [16, 18, 20]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
node-version: ${{ matrix.node }}
- run: npm i pnpm yarn -g
- run: git config --global user.name "GitHub Actions"
- run: git config --global user.email "bots@github.com"
- run: npm install
- run: npm run lint
- run: npm run build
- run: npm run test
- run: npm i @vitest/coverage-v8
- run: npx ts-standard
- run: npx vitest run --coverage
- run: npx codecov

publish:
if: startsWith(github.ref, 'refs/tags')
needs: build
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
57 changes: 21 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "caz",
"version": "1.1.0",
"version": "2.0.0-beta.0",
"description": "A simple yet powerful template-based Scaffolding tools.",
"keywords": [
"productivity",
Expand Down Expand Up @@ -37,7 +37,7 @@
"scripts": {
"build": "tsup",
"lint": "ts-standard",
"test": "jest --coverage",
"test": "vitest run",
"prepare": "husky install .github/husky"
},
"tsup": {
Expand All @@ -49,8 +49,8 @@
"minify": true,
"splitting": true,
"dts": {
"resolve": true,
"entry": "src/index.ts"
"entry": "src/index.ts",
"resolve": true
}
},
"commitlint": {
Expand All @@ -61,55 +61,40 @@
"lint-staged": {
"*.{ts,js}": "ts-standard --fix"
},
"jest": {
"preset": "ts-jest/presets/js-with-ts",
"testTimeout": 20000,
"testEnvironment": "node",
"collectCoverageFrom": [
"src/**"
],
"moduleNameMapper": {
"#(.*)": "<rootDir>/node_modules/$1"
},
"transformIgnorePatterns": [
"node_modules/(?!(node-fetch|fetch-blob|formdata-polyfill|data-uri-to-buffer|env-paths|ora|cli-cursor|restore-cursor|chalk|log-symbols|is-unicode-supported|is-interactive|strip-ansi|ansi-regex))"
]
},
"renovate": {
"extends": [
"zce"
]
},
"devDependencies": {
"@commitlint/cli": "17.4.2",
"@commitlint/config-conventional": "17.4.2",
"@commitlint/cli": "17.6.7",
"@commitlint/config-conventional": "17.6.7",
"@types/adm-zip": "0.5.0",
"@types/ini": "1.3.31",
"@types/lodash": "4.14.191",
"@types/node": "18.11.18",
"@types/semver": "7.3.13",
"@types/lodash": "4.14.195",
"@types/node": "20.4.4",
"@types/semver": "7.5.0",
"@types/validate-npm-package-name": "4.0.0",
"adm-zip": "0.5.10",
"cac": "6.7.14",
"env-paths": "3.0.0",
"fast-glob": "3.2.12",
"fast-glob": "3.3.1",
"husky": "8.0.3",
"ini": "3.0.1",
"jest": "29.4.1",
"lint-staged": "13.1.0",
"ini": "4.1.1",
"lint-staged": "13.2.3",
"lodash": "4.17.21",
"node-fetch": "3.3.0",
"ora": "6.1.2",
"node-fetch": "3.3.1",
"ora": "6.3.1",
"prompts": "2.4.2",
"semver": "7.3.8",
"socks-proxy-agent": "7.0.0",
"ts-jest": "29.0.5",
"semver": "7.5.4",
"socks-proxy-agent": "8.0.1",
"ts-standard": "12.0.2",
"tsup": "6.5.0",
"typescript": "4.9.5",
"validate-npm-package-name": "5.0.0"
"tsup": "7.1.0",
"typescript": "5.1.6",
"validate-npm-package-name": "5.0.0",
"vitest": "0.33.0"
},
"engines": {
"node": ">=14.14"
"node": ">=16"
}
}
48 changes: 2 additions & 46 deletions src/cli.spec.ts
Original file line number Diff line number Diff line change
@@ -1,61 +1,17 @@
import { jest, test, expect, beforeEach, afterAll } from '@jest/globals'

const mockedInit = jest.fn()
const mockedList = jest.fn()
import { vi, test, expect } from 'vitest'

const mockArgv = (...args: string[]): () => void => {
const original = process.argv
process.argv = original.slice(0, 2).concat(...args)
return () => { process.argv = original }
}

beforeEach(async () => {
jest.resetAllMocks()
jest.resetModules()
jest.mock('.', () => ({ __esModule: true, default: mockedInit, list: mockedList }))
})

afterAll(async () => {
jest.clearAllMocks()
})

test('unit:cli:init', async () => {
const restore = mockArgv('template', 'project', '--force', '--offline', '--foo', 'bar')
await import('./cli')
expect(mockedInit).toHaveBeenCalled()
expect(mockedInit.mock.calls[0][0]).toBe('template')
expect(mockedInit.mock.calls[0][1]).toBe('project')
expect(mockedInit.mock.calls[0][2]).toEqual({ '--': [], f: true, force: true, o: true, offline: true, foo: 'bar' })
restore()
})

test('unit:cli:list', async () => {
const restore = mockArgv('list', 'zce', '--json', '--short')
await import('./cli')
expect(mockedList).toHaveBeenCalled()
expect(mockedList.mock.calls[0][0]).toBe('zce')
expect(mockedList.mock.calls[0][1]).toEqual({ '--': [], j: true, json: true, s: true, short: true })
restore()
})

test('unit:cli:help', async () => {
const restore = mockArgv('--help')
const log = jest.spyOn(console, 'log')
const log = vi.spyOn(console, 'log')
await import('./cli')
expect(log).toHaveBeenCalledTimes(1)
expect(log.mock.calls[0][0]).toContain('$ caz <template> [project]')
log.mockRestore()
restore()
})

// // TODO: error
// // https://github.com/facebook/jest/issues/5620
// test('unit:cli:error', async () => {
// const error = jest.spyOn(console, 'error')
// const exit = jest.spyOn(process, 'exit')
// const restore = mockArgv()
// await import('./cli')
// expect(error).toHaveBeenCalled()
// expect(exit).toHaveBeenCalled()
// restore()
// })
1 change: 0 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cli
cli.help().version(version).parse()

// https://github.com/cacjs/cac#error-handling
/* istanbul ignore next */
const onError = (err: Error): void => {
// output details when exception occurs
cli.options.debug as boolean && console.error(err)
Expand Down
13 changes: 6 additions & 7 deletions src/complete.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { SpyInstance } from 'jest-mock'
import { jest, test, expect, beforeEach, afterEach } from '@jest/globals'
import { vi, test, expect, beforeEach, afterEach, SpyInstance } from 'vitest'
import { context } from '../test/helpers'
import complete from './complete'
import { Context } from './types'

let log: SpyInstance<(message?: any, ...optionalParams: any[]) => void>
let log: SpyInstance<[message?: any, ...optionalParams: any[]], void>

beforeEach(async () => {
log = jest.spyOn(console, 'log')
log = vi.spyOn(console, 'log')
})

afterEach(async () => {
Expand Down Expand Up @@ -39,15 +38,15 @@ test('unit:complete:string', async () => {
})

test('unit:complete:callback', async () => {
const callback = jest.fn<(ctx: Context) => string | Promise<string> | Promise<void>>()
const callback = vi.fn<(ctx: Context) => string | Promise<string> | Promise<void>>()
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback.mock.calls[0][0]).toBe(ctx)
})

test('unit:complete:callback-return', async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-parens
const callback = jest.fn<(ctx: Context) => string | Promise<string> | Promise<void>>(() => 'completed')
const callback = vi.fn<(ctx: Context) => string | Promise<string> | Promise<void>>(() => 'completed')
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback).toHaveBeenCalled()
Expand All @@ -56,7 +55,7 @@ test('unit:complete:callback-return', async () => {

test('unit:complete:callback-promise', async () => {
// eslint-disable-next-line @typescript-eslint/no-extra-parens
const callback = jest.fn<(ctx: Context) => string | Promise<string> | Promise<void>>(async () => 'completed')
const callback = vi.fn<(ctx: Context) => string | Promise<string> | Promise<void>>(async () => 'completed')
const ctx = context({}, { complete: callback })
await complete(ctx)
expect(callback).toHaveBeenCalled()
Expand Down
Loading

0 comments on commit a41af44

Please sign in to comment.