{"record":{"id":"14b70ca3c633b32b","repo":"karatelabs/karate","slug":"toisostring-is-not-a-function","errorCode":null,"errorMessage":"toISOString is not a function","messagePattern":"toISOString is not a function","errorType":"exception","errorClass":"JsErrorException","httpStatus":null,"severity":"error","filePath":"karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java","lineNumber":245,"sourceCode":"        // Invoke(O, \"toISOString\") path doing a redundant getMember dispatch.\n        if (o instanceof JsDate d) {\n            return formatIso(d);\n        }\n        // Invoke O.toISOString()\n        if (o instanceof ObjectLike ol) {\n            Object iso = ol.getMember(\"toISOString\");\n            if (iso instanceof JsCallable jc) {\n                CoreContext sub = (cc == null) ? null : new CoreContext(cc, null, null);\n                if (sub != null) sub.thisObject = o;\n                Object r = jc.call(sub == null ? context : sub, new Object[0]);\n                if (sub != null && sub.isError() && cc != null) {\n                    cc.updateFrom(sub);\n                    return null;\n                }\n                return r;\n            }\n        }\n        throw JsErrorException.typeError(\"toISOString is not a function\");\n    }\n\n    private Object getFullYear(Context context, Object[] args) {\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) return Double.NaN;\n        return localZdt(d).getYear();\n    }\n\n    private Object getYear(Context context, Object[] args) {\n        // Annex B: getYear returns getFullYear() - 1900\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) return Double.NaN;\n        return localZdt(d).getYear() - 1900;\n    }\n\n    private Object getMonth(Context context, Object[] args) {\n        JsDate d = requireDate(context);\n        if (d.isInvalid()) return Double.NaN;","sourceCodeStart":227,"sourceCodeEnd":263,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-js/src/main/java/io/karatelabs/js/JsDatePrototype.java#L227-L263","documentation":"Date.prototype.toJSON finishes by invoking the object's `toISOString` property. If the result of ToPrimitive is an object whose `toISOString` is missing or not callable, Karate throws this TypeError per spec step 4.b/c. It means the receiver does not actually behave like a Date.","triggerScenarios":"Date.prototype.toJSON.call({ toISOString: 42 }), .call({}) (after numeric coercion yields an object), or a plain object inheriting Date.prototype.toJSON without defining a callable toISOString.","commonSituations":"Objects created via Object.create(Date.prototype) or mixin patterns, mocked Date-like objects in tests lacking toISOString, and serialization code that assumes any date-ish object has the method.","solutions":["Ensure the object exposes a callable toISOString, or use a real Date instance.","Add toISOString to your mock/stub if you deliberately use Date-like objects in tests.","Check typeof obj.toISOString === 'function' before serializing."],"exampleFix":"// before\nconst fake = Object.create(Date.prototype);\nJSON.stringify({ d: fake }); // TypeError: toISOString is not a function\n// after\nconst fake = Object.create(Date.prototype);\nfake.toISOString = () => new Date().toISOString();","handlingStrategy":"type-guard","validationCode":"if (typeof (obj && obj.toISOString) !== 'function') throw new Error('receiver has no callable toISOString');","typeGuard":"function hasToISOString(v) { return v !== null && typeof v === 'object' && typeof v.toISOString === 'function'; }","tryCatchPattern":"try { return JSON.stringify(obj); } catch (e) { if (e instanceof TypeError && /toISOString is not a function/.test(e.message)) return JSON.stringify(fallback(obj)); throw e; }","preventionTips":["Use real Date instances instead of Date-like mocks when serializing.","Ensure test mocks implement the full Date surface they are used for.","Check typeof obj.toISOString === 'function' before generic serialization."],"tags":["javascript","date","type-error","mocking"],"backgroundTag":"type-mismatch","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}