TypeError:使用玩笑时在Function.defineProperty(<anonymous>)的非对象上调用Object.defineProperty

时间:2019-02-10 14:25:48

标签: javascript testing jestjs

测试代码:

import { rule } from  '../../../../src/core/linter-rules/check-charset.js';

describe("Core Linter Rule - 'check-charset'", () => {
  const ruleName = "check-charset";
  const config = {
    lint: { [ruleName]: true },
  };

  test("checks if meta[charset] is set to utf-8", async () => {
    const doc = document.implementation.createHTMLDocument("test doc");
    doc.head.innerHTML = `
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width">
      <title>ReSpec</title>
    `;

    const results = await rule.lint(config, doc);
    expect(results.length).toBe(0);
  });

  test("doesn't give an error when written in capitals", async () => {
    const doc = document.implementation.createHTMLDocument("test doc");
    doc.head.innerHTML = `
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width">
      <title>ReSpec</title>
    `;

    const results = await rule.lint(config, doc);
    expect(results.length).toBe(0);
  });

  test("checks if meta[charset] is present or not", async () => {
    const doc = document.implementation.createHTMLDocument("test doc");
    doc.head.innerHTML = `
      <meta name="viewport" content="width=device-width">
      <title>ReSpec</title>
    `;

    const results = await rule.lint(config, doc);
    expect(results.occurrences).toBe(0);
  });

  test("returns error when more then one meta[charset] present", async () => {
    const doc = document.implementation.createHTMLDocument("test doc");
    doc.head.innerHTML = `
      <meta charset="utf-8">
      <meta charset="ascii-128">
      <meta name="viewport" content="width=device-width">
      <title>ReSpec</title>
    `;

    const results = await rule.lint(config, doc);
    expect(results.occurrences).toBe(2);
  });

  test("return error when some other charset defined", async () => {
    const doc = document.implementation.createHTMLDocument("test doc");
    doc.head.innerHTML = `
      <meta charset="ascii-128">
      <meta name="viewport" content="width=device-width">
      <title>ReSpec</title>
    `;

    const results = await rule.lint(config, doc);
    expect(results.occurrences).toBe(1);
  });
});

check-charset.js

/*
 * Linter Rule "check-charset".
 *
 * Checks whether the document has `<meta charset="utf-8">` properly.
 */
import LinterRule from "../LinterRule";
import { lang as defaultLang } from "../l10n";

const name = "check-charset";
const meta = {
  en: {
    description: `Document must only contain one \`<meta>\` tag with charset set to 'utf-8'`,
    howToFix: `Add this line in your document \`<head>\` section - \`<meta charset="utf-8">\` or set charset to "utf-8" if not set already.`,
  },
};

// Fall back to english, if language is missing
const lang = defaultLang in meta ? defaultLang : "en";

/*
 * Runs Linter Rule.
 *
 * @param {Object} conf The ReSpec config.
 * @param {Document} doc The document to be checked.
 */
function linterFunction(conf, 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");

  // only a single meta[charset] and is set to utf-8, correct case
  if (utfExists && metas.length === 1) {
    return [];
  }
  // if more than one meta[charset] tag defined along with utf-8
  // or
  // no meta[charset] present in the document
  return {
    name,
    occurrences: metas.length,
    ...meta[lang],
  };
}
export const rule = new LinterRule(name, linterFunction);

我正在尝试将jest集成到我的node.js项目中,最初的测试是用jasmine编写的,我使用jest-codemods将茉莉花测试转换成笑话!

但是,当我运行测试npm test时,我得到了这些奇怪的错误。

我收到此错误消息: TypeError: Object.defineProperty called on non-object at Function.defineProperty (<anonymous>)

Screenshot of the error

这到底是什么意思?我在网上搜索了很多内容,在任何地方都找不到令人满意的解释!

此外,奇怪的是为什么在显示注释的代码部分向我显示错误!

0 个答案:

没有答案