YMFE/yapi · error

context.caseId 不能被赋值

Error message

context.caseId 不能被赋值

What it means

context.caseId in the test-script sandbox is read-only; its setter exists only to throw. caseId is supplied by the runner via options and must not be overwritten by scripts.

Source

Thrown at common/postmanLib.js:274

    get href() {
      return urlObj.href;
    },
    set href(val) {
      throw new Error('context.href 不能被赋值');
    },
    get hostname() {
      return urlObj.hostname;
    },
    set hostname(val) {
      throw new Error('context.hostname 不能被赋值');
    },

    get caseId() {
      return options.caseId;
    },

    set caseId(val) {
      throw new Error('context.caseId 不能被赋值');
    },

    method: options.method,
    pathname: urlObj.pathname,
    query: query,
    requestHeader: options.headers || {},
    requestBody: options.data,
    promise: false,
    storage: await getStorage(taskId)
  };

  Object.assign(context, commonContext)

  context.utils = Object.freeze({
    _: _,
    CryptoJS: CryptoJS,
    jsrsasign: jsrsasign,
    base64: utils.base64,

View on GitHub (pinned to 59bade3a8a)

Solutions

  1. Remove the context.caseId assignment from the script
  2. Pass the intended caseId through the runner options, not by mutation
  3. Use context.caseId only for reading

Example fix

// before
context.caseId = 42;
// after
const id = context.caseId;
Defensive patterns

Strategy: type-guard

Validate before calling

if (typeof context.caseId === 'number' && context.caseId !== expectedCaseId) throw new Error('running wrong case');

Type guard

function isReadOnlyCtxProp(obj, key){ const d = Object.getOwnPropertyDescriptor(obj, key); return !!d && d.get && d.set && d.set.toString().includes('throw'); }

Try / catch

try {
  context.caseId = id;
} catch (e) {
  console.warn('caseId is set by the runner; cannot be overridden');
}

Prevention

When it happens

Trigger: A script assigns context.caseId = <value> during a case run.

Common situations: Scripts trying to fake or reuse another case's id for cross-case assertions; copy-pasted template code mutating context fields.

Related errors


AI-assisted analysis of YMFE/yapi@59bade3a8a (2026-08-29). Data as JSON: /api/errors/c03350272363c4c2. Report an issue: GitHub.