From a41af44ade51d56c0607957792b06d4505b9a8d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=AA=E7=A3=8A?= Date: Wed, 26 Jul 2023 15:37:32 +0800 Subject: [PATCH] test: replace jest with vitest (#156) * test: replace jest with vitest * chore: remove built-in modules prefix * chore: version bump to v2.0.0-beta.0 --- .github/workflows/main.yml | 17 ++++---- package.json | 57 ++++++++++---------------- src/cli.spec.ts | 48 +--------------------- src/cli.ts | 1 - src/complete.spec.ts | 13 +++--- src/confirm.spec.ts | 84 +++++++++++++++----------------------- src/core/config.spec.ts | 19 ++++----- src/core/exec.spec.ts | 2 +- src/core/file.spec.ts | 22 +++++----- src/core/file.ts | 6 +-- src/core/http.spec.ts | 8 ++-- src/core/http.ts | 4 +- src/core/index.spec.ts | 2 +- src/core/ware.spec.ts | 2 +- src/emit.spec.ts | 10 ++--- src/index.spec.ts | 26 ++++++------ src/init.spec.ts | 16 ++++---- src/inquire.spec.ts | 64 ++++++++++++++--------------- src/inquire.ts | 4 +- src/install.spec.ts | 18 ++++---- src/install.ts | 2 +- src/list/fetch.spec.ts | 2 +- src/list/index.spec.ts | 14 +++---- src/load.spec.ts | 6 +-- src/load.ts | 2 +- src/prepare.spec.ts | 4 +- src/rename.spec.ts | 2 +- src/render.spec.ts | 2 +- src/resolve.spec.ts | 19 ++++----- src/setup.spec.ts | 4 +- test/helpers.ts | 10 ++--- 31 files changed, 204 insertions(+), 286 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 403a348d..84f9a2ef 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/package.json b/package.json index 690820a0..3f7ec86e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -37,7 +37,7 @@ "scripts": { "build": "tsup", "lint": "ts-standard", - "test": "jest --coverage", + "test": "vitest run", "prepare": "husky install .github/husky" }, "tsup": { @@ -49,8 +49,8 @@ "minify": true, "splitting": true, "dts": { - "resolve": true, - "entry": "src/index.ts" + "entry": "src/index.ts", + "resolve": true } }, "commitlint": { @@ -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": { - "#(.*)": "/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" } } diff --git a/src/cli.spec.ts b/src/cli.spec.ts index 0b6db054..b27dd2e8 100644 --- a/src/cli.spec.ts +++ b/src/cli.spec.ts @@ -1,7 +1,4 @@ -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 @@ -9,53 +6,12 @@ const mockArgv = (...args: string[]): () => void => { 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