Skip to content

Commit 8b987c2

Browse files
committed
test: migrate util.js to ESM
- Changed all instances of loading the utility functions from 'util.js' to 'util.mjs' across various test files. - Removed the old 'util.js' file and created a new 'util.mjs' file implementing the same functionality using ES module syntax. - Updated paths and imports in test files to ensure compatibility with the new module format.
1 parent b6eb78e commit 8b987c2

96 files changed

Lines changed: 228 additions & 150 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/test/arg/arg.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const assert = require('assert')
3-
const { load } = require('../util')
3+
const { load } = require('../util.mjs')
44

55
module.exports = load('arg').then((addon) => {
66
assert.strictEqual(addon.add(3, 5), 8)

packages/test/array/array.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const assert = require('assert')
3-
const { load } = require('../util')
3+
const { load } = require('../util.mjs')
44

55
// eslint-disable-next-line camelcase
66
module.exports = load('array').then(test_array => {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/* eslint-disable no-undef */
2+
/* eslint-disable camelcase */
3+
4+
import { test } from 'vitest'
5+
import assert from 'assert'
6+
7+
console.log(assert)
8+
9+
test('async-wasi', async function main () {
10+
const init = async function () {
11+
const { WASI } = await import('../../../node_modules/@tybys/wasm-util/dist/wasm-util.esm.js')
12+
const { createNapiModule, loadNapiModule } = await import('../../core/dist/emnapi-core.js')
13+
const { getDefaultContext } = await import('../../runtime/dist/emnapi.js')
14+
const wasi = new WASI()
15+
const napiModule = createNapiModule({
16+
context: getDefaultContext(),
17+
reuseWorker: {
18+
size: 4
19+
},
20+
waitThreadStart: typeof window === 'undefined' ? 1000 : false,
21+
onCreateWorker () {
22+
return new Worker(new URL('../worker.mjs', import.meta.url), { type: 'module' })
23+
},
24+
})
25+
const wasmMemory = new WebAssembly.Memory({
26+
initial: 16777216 / 65536,
27+
maximum: 2147483648 / 65536,
28+
shared: true
29+
})
30+
31+
const p = new Promise((resolve, reject) => {
32+
loadNapiModule(napiModule, '../.build/wasm32-wasi-threads/Debug/async.wasm', {
33+
wasi,
34+
overwriteImports (importObject) {
35+
importObject.env.memory = wasmMemory
36+
}
37+
}).then(() => {
38+
resolve(napiModule.exports)
39+
}).catch(reject)
40+
})
41+
p.Module = napiModule
42+
return p
43+
}
44+
const test_async = await init()
45+
46+
await new Promise((resolve) => {
47+
// Successful async execution and completion callback.
48+
test_async.Test(5, {}, function (err, val) {
49+
console.log('test_async.Test(5, {}, callback)')
50+
assert.strictEqual(err, null)
51+
assert.strictEqual(val, 10)
52+
resolve()
53+
})
54+
})
55+
56+
await new Promise((resolve) => {
57+
// Async work item cancellation with callback.
58+
test_async.TestCancel(() => {
59+
console.log('test_async.TestCancel(callback)')
60+
resolve()
61+
})
62+
})
63+
64+
const iterations = 500
65+
let x = 0
66+
const workDone = (status) => {
67+
assert.strictEqual(status, 0)
68+
if (++x < iterations) {
69+
setTimeout(() => test_async.DoRepeatedWork(workDone))
70+
} else {
71+
console.log(x)
72+
}
73+
}
74+
test_async.DoRepeatedWork(workDone)
75+
76+
await new Promise((resolve) => {
77+
setTimeout(resolve, 3000)
78+
})
79+
80+
if (typeof postMessage === 'function') {
81+
postMessage('done')
82+
}
83+
})

packages/test/async/async.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable camelcase */
22
'use strict'
3-
const { load } = require('../util')
3+
const { load } = require('../util.mjs')
44
const main = require('./main')
55

66
module.exports = main(load('async'), __filename)

packages/test/async/async_hooks.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable camelcase */
22
'use strict'
3-
const { load } = require('../util')
3+
const { load } = require('../util.mjs')
44
const common = require('../common')
55
const assert = require('assert')
66
const async_hooks = require('async_hooks')

packages/test/async/async_st.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable camelcase */
22
'use strict'
3-
const { load } = require('../util')
3+
const { load } = require('../util.mjs')
44
const main = require('./main')
55

66
const RUNTIME_UV_THREADPOOL_SIZE = ('UV_THREADPOOL_SIZE' in process.env) ? Number(process.env.UV_THREADPOOL_SIZE) : 4

packages/test/async_cleanup_hook/async_cleanup_hook.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict'
22
const common = require('../common')
33
const { Worker } = require('worker_threads')
4-
const { load } = require('../util')
4+
const { load } = require('../util.mjs')
55

66
module.exports = new Promise((resolve, reject) => {
77
const w = new Worker(`
8-
const { load } = require(${JSON.stringify(require.resolve('../util.js'))})
8+
const { load } = require(${JSON.stringify(require.resolve('../util.mjs'))})
99
load('async_cleanup_hook')
1010
`, {
1111
eval: true,

packages/test/async_context/ac.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const assert = require('assert')
66
const async_hooks = require('async_hooks')
7-
const { load } = require('../util')
7+
const { load } = require('../util.mjs')
88

99
module.exports = load('async_context', { nodeBinding: require('@emnapi/node-binding') }).then(binding => {
1010
const {

packages/test/async_context/gcable-callback.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
const assert = require('assert')
66
const async_hooks = require('async_hooks')
7-
const { load } = require('../util')
7+
const { load } = require('../util.mjs')
88

99
module.exports = load('async_context', { nodeBinding: require('@emnapi/node-binding') }).then(binding => {
1010
return new Promise((resolve, reject) => {

packages/test/async_context/gcable.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const assert = require('assert')
66
const async_hooks = require('async_hooks')
77
const common = require('../common')
88

9-
const { load } = require('../util')
9+
const { load } = require('../util.mjs')
1010

1111
module.exports = load('async_context', { nodeBinding: require('@emnapi/node-binding') }).then(binding => {
1212
const { createAsyncResource } = binding

0 commit comments

Comments
 (0)