Skip to content

Commit 8aa6a4e

Browse files
committed
test: Replace phantomjs with nodejs and puppeteer
1 parent 701316c commit 8aa6a4e

File tree

10 files changed

+206
-39
lines changed

10 files changed

+206
-39
lines changed

test/amd.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>AsyncTaskQueue AMD Test Suite</title>
5+
</head>
6+
<body>
7+
<script src="../node_modules/@prantlf/requirejs/require.min.js"
8+
data-main="amd" ></script>
9+
</body>
10+
</html>

test/amd.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
require.config({
2+
paths: {
3+
tehanu: '../node_modules/tehanu/dist/index.min',
4+
tape: '../node_modules/tehanu-repo-tape/dist/index.min',
5+
teas: '../node_modules/tehanu-teas/dist/index.min',
6+
underscore: '../node_modules/underscore/underscore-umd-min',
7+
jquery: '../node_modules/jquery//dist/jquery.min',
8+
asynctaskqueue: '../asynctaskqueue-min'
9+
},
10+
deps: ['tehanu', 'teas', 'asynctaskqueue', 'tape'],
11+
callback: ({ factory, run }, { strictEqual }, AsyncTaskQueue, reporter) => {
12+
const test = factory('AsyncTaskQueue')
13+
14+
const { Task } = AsyncTaskQueue.prototype
15+
16+
test('constructor', function() {
17+
const worker = function () {}
18+
const task = new Task(worker)
19+
strictEqual(task.worker, worker, 'worker should be stored')
20+
})
21+
22+
test('promise', function() {
23+
const task = new Task(function () {})
24+
const promise = task.promise()
25+
strictEqual(promise.state(), 'pending', 'promise should be pending initially')
26+
task.deferred.resolve()
27+
strictEqual(promise.state(), 'resolved', 'earlier promise should be resolved')
28+
strictEqual(task.promise().state(), 'resolved', 'new promise should be resolved')
29+
})
30+
31+
run({ reporter })
32+
}
33+
})

test/esm.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>AsyncTaskQueue ESM Test Suite</title>
5+
</head>
6+
<body>
7+
<script src="esm.js" type="module"></script>
8+
</body>
9+
</html>

test/esm.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import test from '../node_modules/tehanu/dist/suite.min.mjs?name=AsyncTaskQueue'
2+
import '../node_modules/tehanu-repo-tape/dist/index.min.mjs'
3+
import { strictEqual } from '../node_modules/tehanu-teas/dist/index.min.mjs'
4+
import '../node_modules/underscore/underscore-umd-min.js'
5+
import '../node_modules/jquery//dist/jquery.min.js'
6+
import '../asynctaskqueue-min.js'
7+
8+
const { Task } = AsyncTaskQueue.prototype
9+
10+
test('constructor', function() {
11+
const worker = function () {}
12+
const task = new Task(worker)
13+
strictEqual(task.worker, worker, 'worker should be stored')
14+
})
15+
16+
test('promise', function() {
17+
const task = new Task(function () {})
18+
const promise = task.promise()
19+
strictEqual(promise.state(), 'pending', 'promise should be pending initially')
20+
task.deferred.resolve()
21+
strictEqual(promise.state(), 'resolved', 'earlier promise should be resolved')
22+
strictEqual(task.promise().state(), 'resolved', 'new promise should be resolved')
23+
})

test/iife.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>AsyncTaskQueue IIFE Test Suite</title>
5+
</head>
6+
<body>
7+
<script src="../node_modules/tehanu/dist/index.min.js"></script>
8+
<script src="../node_modules/tehanu-repo-tape/dist/index.min.js"></script>
9+
<script src="../node_modules/tehanu-teas/dist/index.min.js"></script>
10+
<script src="../node_modules/underscore/underscore-umd-min.js"></script>
11+
<script src="../node_modules/jquery/dist/jquery.min.js"></script>
12+
13+
<script src="../asynctaskqueue-min.js"></script>
14+
15+
<script src="iife.js"></script>
16+
</body>
17+
</html>

test/iife.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(() => {
2+
const test = tehanu('AsyncTaskQueue')
3+
const { strictEqual } = tehanuTeas
4+
5+
const { Task } = AsyncTaskQueue.prototype
6+
7+
test('constructor', function() {
8+
const worker = function () {}
9+
const task = new Task(worker)
10+
strictEqual(task.worker, worker, 'worker should be stored')
11+
})
12+
13+
test('promise', function() {
14+
const task = new Task(function () {})
15+
const promise = task.promise()
16+
strictEqual(promise.state(), 'pending', 'promise should be pending initially')
17+
task.deferred.resolve()
18+
strictEqual(promise.state(), 'resolved', 'earlier promise should be resolved')
19+
strictEqual(task.promise().state(), 'resolved', 'new promise should be resolved')
20+
})
21+
})()

test/index.html

Lines changed: 0 additions & 17 deletions
This file was deleted.

test/launch.mjs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import puppeteer from 'puppeteer'
2+
3+
let headless = true
4+
let disconnect
5+
let timeout = 1000
6+
let arg
7+
for (let i = 2, { argv } = process, { length } = argv; i < length; ++i) {
8+
arg = argv[i]
9+
switch (arg) {
10+
case '--no-headless':
11+
headless = false
12+
continue
13+
case '--disconnect':
14+
disconnect = true
15+
continue
16+
}
17+
if (arg.startsWith('--timeout=')) {
18+
timeout = +arg.substring(10)
19+
continue
20+
}
21+
break
22+
}
23+
if (!arg) throw new Error('missing URL')
24+
25+
const collect = page => {
26+
return new Promise((resolve, reject) => {
27+
let worked, failed
28+
page
29+
.on('console', message => {
30+
const text = message.text()
31+
console.log(text)
32+
worked = true
33+
if (text.startsWith('not ok'))
34+
failed = true
35+
else if (text.match(/^\d+\.\.\d+$/))
36+
if (failed) reject()
37+
else resolve()
38+
})
39+
.on('pageerror', ({ message }) => {
40+
console.error(message)
41+
failed = true
42+
})
43+
.on('requestfailed', request => {
44+
console.log(`${request.failure().errorText} ${request.url()}`)
45+
failed = true
46+
})
47+
})
48+
}
49+
50+
const wait = time => {
51+
let timeout
52+
const promise = new Promise((resolve, reject) => timeout = setTimeout(reject, time))
53+
promise.abort = () => clearTimeout(timeout)
54+
return promise
55+
}
56+
57+
let browser, waiting
58+
try {
59+
browser = await puppeteer.launch({ headless })
60+
const page = await browser.newPage()
61+
const collecting = collect(page)
62+
await page.goto(`file://${process.cwd()}/${arg}`)
63+
waiting = wait(timeout)
64+
await Promise.race([collecting, waiting])
65+
} catch (error) {
66+
if (error) console.error(error)
67+
process.exitCode = 1
68+
} finally {
69+
if (waiting) waiting.abort()
70+
if (browser)
71+
if (disconnect) browser.disconnect()
72+
else browser.close()
73+
}

test/node.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require('@prantlf/dom-lite/global')
2+
document.implementation = {
3+
createHTMLDocument() {
4+
return new Document()
5+
}
6+
}
7+
global.window = {
8+
document,
9+
setTimeout,
10+
location: {
11+
href: ''
12+
}
13+
}
14+
15+
global.tehanu = require('tehanu')
16+
global.tehanuTeas = require('tehanu-teas')
17+
18+
global.AsyncTaskQueue = require('..')
19+
20+
require('./iife')

test/task.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)