Skip to content

Commit e53ae97

Browse files
committed
Tweaks to test code
1 parent 75a62b0 commit e53ae97

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

test/tests.js

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
let expect
2-
let npmcanvas
2+
let nodeCanvas
33
let helloModule
44

55
const IS_NODE = typeof process === 'object' && typeof require === 'function'
66

7-
if (IS_NODE) {
8-
expect = require('chai').expect
9-
npmcanvas = require('canvas')
10-
} else {
11-
expect = chai.expect
12-
mocha.setup('bdd')
13-
window.createHelloModule().then(module => {
14-
helloModule = module
7+
const main = async () => {
8+
if (IS_NODE) {
9+
expect = require('chai').expect
10+
nodeCanvas = require('canvas')
11+
} else {
12+
expect = chai.expect
13+
mocha.setup('bdd')
14+
helloModule = await window.createHelloModule()
1515
mocha.run()
16-
})
16+
}
1717
}
1818

19+
main()
20+
1921
const getImageDataNode = async () => {
2022
const fs = require('fs').promises
2123
const png = await fs.readFile('src/images/sudoku-1.png')
2224
return new Promise(resolve => {
23-
const img = new npmcanvas.Image()
25+
const img = new nodeCanvas.Image()
2426
img.onload = () => {
25-
const canvas = npmcanvas.createCanvas(img.width, img.height)
27+
const canvas = nodeCanvas.createCanvas(img.width, img.height)
2628
const ctx = canvas.getContext('2d')
2729
ctx.drawImage(img, 0, 0)
2830
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height)
@@ -52,17 +54,10 @@ const getImageData = () =>
5254

5355
describe('tests', () => {
5456

55-
before(() => {
57+
before(async () => {
5658
if (IS_NODE) {
57-
return new Promise(resolve => {
58-
// This will need changing since adding these flags:
59-
// -s MODULARIZE=1 -s EXPORT_NAME=createHelloModule
60-
const Module = require('../build/hello.js')
61-
Module.onRuntimeInitialized = () => {
62-
helloModule = Module
63-
resolve()
64-
}
65-
})
59+
const createHelloModule = require('../build/hello.js')
60+
helloModule = await createHelloModule()
6661
}
6762
})
6863

@@ -82,21 +77,25 @@ describe('tests', () => {
8277

8378
const imageData = await getImageData()
8479
const { data, width, height } = imageData
80+
8581
const addr = processImage(data, width, height)
86-
const returnDataAddr = addr / helloModule.HEAP32.BYTES_PER_ELEMENT
87-
const returnData = helloModule.HEAP32.slice(returnDataAddr, returnDataAddr + 8)
8882

89-
const [bbx, bby, bbw, bbh, imgw, imgh, imgd] = returnData
83+
try {
84+
const addr32 = addr / helloModule.HEAP32.BYTES_PER_ELEMENT
85+
const data32 = helloModule.HEAP32.slice(addr32, addr32 + 22)
9086

91-
expectWithinTolerance(bbx, 20)
92-
expectWithinTolerance(bby, 30)
93-
expectWithinTolerance(bbw, 185)
94-
expectWithinTolerance(bbh, 185)
87+
const [bbx, bby, bbw, bbh, imgw, imgh, imgd] = data32
9588

96-
expect(imgw).to.equal(224)
97-
expect(imgh).to.equal(224)
98-
expect(imgd).to.equal(1)
89+
expectWithinTolerance(bbx, 20)
90+
expectWithinTolerance(bby, 30)
91+
expectWithinTolerance(bbw, 185)
92+
expectWithinTolerance(bbh, 185)
9993

100-
helloModule._free(addr)
94+
expect(imgw).to.equal(224)
95+
expect(imgh).to.equal(224)
96+
expect(imgd).to.equal(1)
97+
} finally {
98+
helloModule._free(addr)
99+
}
101100
})
102101
})

0 commit comments

Comments
 (0)