Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1.5 KB

use-with-decimal-js.md

File metadata and controls

38 lines (29 loc) · 1.5 KB

Use with decimal.js

You won't be able to use prismock with decimal.js with the default configuration.

Because prismock makes use of structuredClone which decimal.js doesn't support, you will encounter a DataCloneError, as pointed in #937.

@badeleux provide a workaround in his issue, which I recommend you setup in a setupFilesAfterEnv.

// Backup the original structuredClone function
const originalStructuredClone = structuredClone;

// Custom structuredClone that handles Decimal types
function customStructuredClone(input) {
    const replacer = (key, value) => {
        if (value instanceof Decimal) {
            // Convert Decimal to a serializable form
            return {type: 'Decimal', value: value.toString()};
        }
        return value;
    };

    const reviver = (key, value) => {
        if (value && value.type === 'Decimal') {
            // Convert back to Decimal
            return new Decimal(value.value);
        }
        return value;
    };

    // Use JSON stringify and parse as an example of handling custom types
    return originalStructuredClone(JSON.parse(JSON.stringify(input, replacer), reviver));
}

// Override global structuredClone with the custom function
global.structuredClone = customStructuredClone;