You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Jest模拟jose.jwtVerify?多方案尝试均报错

模拟jose.jwtVerify时遇到的Jest只读属性错误

我尝试使用Jest模拟jose.jwtVerify函数,但采用不同方案时遇到了以下错误:

  • Jest错误:property is not declared configurable
  • Cannot assign to 'type' because it is a read-only property

尚未找到模拟导出只读函数的可行方案,当前项目配置如下:

tsconfig 配置

{
    "extends": "@tsconfig/node20/tsconfig.json",
    "compilerOptions": {
        "declaration": true,
        "outDir": "./dist/",
        "noUnusedParameters": false,
        "noPropertyAccessFromIndexSignature": false,
        "resolveJsonModule": true
    },
}

Jest 配置

module.exports = {
    preset: 'ts-jest',
    testEnvironment: 'node',
    testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.(ts?)$',
    moduleNameMapper: {
        '^(\\.{1,2}/.*)\\.js$': '$1',
    },
    extensionsToTreatAsEsm: ['.ts'],
    transform: {
        '^.+\\.tsx?$': [
            'ts-jest',
            {
                useESM: true,
            },
        ],
    },

};

package.json 配置

{
  "name": "test",
  "version": "0.0.1",
  "description": "test",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "build": "npm run clean && npm run copy-files && npm run tsc",
    "copy-files": "copyfiles -e \"!{*.md,package?(-lock).json}\" \"**/*.*\" dist",
    "clean": "rimraf ./dist",
    "tsc": "tsc -p tsconfig.build.json",
    "deploy": "cdktf deploy",
    "lint": "npx eslint . --ext .ts,.tsx",
    "test": "node --experimental-vm-modules ./node_modules/.bin/jest --maxWorkers=2 -c jest.config.cjs",
    "coverage": "jest -c jest-cov.config.cjs"
  },
  "author": "test",
  "license": "test",
  "devDependencies": {
    "@cdktf/provider-aws": "18.0.6",
    "@jest/globals": "29.7.0",
    "@tsconfig/node20": "20.1.2",
    "@types/aws-lambda": "8.10.130",
    "@types/jest": "29.5.11",
    "@types/lodash": "^4.14.188",
    "@types/node": "20.10.5",
    "@typescript-eslint/eslint-plugin": "6.16.0",
    "@typescript-eslint/parser": "6.16.0",
    "cdktf": "0.19.1",
    "constructs": "10.3.0",
    "copyfiles": "2.4.1",
    "eslint": "8.38.0",
    "eslint-config-airbnb-base": "15.0.0",
    "eslint-config-airbnb-typescript": "17.1.0",
    "eslint-plugin-import": "2.28.0",
    "eslint-plugin-jest": "27.2.3",
    "jest": "29.7.0",
    "jest-junit": "16.0.0",
    "lodash": "^4.17.21",
    "rimraf": "5.0.5",
    "ts-jest": "29.1.1",
    "ts-node": "10.9.2",
    "ts-sinon": "^2.0.2",
    "typescript": "5.3.3"
  },
  "dependencies": {
    "@aws-sdk/client-dynamodb": "3.468.0",
    "@types/rewire": "^2.5.30",
    "aws-jwt-verify": "4.0.0",
    "aws-sdk": "2.1422.0",
    "axios": "1.6.2",
    "dotenv": "16.3.1",
    "jose": "5.2.0",
    "nanoid": "3.3.6",
    "rewire": "^7.0.0",
    "sinon": "^17.0.1"
  }
}

已尝试的模拟方案

// 方案1
jest.spyOn(jose, 'jwtVerify', 'get').mockReturnValue('ok')

// 方案2
Object.defineProperty(jose, 'jwtVerify', {
    get: jest.fn(() => 'bar'),
});

// 方案3
jest.replaceProperty(jose, 'jwtVerify', 'ok');

// 方案4
jest.mock('jose', () => {
    return {
        jwtVerify: () => 'ok',
    };
});

内容的提问来源于stack exchange,提问作者nahum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 03:24:55