Skip to content

Commit

Permalink
make testAllOk test tool reusable
Browse files Browse the repository at this point in the history
  • Loading branch information
axelpale committed Jan 13, 2025
1 parent f7afb4b commit 21b3dfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 30 deletions.
3 changes: 2 additions & 1 deletion test/headless/components/Item/index.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export default function (test) {
const namespace = 'Item'
const methods = [
'at',
'boundaries',
Expand All @@ -16,6 +17,6 @@ export default function (test) {
let i, m
for (i = 0; i < methods.length; i += 1) {
m = methods[i]
test('Item:' + m, import.meta.dirname, m + '.html')
test(namespace + ':' + m, import.meta.dirname, m + '.html')
}
}
31 changes: 2 additions & 29 deletions test/headless/components/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,12 @@ import testFrameComponent from './FrameComponent/index.mjs'
import testItem from './Item/index.mjs'
import testSpace from './Space/index.mjs'
import testViewport from './Viewport/index.mjs'
import { getFileUrl } from '../utils.mjs'
import { makeTestAllOk } from '../utils.mjs'

export default function (test, browser) {
// Custom test runner to reduce boilerplate code.
// Opens the test page and evaluates the test results.
const testAllOk = async (unitName, dirname, filename) => {
test(unitName, async (t) => {
// Setup
const pageUrl = getFileUrl(dirname, filename)
const page = await browser.newPage()
await page.setViewport({ width: 1000, height: 500 })
await page.goto(pageUrl, { waitUntil: 'domcontentloaded' })

// Wait max 5 sec for an async test to finish.
await page.waitForFunction('window.test.finished === true', { timeout: 5000 })
// Collect test results.
const report = await page.evaluate(() => window.test.report())

// Check test results.
for (let i = 0; i < report.length; i += 1) {
t._assert(report[i].result, {
message: report[i].message,
operator: report[i].operator,
actual: report[i].actual,
expected: report[i].expected
})
}

// Exit
await page.close()
t.end()
})
}
const testAllOk = makeTestAllOk(test, browser)

testArc(testAllOk)
testComponent(testAllOk)
Expand Down
43 changes: 43 additions & 0 deletions test/headless/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,46 @@ import { join } from 'path'
export const getFileUrl = (dirname, filename) => {
return 'file:' + join(dirname, filename)
}

export const makeTestAllOk = (test, browser) => {
// Create a custom test runner to reduce boilerplate code.
// Opens the test page and evaluates the test results.
//
// Parameters:
// test
// a tape.Test instance
// browser
// a puppeteer Browser instance
//
// Returns
// a function (unitName, dirname, filename)
//
return async (unitName, dirname, filename) => {
test(unitName, async (t) => {
// Setup
const pageUrl = getFileUrl(dirname, filename)
const page = await browser.newPage()
await page.setViewport({ width: 1000, height: 500 })
await page.goto(pageUrl, { waitUntil: 'domcontentloaded' })

// Wait max 5 sec for an async test to finish.
await page.waitForFunction('window.test.finished === true', { timeout: 5000 })
// Collect test results.
const report = await page.evaluate(() => window.test.report())

// Check test results.
for (let i = 0; i < report.length; i += 1) {
t._assert(report[i].result, {
message: report[i].message,
operator: report[i].operator,
actual: report[i].actual,
expected: report[i].expected
})
}

// Exit
await page.close()
t.end()
})
}
}

0 comments on commit 21b3dfd

Please sign in to comment.