dimanche 10 février 2019

Jest showing ` Object.defineProperty called on non-object` error

I was trying out jest for the first time, and just when I ran npm test I got this error saying , TypeError: Object.defineProperty called on non-object at Function.defineProperty (<anonymous>)

q.js - /src/q.js

function linterFunction(doc) {
const metas = doc.querySelectorAll("meta[charset]");
const val = [];
for (const meta of metas) {
   val.push(
   meta
    .getAttribute("charset")
    .trim()
    .toLowerCase()
   );
 }
 const utfExists = val.includes("utf-8");

 if (utfExists && metas.length === 1) {
   return [];
 }
 return {
   occurrences: metas.length,
 };
}

export const rule = linterFunction;

q.spec.js - TEST FILE - /tests/q.spec.js

import { rule } from '../src/q.js';

test("testing doc", () => {
   const doc = document.implementation.createHTMLDocument("test doc");
   doc.head.innerHTML = `
     <meta charset="utf-8">
     <meta name="viewport" content="width=device-width">
   `;

   const results = rule(doc);
   expect(results.length).toBe(0);
});

ss of the error

Aucun commentaire:

Enregistrer un commentaire