-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
70 lines (60 loc) · 2.97 KB
/
test.html
File metadata and controls
70 lines (60 loc) · 2.97 KB
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
<title>Tests</title>
</head>
<body>
<div id="mocha"></div>
<script>
function loadScript(src, type = "text/javascript") {
return new Promise((resolve, reject) => {
const script = document.createElement("script")
script.src = src
script.type = type
script.onload = () => {
console.log(`Loaded: ${src}`)
resolve()
}
script.onerror = () => reject(new Error(`Failed to load: ${src}`))
document.body.appendChild(script)
})
}
window.addEventListener('DOMContentLoaded', async () => {
try {
const response = await fetch('index.html')
const html = await response.text()
const tempHtmlDoc = new DOMParser().parseFromString(html, 'text/html')
const headContent = tempHtmlDoc.head
const bodyContent = tempHtmlDoc.body
const bodyHeader = bodyContent.querySelector('header')
const bodyMain = bodyContent.querySelector('main')
// Array.from(headContent.children).forEach(child => document.head.append(child))
// document.head.innerHTML += headContent.innerHTML
document.body.append(bodyHeader)
document.body.append(bodyMain)
await loadScript('https://cdnjs.cloudflare.com/ajax/libs/mocha/10.2.0/mocha.min.js')
await loadScript('https://cdnjs.cloudflare.com/ajax/libs/chai/4.3.6/chai.min.js')
await loadScript('https://cdnjs.cloudflare.com/ajax/libs/sinon.js/15.0.1/sinon.min.js')
// need old sinon-chai version to load CDN, yet doesn't work with newer versions of chai
// await loadScript('https://cdn.jsdelivr.net/npm/sinon-chai@1.3.1/lib/sinon-chai.min.js')
const mochaSetupScript = document.createElement('script')
mochaSetupScript.className = 'mocha-init'
// if can get sinon-chai to work
// chai.use(sinonChai);
mochaSetupScript.innerHTML = `
mocha.setup("bdd");
window.expect = chai.expect;
`
document.body.appendChild(mochaSetupScript)
await loadScript("/test/spec.js", "module")
mocha.run()
} catch (error) {
console.warn('Failed to load app.html:', error)
}
})
</script>
</body>
</html>