diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1cc092d..d35964a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,7 @@ jobs: run: npm install - name: Run Jest tests - run: npm run test + run: npm run test:real env: TEST_GITEE_TOKEN: ${{ secrets.TEST_GITEE_TOKEN }} TEST_GITEE_OWNER: ${{ secrets.TEST_GITEE_OWNER }} diff --git a/jest.config.fake.js b/jest.config.fake.js new file mode 100644 index 0000000..7f46d16 --- /dev/null +++ b/jest.config.fake.js @@ -0,0 +1,20 @@ +module.exports = { + testTimeout: 120000, + preset: 'ts-jest', + testEnvironment: 'node', + collectCoverageFrom: [ + "src/**/*.ts", // 包括 src 目录下所有的 TypeScript 文件 + "!src/**/*.d.ts", // 排除 TypeScript 声明文件 + "!src/__tests__/**/*.ts" + ], + testMatch: ["/src/__tests__/**/*.test.ts"], + testPathIgnorePatterns: [ + "src/__tests__/authenticate-gitee.test.ts", + "src/__tests__/authenticate-github.test.ts", + "src/__tests__/authenticate-gitlab.test.ts", + "src/__tests__/book-repository.test.ts", + "src/__tests__/chat-repository.test.ts", + "src/__tests__/user-repository.test.ts", + "src/__tests__/wx-request.test.ts" + ] +}; diff --git a/jest.config.js b/jest.config.real.js similarity index 60% rename from jest.config.js rename to jest.config.real.js index 75da8e1..8f7b89e 100644 --- a/jest.config.js +++ b/jest.config.real.js @@ -1,11 +1,15 @@ -module.exports = { - testTimeout: 120000, - preset: 'ts-jest', - testEnvironment: 'node', - collectCoverageFrom: [ - "src/**/*.ts", // 包括 src 目录下所有的 TypeScript 文件 - "!src/**/*.d.ts", // 排除 TypeScript 声明文件 - "!src/__tests__/**/*.ts" - ], - testMatch: ["/src/__tests__/**/*.test.ts"] -}; +module.exports = { + testTimeout: 120000, + preset: 'ts-jest', + testEnvironment: 'node', + collectCoverageFrom: [ + "src/**/*.ts", // 包括 src 目录下所有的 TypeScript 文件 + "!src/**/*.d.ts", // 排除 TypeScript 声明文件 + "!src/__tests__/**/*.ts" + ], + testMatch: ["/src/__tests__/**/*.test.ts"], + testPathIgnorePatterns: [ + "src/__tests__/fake-authenticate-gitee.test.ts", + "src/__tests__/wx-request.test.ts" + ] +}; diff --git a/package-lock.json b/package-lock.json index aa683d8..004701d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "@types/jest": "^29.5.9", "@types/wechat-miniprogram": "^3.4.7", "axios": "^1.6.2", + "axios-mock-adapter": "^1.22.0", "crypto-js": "^4.2.0", "dayjs": "^1.11.10", "dotenv": "^16.3.1", @@ -3062,6 +3063,19 @@ "proxy-from-env": "^1.1.0" } }, + "node_modules/axios-mock-adapter": { + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz", + "integrity": "sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==", + "dev": true, + "dependencies": { + "fast-deep-equal": "^3.1.3", + "is-buffer": "^2.0.5" + }, + "peerDependencies": { + "axios": ">= 0.17.0" + } + }, "node_modules/babel-jest": { "version": "29.7.0", "resolved": "https://registry.npmjs.org/babel-jest/-/babel-jest-29.7.0.tgz", @@ -4038,6 +4052,12 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true + }, "node_modules/fast-json-stable-stringify": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz", @@ -4402,6 +4422,29 @@ "integrity": "sha512-zz06S8t0ozoDXMG+ube26zeCTNXcKIPJZJi8hBrF4idCLms4CG9QtK7qBl1boi5ODzFpjswb5JPmHCbMpjaYzg==", "dev": true }, + "node_modules/is-buffer": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz", + "integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "engines": { + "node": ">=4" + } + }, "node_modules/is-builtin-module": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz", diff --git a/package.json b/package.json index 1d8c24a..3acc4a6 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "module": "dist/bundle.esm.js", "types": "dist/index.d.ts", "scripts": { - "test": "jest --coverage", + "test:fake": "jest --coverage --config=jest.config.fake.js", + "test:real": "jest --coverage --config=jest.config.real.js", "prebuild": "rimraf ./dist", "build": "rollup --config" }, @@ -27,6 +28,7 @@ "@types/jest": "^29.5.9", "@types/wechat-miniprogram": "^3.4.7", "axios": "^1.6.2", + "axios-mock-adapter": "^1.22.0", "crypto-js": "^4.2.0", "dayjs": "^1.11.10", "dotenv": "^16.3.1", diff --git a/src/__tests__/fake-authenticate-gitee.test.ts b/src/__tests__/fake-authenticate-gitee.test.ts new file mode 100644 index 0000000..2f6c110 --- /dev/null +++ b/src/__tests__/fake-authenticate-gitee.test.ts @@ -0,0 +1,37 @@ +import { giteeRequest } from './helper/helper'; +describe('Test Use Fake Authenticate Gitee', () => { + test('Test Authenticate Gitee', async () => { + const res = await giteeRequest.authenticate(); + expect(res).toEqual({ + id: 1000001, + login: '***', + name: '***', + avatar_url: 'https://foruda.gitee.com/avatar/***/***.png', + url: 'https://gitee.com/api/v5/users/***', + html_url: 'https://gitee.com/***', + remark: '', + followers_url: 'https://gitee.com/api/v5/users/***/followers', + following_url: 'https://gitee.com/api/v5/users/***/following_url{/other_user}', + gists_url: 'https://gitee.com/api/v5/users/***/gists{/gist_id}', + starred_url: 'https://gitee.com/api/v5/users/***/starred{/owner}{/repo}', + subscriptions_url: 'https://gitee.com/api/v5/users/***/subscriptions', + organizations_url: 'https://gitee.com/api/v5/users/***/orgs', + repos_url: 'https://gitee.com/api/v5/users/***/repos', + events_url: 'https://gitee.com/api/v5/users/***/events{/privacy}', + received_events_url: 'https://gitee.com/api/v5/users/***/received_events', + type: 'User', + blog: null, + weibo: null, + bio: '', + public_repos: 0, + public_gists: 0, + followers: 0, + following: 0, + stared: 0, + watched: 0, + created_at: '2020-02-26T13:20:27+08:00', + updated_at: '2024-01-10T22:27:59+08:00', + email: null + }); + }); +}); \ No newline at end of file diff --git a/src/__tests__/helper/helper.ts b/src/__tests__/helper/helper.ts index 5d1859c..082b05e 100644 --- a/src/__tests__/helper/helper.ts +++ b/src/__tests__/helper/helper.ts @@ -6,6 +6,40 @@ import dayjs from 'dayjs'; import utc from 'dayjs/plugin/utc'; import timezone from 'dayjs/plugin/timezone'; +// const mock = new MockAdapter(axios); + +// mock.onGet('https://fake-gitee/api/v5/user').reply(200, { +// "id": 1000001, +// "login": "***", +// "name": "***", +// "avatar_url": "https://foruda.gitee.com/avatar/***/***.png", +// "url": "https://gitee.com/api/v5/users/***", +// "html_url": "https://gitee.com/***", +// "remark": "", +// "followers_url": "https://gitee.com/api/v5/users/***/followers", +// "following_url": "https://gitee.com/api/v5/users/***/following_url{/other_user}", +// "gists_url": "https://gitee.com/api/v5/users/***/gists{/gist_id}", +// "starred_url": "https://gitee.com/api/v5/users/***/starred{/owner}{/repo}", +// "subscriptions_url": "https://gitee.com/api/v5/users/***/subscriptions", +// "organizations_url": "https://gitee.com/api/v5/users/***/orgs", +// "repos_url": "https://gitee.com/api/v5/users/***/repos", +// "events_url": "https://gitee.com/api/v5/users/***/events{/privacy}", +// "received_events_url": "https://gitee.com/api/v5/users/***/received_events", +// "type": "User", +// "blog": null, +// "weibo": null, +// "bio": "", +// "public_repos": 0, +// "public_gists": 0, +// "followers": 0, +// "following": 0, +// "stared": 0, +// "watched": 0, +// "created_at": "2020-02-26T13:20:27+08:00", +// "updated_at": "2024-01-10T22:27:59+08:00", +// "email": null +// }); + dayjs.extend(utc); dayjs.extend(timezone); @@ -23,10 +57,14 @@ export const TEST_ENCRYPT_KEY = process.env.TEST_ENCRYPT_KEY as string; export const giteeRequest = createRequest({ httpLib: 'axios', httpClient: axios, + + baseURL: undefined, accessToken: process.env.TEST_GITEE_TOKEN as string, + platform: 'gitee', owner: process.env.TEST_GITEE_OWNER as string, repo: process.env.TEST_GITEE_REPO as string, + useEncrypt: true, encryptFn: (data: string) => { return CryptoJS.AES.encrypt(data, TEST_ENCRYPT_KEY).toString(); diff --git a/src/__tests__/wx-request.ts b/src/__tests__/wx-request.test.ts similarity index 100% rename from src/__tests__/wx-request.ts rename to src/__tests__/wx-request.test.ts diff --git a/src/repository-lib/base/base-repository.ts b/src/repository-lib/base/base-repository.ts index 7eae05e..49b5b5a 100644 --- a/src/repository-lib/base/base-repository.ts +++ b/src/repository-lib/base/base-repository.ts @@ -133,12 +133,7 @@ export abstract class BaseRepository { async deleteAll(): Promise { const findUrl = this.getRoute(RouteType.find); const findResult = await this.request.get(findUrl); - console.log(findResult); - for (const item of findResult) { - const deleteResult = await this.deleteById(item.id); - console.log(deleteResult); - } - // await Promise.all(findResult.map((item) => this.deleteById(item.id))); + await Promise.all(findResult.map((item) => this.deleteById(item.id))); } // 序列化: 将对象转换为字符串