我第一次尝试开玩笑,而当我运行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);
});
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"]
}