开玩笑显示`Object.defineProperty在非对象错误上调用

时间:2019-02-11 05:00:08

标签: javascript testing jestjs

我第一次尝试开玩笑,而当我运行npm test时,我得到了这个错误,说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-测试文件-/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

package.json

"jest": {
  "testRegex": "(/tests/.*|(\\.|/)(test|spec))\\.jsx?$",
  "testURL": "http://localhost/"
},

.babelrc

{
  "presets": [
  [
    "@babel/preset-env",
    {
      "targets": "last 1 Firefox version, last 1 Chrome version, last 1 
      Safari version, last 1 Edge version",
      "modules": "amd"
    }
  ]
 ],
 "plugins": ["transform-amd-to-commonjs"]
}

0 个答案:

没有答案