From f5e0826d761c87d150403a43bb5edbbecc351ea6 Mon Sep 17 00:00:00 2001 From: JOU Amjs Date: Mon, 8 May 2023 23:02:24 +0800 Subject: [PATCH] feat(ssr): support ssr --- .eslintrc.js => .eslintrc.cjs | 0 .github/workflows/release.yml | 16 +- .prettierrc.js => .prettierrc.cjs | 0 .releaserc.yml | 14 +- babel.config.js | 12 - commitlint.config.js => commitlint.config.cjs | 0 config/{rollup.js => rollup.cjs} | 9 +- .../{rollup.config.js => rollup.config.cjs} | 2 +- ...up.config.esm.js => rollup.config.esm.cjs} | 2 +- ...up.config.umd.js => rollup.config.umd.cjs} | 2 +- jest.config.node.ts | 209 ++++++++++++++++++ jest.config.ts | 4 +- package-lock.json | 89 +++++++- package.json | 14 +- test/mockResponse.spec.ts | 2 +- test/requestAdapter.spec.ts | 6 +- test/server.ts | 10 +- test/setup.ts | 1 + test/test-env.d.ts | 1 + tsconfig.json | 3 +- 20 files changed, 344 insertions(+), 52 deletions(-) rename .eslintrc.js => .eslintrc.cjs (100%) rename .prettierrc.js => .prettierrc.cjs (100%) delete mode 100644 babel.config.js rename commitlint.config.js => commitlint.config.cjs (100%) rename config/{rollup.js => rollup.cjs} (81%) rename config/{rollup.config.js => rollup.config.cjs} (95%) rename config/{rollup.config.esm.js => rollup.config.esm.cjs} (94%) rename config/{rollup.config.umd.js => rollup.config.umd.cjs} (97%) create mode 100644 jest.config.node.ts create mode 100644 test/setup.ts create mode 100644 test/test-env.d.ts diff --git a/.eslintrc.js b/.eslintrc.cjs similarity index 100% rename from .eslintrc.js rename to .eslintrc.cjs diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 8782ad6..bd119d4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -19,6 +19,8 @@ jobs: # 拉取代码 - name: Checkout uses: actions/checkout@v3 + with: + persist-credentials: false - name: Use Node.js uses: actions/setup-node@v3 with: @@ -35,16 +37,19 @@ jobs: # 这条命令包含了测试 - name: Unit tests + if: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/alpha' && github.ref != 'refs/heads/beta' }} run: npm run test release: runs-on: ubuntu-latest needs: [quality] - if: ${{ github.ref == 'refs/heads/main' }} + if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/alpha' || github.ref == 'refs/heads/beta' }} steps: # 拉取代码 - name: Checkout uses: actions/checkout@v3 + with: + persist-credentials: false - name: Use Node.js uses: actions/setup-node@v3 with: @@ -56,16 +61,15 @@ jobs: - name: Install deps run: npm ci + - name: Test SSR + run: npm run test:node + # 这条命令包含了测试和上传覆盖率 - name: Coverage run: npm run coveralls - # 打包发布 - - name: Build - run: npm run build --if-present - - name: Release run: npm run release env: NODE_AUTH_TOKEN: ${{ secrets.NPM_ALOVA_PUBLISH_TOKEN }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_TOKEN: ${{ secrets.ALOVA_GITHUB_TOKEN }} diff --git a/.prettierrc.js b/.prettierrc.cjs similarity index 100% rename from .prettierrc.js rename to .prettierrc.cjs diff --git a/.releaserc.yml b/.releaserc.yml index 16a68dc..86cb1fd 100644 --- a/.releaserc.yml +++ b/.releaserc.yml @@ -6,8 +6,12 @@ branches: prerelease: true plugins: - - "@semantic-release/commit-analyzer" - - "@semantic-release/release-notes-generator" - - "@semantic-release/npm" - - "@semantic-release/github" - - "@semantic-release/git" + - '@semantic-release/commit-analyzer' + - '@semantic-release/release-notes-generator' + - '@semantic-release/npm' + - '@semantic-release/github' + - '@semantic-release/git' + - - '@semantic-release/exec' + + # 使用下一个版本号执行打包命令 + - prepareCmd: 'cross-env VERSION=${nextRelease.version} npm run build' diff --git a/babel.config.js b/babel.config.js deleted file mode 100644 index 650f6ff..0000000 --- a/babel.config.js +++ /dev/null @@ -1,12 +0,0 @@ -module.exports = { - presets: [ - [ - "@babel/preset-env", - { - targets: { - node: "current" - } - } - ] - ] -}; \ No newline at end of file diff --git a/commitlint.config.js b/commitlint.config.cjs similarity index 100% rename from commitlint.config.js rename to commitlint.config.cjs diff --git a/config/rollup.js b/config/rollup.cjs similarity index 81% rename from config/rollup.js rename to config/rollup.cjs index 64e86d5..f5f9e2c 100644 --- a/config/rollup.js +++ b/config/rollup.cjs @@ -1,7 +1,7 @@ /* * @Date: 2020-04-09 11:06:01 * @LastEditors: JOU(wx: huzhen555) - * @LastEditTime: 2022-11-30 21:15:34 + * @LastEditTime: 2023-05-08 23:00:03 */ var typescript = require('rollup-plugin-typescript2'); var { readFileSync } = require('fs'); @@ -19,11 +19,12 @@ const getCompiler = ( ) => typescript(opt); exports.getCompiler = getCompiler; -const pkg = JSON.parse(readFileSync('package.json').toString() || '{}'); -const { version, author, homepage } = pkg; +const pkg = require('../package.json'); +const version = process.env.VERSION || pkg.version; +const author = pkg.author; const repository = pkg.repository.url.replace('git', 'https').replace('.git', ''); exports.banner = `/** - * ${pkg.name} ${version} (${homepage}) + * ${pkg.name} ${version} (${pkg.homepage}) * Copyright ${new Date().getFullYear()} ${author}. All Rights Reserved * Licensed under MIT (${repository}/blob/master/LICENSE) */ diff --git a/config/rollup.config.js b/config/rollup.config.cjs similarity index 95% rename from config/rollup.config.js rename to config/rollup.config.cjs index 61370d4..fffe89b 100644 --- a/config/rollup.config.js +++ b/config/rollup.config.cjs @@ -5,7 +5,7 @@ */ // rollup.config.js // commonjs -var config = require('./rollup.js'); +var config = require('./rollup.cjs'); var module = process.argv.pop().replace('--', '') || 'core'; var paths = config.compilePath; var moduleType = 'cjs'; diff --git a/config/rollup.config.esm.js b/config/rollup.config.esm.cjs similarity index 94% rename from config/rollup.config.esm.js rename to config/rollup.config.esm.cjs index 7bae207..da7501d 100644 --- a/config/rollup.config.esm.js +++ b/config/rollup.config.esm.cjs @@ -1,7 +1,7 @@ // rollup.config.js // ES output var { nodeResolve } = require('@rollup/plugin-node-resolve'); -var config = require('./rollup.js'); +var config = require('./rollup.cjs'); var paths = config.compilePath; var moduleType = 'esm'; diff --git a/config/rollup.config.umd.js b/config/rollup.config.umd.cjs similarity index 97% rename from config/rollup.config.umd.js rename to config/rollup.config.umd.cjs index 203f844..6d12630 100644 --- a/config/rollup.config.umd.js +++ b/config/rollup.config.umd.cjs @@ -10,7 +10,7 @@ var commonjs = require('@rollup/plugin-commonjs'); var { terser } = require('rollup-plugin-terser'); var replace = require('@rollup/plugin-replace'); var json = require('@rollup/plugin-json'); -var config = require('./rollup.js'); +var config = require('./rollup.cjs'); var prod = process.env.NODE_ENV === 'production'; var paths = config.compilePath; var moduleType = prod ? 'umd.min' : 'umd'; diff --git a/jest.config.node.ts b/jest.config.node.ts new file mode 100644 index 0000000..40502f4 --- /dev/null +++ b/jest.config.node.ts @@ -0,0 +1,209 @@ +/* + * For a detailed explanation regarding each configuration property and type check, visit: + * https://jestjs.io/docs/configuration + */ + +export default { + // All imported modules in your tests should be mocked automatically + // automock: false, + + // Stop running tests after `n` failures + // bail: 0, + + // The directory where Jest should store its cached dependency information + // cacheDirectory: "C:\\Users\\Administrator\\AppData\\Local\\Temp\\jest", + + // Automatically clear mock calls, instances, contexts and results before every test + // clearMocks: false, + + // Indicates whether the coverage information should be collected while executing the test + collectCoverage: false, + + // An array of glob patterns indicating a set of files for which coverage information should be collected + // collectCoverageFrom: undefined, + + // The directory where Jest should output its coverage files + coverageDirectory: 'coverage', + + // An array of regexp pattern strings used to skip coverage collection + coveragePathIgnorePatterns: ['\\\\node_modules\\\\', '/node_modules/', 'test/utils.ts'], + + // Indicates which provider should be used to instrument code for coverage + coverageProvider: 'v8', + + // A list of reporter names that Jest uses when writing coverage reports + coverageReporters: ['json', 'text', 'lcov', 'clover'], + + // An object that configures minimum threshold enforcement for coverage results + // coverageThreshold: undefined, + + // A path to a custom dependency extractor + // dependencyExtractor: undefined, + + // Make calling deprecated APIs throw helpful error messages + // errorOnDeprecated: false, + + // The default configuration for fake timers + // fakeTimers: { + // "enableGlobally": false + // }, + + // Force coverage collection from ignored files using an array of glob patterns + // forceCoverageMatch: [], + + // A path to a module which exports an async function that is triggered once before all test suites + // globalSetup: undefined, + + // A path to a module which exports an async function that is triggered once after all test suites + // globalTeardown: undefined, + + // A set of global variables that need to be available in all test environments + // globals: {}, + + // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. + // maxWorkers: "50%", + + // An array of directory names to be searched recursively up from the requiring module's location + // moduleDirectories: ['/node_modules', 'node_modules'], + + // An array of file extensions your modules use + moduleFileExtensions: [ + 'js', + // "mjs", + // "cjs", + 'jsx', + 'ts', + 'tsx', + 'json', + 'node', + 'svelte' + ], + + // A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module + moduleNameMapper: { + // must use absolute paths + }, + + // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader + // modulePathIgnorePatterns: [], + + // Activates notifications for test results + // notify: false, + + // An enum that specifies notification mode. Requires { notify: true } + // notifyMode: "failure-change", + + // A preset that is used as a base for Jest's configuration + // preset: 'ts-jest/presets/js-with-ts', + + // Run tests from one or more projects + // projects: undefined, + + // Use this configuration option to add custom reporters to Jest + // reporters: undefined, + + // Automatically reset mock state before every test + // resetMocks: false, + + // Reset the module registry before running each individual test + // resetModules: false, + + // A path to a custom resolver + // resolver: undefined, + + // Automatically restore mock state and implementation before every test + // restoreMocks: false, + + // The root directory that Jest should scan for tests and modules within + // rootDir: undefined, + + // A list of paths to directories that Jest should use to search for files in + // roots: [ + // "" + // ], + + // Allows you to use a custom runner instead of Jest's default test runner + // runner: "jest-runner", + + // The paths to modules that run some code to configure or set up the testing environment before each test + // setupFiles: [], + + // A list of paths to modules that run some code to configure or set up the testing framework before each test + setupFilesAfterEnv: ['/test/setup.ts'], + + // The number of seconds after which a test is considered as slow and reported as such in the results. + // slowTestThreshold: 5, + + // A list of paths to snapshot serializer modules Jest should use for snapshot testing + // snapshotSerializers: [], + + // The test environment that will be used for testing + // testEnvironment: 'node', + + testMatch: [ + '**/?(*.)+(spec|test).[tj]s?(x)' + // '**/test/requestAdapter.spec.ts(x)?' + // '**/test/mockResponse.spec.ts(x)?' + ], + + // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped + testPathIgnorePatterns: [ + '\\\\node_modules\\\\', // windows + '/node_modules/' // mac + ], + + // The regexp pattern or array of patterns that Jest uses to detect test files + // testRegex: [], + + // This option allows the use of a custom results processor + // testResultsProcessor: undefined, + + // This option allows use of a custom test runner + // testRunner: "jest-circus/runner", + + // A map from regular expressions to paths to transformers + transform: { + '\\.(j|t)sx?$': [ + 'ts-jest', + // mock import.meta, see https://www.npmjs.com/package/ts-jest-mock-import-meta + { + diagnostics: { + ignoreCodes: [1343] + }, + astTransformers: { + // 转换import.meta + before: [ + { + path: 'node_modules/ts-jest-mock-import-meta', + options: { + metaObjectReplacement: { + url: 'https://xxx', + env: { + PROD: false, + DEV: true + }, + status: 2 + } + } + } + ] + } + } + ] + }, + + // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation + transformIgnorePatterns: ['/node_modules/(?!alova)'] + + // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them + // unmockedModulePathPatterns: undefined, + + // Indicates whether each individual test should be reported during the run + // verbose: undefined, + + // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode + // watchPathIgnorePatterns: [], + + // Whether to use watchman for file crawling + // watchman: true, +}; diff --git a/jest.config.ts b/jest.config.ts index 2abc5bb..5559abf 100644 --- a/jest.config.ts +++ b/jest.config.ts @@ -126,10 +126,10 @@ export default { // runner: "jest-runner", // The paths to modules that run some code to configure or set up the testing environment before each test - // setupFiles: ['/test/server.ts'], + // setupFiles: [], // A list of paths to modules that run some code to configure or set up the testing framework before each test - // setupFilesAfterEnv: [], + setupFilesAfterEnv: ['/test/setup.ts'], // The number of seconds after which a test is considered as slow and reported as such in the results. // slowTestThreshold: 5, diff --git a/package-lock.json b/package-lock.json index f067875..cb997ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17,12 +17,13 @@ "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.3.0", "@rollup/plugin-replace": "^4.0.0", + "@semantic-release/exec": "^6.0.3", "@semantic-release/git": "^10.0.1", "@testing-library/jest-dom": "^5.16.4", "@types/jest": "^29.4.0", "@typescript-eslint/eslint-plugin": "^5.54.0", "@typescript-eslint/parser": "^5.54.0", - "alova": "^2.0.4", + "alova": "^2.3.1", "axios": "^1.3.4", "babel-jest": "^29.2.2", "commitizen": "^4.3.0", @@ -3594,6 +3595,48 @@ "node": ">=14.17" } }, + "node_modules/@semantic-release/exec": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/@semantic-release/exec/-/exec-6.0.3.tgz", + "integrity": "sha512-bxAq8vLOw76aV89vxxICecEa8jfaWwYITw6X74zzlO0mc/Bgieqx9kBRz9z96pHectiTAtsCwsQcUyLYWnp3VQ==", + "dev": true, + "dependencies": { + "@semantic-release/error": "^3.0.0", + "aggregate-error": "^3.0.0", + "debug": "^4.0.0", + "execa": "^5.0.0", + "lodash": "^4.17.4", + "parse-json": "^5.0.0" + }, + "engines": { + "node": ">=14.17" + }, + "peerDependencies": { + "semantic-release": ">=18.0.0" + } + }, + "node_modules/@semantic-release/exec/node_modules/aggregate-error": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", + "integrity": "sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==", + "dev": true, + "dependencies": { + "clean-stack": "^2.0.0", + "indent-string": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/@semantic-release/exec/node_modules/clean-stack": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", + "integrity": "sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==", + "dev": true, + "engines": { + "node": ">=6" + } + }, "node_modules/@semantic-release/git": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@semantic-release/git/-/git-10.0.1.tgz", @@ -5042,9 +5085,9 @@ } }, "node_modules/alova": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/alova/-/alova-2.0.4.tgz", - "integrity": "sha512-RH2mUT4Xxt0u8KIi6lYxhi+Z/QwsZ1AvPgERnzsuT0yAi0BMhql8Awa7Dt9SB+dwJLRiTLd2FaoRHfJKgOKAkQ==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/alova/-/alova-2.3.1.tgz", + "integrity": "sha512-w0vFOoB5QbpYWwq003LRWVaLKhgo5cgf9d2u16fADajMH++k48F55zwlBSbvKa7MtF8Ap/5GGvLrEGCrx/G++w==", "dev": true, "engines": { "node": ">= 0.12.0" @@ -21836,6 +21879,38 @@ "integrity": "sha512-5hiM4Un+tpl4cKw3lV4UgzJj+SmfNIDCLLw0TepzQxz9ZGV5ixnqkzIVF+3tp0ZHgcMKE+VNGHJjEeyFG2dcSw==", "dev": true }, + "@semantic-release/exec": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/@semantic-release/exec/-/exec-6.0.3.tgz", + "integrity": "sha512-bxAq8vLOw76aV89vxxICecEa8jfaWwYITw6X74zzlO0mc/Bgieqx9kBRz9z96pHectiTAtsCwsQcUyLYWnp3VQ==", + "dev": true, + "requires": { + "@semantic-release/error": "^3.0.0", + "aggregate-error": "^3.0.0", + "debug": "^4.0.0", + "execa": "^5.0.0", + "lodash": "^4.17.4", + "parse-json": "^5.0.0" + }, + "dependencies": { + "aggregate-error": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", + "integrity": "sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==", + "dev": true, + "requires": { + "clean-stack": "^2.0.0", + "indent-string": "^4.0.0" + } + }, + "clean-stack": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/clean-stack/-/clean-stack-2.2.0.tgz", + "integrity": "sha512-4diC9HaTE+KRAMWhDhrGOECgWZxoevMc5TlkObMqNSsVU62PYzXZ/SMTjzyGAFF1YusgxGcSWTEXBhp0CPwQ1A==", + "dev": true + } + } + }, "@semantic-release/git": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@semantic-release/git/-/git-10.0.1.tgz", @@ -22970,9 +23045,9 @@ } }, "alova": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/alova/-/alova-2.0.4.tgz", - "integrity": "sha512-RH2mUT4Xxt0u8KIi6lYxhi+Z/QwsZ1AvPgERnzsuT0yAi0BMhql8Awa7Dt9SB+dwJLRiTLd2FaoRHfJKgOKAkQ==", + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/alova/-/alova-2.3.1.tgz", + "integrity": "sha512-w0vFOoB5QbpYWwq003LRWVaLKhgo5cgf9d2u16fADajMH++k48F55zwlBSbvKa7MtF8Ap/5GGvLrEGCrx/G++w==", "dev": true }, "ansi-escapes": { diff --git a/package.json b/package.json index e3dfd3a..0115699 100644 --- a/package.json +++ b/package.json @@ -7,6 +7,7 @@ "jsnext:main": "dist/alova-adapter-axios.esm.js", "module": "dist/alova-adapter-axios.esm.js", "types": "typings/index.d.ts", + "type": "module", "keywords": [ "hooks", "vuejs", @@ -22,13 +23,15 @@ ], "scripts": { "clean": "rimraf ./dist", - "test": "jest", + "test:browser": "jest", + "test:node": "jest --config=jest.config.node.ts", + "test": "npm run test:browser && npm run test:node", "test:coverage": "jest --coverage", "lint": "eslint --ext .ts,.js src/**", "lint:fix": "eslint --ext .ts,.js src/** --fix", - "build:esm": "cross-env NODE_ENV=development rollup -c ./config/rollup.config.esm.js", - "build:umd": "cross-env NODE_ENV=development rollup -c ./config/rollup.config.umd.js", - "build:umd.min": "cross-env NODE_ENV=production rollup -c ./config/rollup.config.umd.js", + "build:esm": "cross-env NODE_ENV=development rollup -c ./config/rollup.config.esm.cjs", + "build:umd": "cross-env NODE_ENV=development rollup -c ./config/rollup.config.umd.cjs", + "build:umd.min": "cross-env NODE_ENV=production rollup -c ./config/rollup.config.umd.cjs", "build": "npm run clean && npm run build:esm && npm run build:umd && npm run build:umd.min", "release": "semantic-release", "coveralls": "npm run test:coverage && coveralls < coverage/lcov.info", @@ -61,12 +64,13 @@ "@rollup/plugin-json": "^4.1.0", "@rollup/plugin-node-resolve": "^13.3.0", "@rollup/plugin-replace": "^4.0.0", + "@semantic-release/exec": "^6.0.3", "@semantic-release/git": "^10.0.1", "@testing-library/jest-dom": "^5.16.4", "@types/jest": "^29.4.0", "@typescript-eslint/eslint-plugin": "^5.54.0", "@typescript-eslint/parser": "^5.54.0", - "alova": "^2.0.4", + "alova": "^2.3.1", "axios": "^1.3.4", "babel-jest": "^29.2.2", "commitizen": "^4.3.0", diff --git a/test/mockResponse.spec.ts b/test/mockResponse.spec.ts index 27454f5..94ded8c 100644 --- a/test/mockResponse.spec.ts +++ b/test/mockResponse.spec.ts @@ -78,7 +78,7 @@ describe('mock response adapter', () => { } }); - test('uploadFile', async () => { + (isSSR ? xtest : test)('uploadFile', async () => { // 使用formData上传文件 const formData = new FormData(); formData.append('f1', 'f1'); diff --git a/test/requestAdapter.spec.ts b/test/requestAdapter.spec.ts index 437b965..9de8fe1 100644 --- a/test/requestAdapter.spec.ts +++ b/test/requestAdapter.spec.ts @@ -163,7 +163,7 @@ describe('request adapter', () => { } }); - const Get = alovaInst.Get('/unit-test'); + const Get = alovaInst.Get('/unit-test-delay'); const { loading, data, downloading, error, abort, onError } = useRequest(Get); expect(loading.value).toBeTruthy(); expect(data.value).toBeUndefined(); @@ -178,7 +178,7 @@ describe('request adapter', () => { expect(error.value?.message).toBe('canceled'); }); - test('should upload file and pass the right args', async () => { + (isSSR ? xtest : test)('should upload file and pass the right args', async () => { const alovaInst = createAlova({ baseURL: mockBaseURL, requestAdapter: axiosRequestAdapter(), @@ -213,7 +213,7 @@ describe('request adapter', () => { expect(error.value).toBeUndefined(); }); - test('should download file and pass the right args', async () => { + (isSSR ? xtest : test)('should download file and pass the right args', async () => { const alovaInst = createAlova({ baseURL: mockBaseURL, requestAdapter: axiosRequestAdapter(), diff --git a/test/server.ts b/test/server.ts index 4766adc..b0c32f6 100644 --- a/test/server.ts +++ b/test/server.ts @@ -36,16 +36,22 @@ const result = (code: number, req: any, res: any, ctx: any, hasBody = false, ext export const mockServer = setupServer( rest.get(mockBaseURL + '/unit-test', (req, res, ctx) => result(200, req, res, ctx)), + rest.get(mockBaseURL + '/unit-test-delay', async (req, res, ctx) => { + await new Promise(resolve => { + setTimeout(resolve, 10); + }); + return result(200, req, res, ctx); + }), rest.get(mockBaseURL + '/unit-test-404', (_, res, ctx) => { return res(ctx.status(404, 'api not found')); }), rest.get(mockBaseURL + '/unit-test-error', () => { - throw new Error('server error'); + throw new Error('Network Error'); }), rest.post(mockBaseURL + '/unit-test', (req, res, ctx) => { return result(200, req, res, ctx, true); }), - rest.get(mockBaseURL + '/unit-test-download', (req, res, ctx) => { + rest.get(mockBaseURL + '/unit-test-download', (_, res, ctx) => { // Read the image from the file system using the "fs" module. const imageBuffer = readFileSync(path.resolve(__dirname, './image.jpg')); return res( diff --git a/test/setup.ts b/test/setup.ts new file mode 100644 index 0000000..c41598b --- /dev/null +++ b/test/setup.ts @@ -0,0 +1 @@ +(global as any).isSSR = typeof window === 'undefined'; diff --git a/test/test-env.d.ts b/test/test-env.d.ts new file mode 100644 index 0000000..bca5b5e --- /dev/null +++ b/test/test-env.d.ts @@ -0,0 +1 @@ +declare let isSSR: boolean; diff --git a/tsconfig.json b/tsconfig.json index baa312c..8072d86 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,7 +7,6 @@ "noImplicitAny": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, - "charset": "utf8", "noEmitOnError": false, "noUnusedLocals": true, "noUnusedParameters": false, @@ -26,6 +25,6 @@ "paths": { "@/*": ["src/*"] }, - "include": ["src/**/*.ts", "typings/*.d.ts"], + "include": ["src/**/*.ts", "typings/*.d.ts", "test/**/*.ts"], "exclude": ["node_modules", "**.d.ts"] }