Search code examples
xml-parsingjestjsxmlserializer

XMLSerializer is undefined in jest test


I was using XMLSerializer class in one of NODE package class method. When writing unit test case using jest it threw error of XMLSerializer is not undefined. As this is native to browser, there can not be any package.

Even I searched online, found few suggestions of using a new jsdom and creating a prototype of XMLSerializer.

No proper solution is found even no posts at SO.


Solution

  • I've currently used a wrapped for XMLSerializer as below (typescript code):

    export default class XmlSerializerWrapper {
        private serializer: XMLSerializer;
        constructor() {
            this.serializer = new XMLSerializer();
        }
        public serializeToString(doc: Document) {
            return this.serializer.serializeToString(doc);
        }
    }
    

    It works fine.