|
1 | 1 | import { createRequire } from 'module';
|
2 | 2 | import { sep, posix, join } from 'path';
|
3 | 3 | import { fileURLToPath } from 'url';
|
| 4 | +import { Worker } from 'worker_threads'; |
4 | 5 |
|
5 | 6 | import { rollup } from 'rollup';
|
6 | 7 | import globby from 'globby';
|
@@ -96,32 +97,26 @@ test('imports', async (t) => {
|
96 | 97 | await testBundle(t, bundle);
|
97 | 98 | });
|
98 | 99 |
|
99 |
| -try { |
100 |
| - // eslint-disable-next-line global-require |
101 |
| - const { Worker } = require('worker_threads'); |
102 |
| - test('worker', async (t) => { |
103 |
| - t.plan(2); |
| 100 | +test('worker', async (t) => { |
| 101 | + t.plan(2); |
104 | 102 |
|
105 |
| - const bundle = await rollup({ |
106 |
| - input: 'fixtures/worker.js', |
107 |
| - plugins: [wasmPlugin()] |
108 |
| - }); |
109 |
| - const code = await getCode(bundle); |
110 |
| - const executeWorker = () => { |
111 |
| - const worker = new Worker(code, { eval: true }); |
112 |
| - return new Promise((resolve, reject) => { |
113 |
| - worker.on('error', (err) => reject(err)); |
114 |
| - worker.on('exit', (exitCode) => resolve(exitCode)); |
115 |
| - }); |
116 |
| - }; |
117 |
| - await t.notThrowsAsync(async () => { |
118 |
| - const result = await executeWorker(); |
119 |
| - t.true(result === 0); |
| 103 | + const bundle = await rollup({ |
| 104 | + input: 'fixtures/worker.js', |
| 105 | + plugins: [wasmPlugin()] |
| 106 | + }); |
| 107 | + const code = await getCode(bundle); |
| 108 | + const executeWorker = () => { |
| 109 | + const worker = new Worker(code, { eval: true }); |
| 110 | + return new Promise((resolve, reject) => { |
| 111 | + worker.on('error', (err) => reject(err)); |
| 112 | + worker.on('exit', (exitCode) => resolve(exitCode)); |
120 | 113 | });
|
| 114 | + }; |
| 115 | + await t.notThrowsAsync(async () => { |
| 116 | + const result = await executeWorker(); |
| 117 | + t.true(result === 0); |
121 | 118 | });
|
122 |
| -} catch (err) { |
123 |
| - // worker threads aren't fully supported in Node versions before 11.7.0 |
124 |
| -} |
| 119 | +}); |
125 | 120 |
|
126 | 121 | test('injectHelper', async (t) => {
|
127 | 122 | t.plan(4);
|
@@ -233,3 +228,29 @@ test('works as CJS plugin', async (t) => {
|
233 | 228 | });
|
234 | 229 | await testBundle(t, bundle);
|
235 | 230 | });
|
| 231 | + |
| 232 | +// uncaught exception will cause test failures on this node version. |
| 233 | +if (!process.version.startsWith('v14')) { |
| 234 | + test('avoid uncaught exception on file read', async (t) => { |
| 235 | + t.plan(2); |
| 236 | + |
| 237 | + const bundle = await rollup({ |
| 238 | + input: 'fixtures/complex.js', |
| 239 | + plugins: [wasmPlugin({ maxFileSize: 0, targetEnv: 'node' })] |
| 240 | + }); |
| 241 | + |
| 242 | + const raw = await getCode(bundle); |
| 243 | + const code = raw.replace('.wasm', '-does-not-exist.wasm'); |
| 244 | + |
| 245 | + const executeWorker = () => { |
| 246 | + const worker = new Worker(`let result; ${code}`, { eval: true }); |
| 247 | + return new Promise((resolve, reject) => { |
| 248 | + worker.on('error', (err) => reject(err)); |
| 249 | + worker.on('exit', (exitCode) => resolve(exitCode)); |
| 250 | + }); |
| 251 | + }; |
| 252 | + |
| 253 | + const err = await t.throwsAsync(() => executeWorker()); |
| 254 | + t.regex(err.message, /no such file or directory/); |
| 255 | + }); |
| 256 | +} |
0 commit comments