-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
106 lines (78 loc) · 2.4 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
"use strict";
const {after, before, it} = require("mocha");
const puppeteer = require("puppeteer");
const puppeteerCoverage = require("puppeteer-to-istanbul");
const runInBrowser = func => () => page.evaluate(func);
let browser, page;
// @todo also use npmjs.com/puppeteer-firefox
before(async () =>
{
browser = await puppeteer.launch({ args: ["--no-sandbox"] });
page = await browser.newPage();
page.on("console", async msg => console[msg._type](...await Promise.all(msg.args().map(arg => arg.jsonValue()))));
page.on("pageerror", console.error);
await Promise.all(
[
page.addScriptTag({ path: "node_modules/chai/chai.js" }),
page.addScriptTag({ path: "temp.js" }),
// @todo https://github.com/istanbuljs/puppeteer-to-istanbul/issues/18
// @todo https://github.com/GoogleChrome/puppeteer/issues/3570
page.coverage.startJSCoverage({ reportAnonymousScripts: true })
]);
await page.evaluate(() =>
{
const iframe = document.createElement("iframe");
document.body.append(iframe);
window.anotherRealm = iframe.contentWindow;
window.expect = chai.expect;
delete window.chai; // cleanup
});
});
after(async () =>
{
let coverage = await page.coverage.stopJSCoverage();
// Exclude tools
coverage = coverage.filter(({url}) => !url.includes("chai"));
puppeteerCoverage.write(coverage);
browser.close();
});
it("is a (bundled) function", runInBrowser(() =>
{
expect(window.isDOMDocument).to.be.a("function");
}));
it("returns true for a Document", runInBrowser(() =>
{
const docs =
[
document,
document.implementation.createDocument("namespaceURI", "qualifiedNameStr"),
document.implementation.createHTMLDocument("title"),
anotherRealm.document
];
docs.forEach(doc => expect(isDOMDocument(doc)).to.be.true);
}));
it("returns false for a non-Document", runInBrowser(() =>
{
const fixtures =
[
"Document",
Symbol("Document"),
{},
[],
/regex/,
true,
1,
null,
undefined,
document.createComment("data"),
document.createDocumentFragment(),
document.createElement("tagName"),
document.createProcessingInstruction("target", "data"),
document.createTextNode("data"),
document.implementation.createDocument("namespaceURI", "qualifiedNameStr").createCDATASection("data"),
document.implementation.createDocumentType("qualifiedNameStr", "publicId", "systemId"),
window,
anotherRealm
];
fixtures.forEach(fixture => expect(isDOMDocument(fixture)).to.be.false);
}));