Код тестирования:
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>)
Скриншот ошибки
Что именно это означает?Я много искал в сети, нигде не мог найти удовлетворительного объяснения!
Плюс странно, почему он показывает ошибку в части кода, в которой есть комментарии!