|  | 
|  | 1 | +'use strict'; | 
|  | 2 | +require('../common'); | 
|  | 3 | +const assert = require('assert'); | 
|  | 4 | +const { Worker } = require('worker_threads'); | 
|  | 5 | +const { test } = require('node:test'); | 
|  | 6 | +const { once } = require('events'); | 
|  | 7 | + | 
|  | 8 | +const esmHelloWorld = ` | 
|  | 9 | +    import worker from 'worker_threads'; | 
|  | 10 | +    const foo: string = 'Hello, World!'; | 
|  | 11 | +    worker.parentPort.postMessage(foo); | 
|  | 12 | +`; | 
|  | 13 | + | 
|  | 14 | +const cjsHelloWorld = ` | 
|  | 15 | +    const { parentPort } = require('worker_threads'); | 
|  | 16 | +    const foo: string = 'Hello, World!'; | 
|  | 17 | +    parentPort.postMessage(foo); | 
|  | 18 | +`; | 
|  | 19 | + | 
|  | 20 | +const disableTypeScriptWarningFlag = '--disable-warning=ExperimentalWarning'; | 
|  | 21 | + | 
|  | 22 | +test('Worker eval module typescript without input-type', async () => { | 
|  | 23 | +  const w = new Worker(esmHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] }); | 
|  | 24 | +  assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']); | 
|  | 25 | +}); | 
|  | 26 | + | 
|  | 27 | +test('Worker eval module typescript with --input-type=module-typescript', async () => { | 
|  | 28 | +  const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript', | 
|  | 29 | +                                                               disableTypeScriptWarningFlag] }); | 
|  | 30 | +  assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']); | 
|  | 31 | +}); | 
|  | 32 | + | 
|  | 33 | +test('Worker eval module typescript with --input-type=commonjs-typescript', async () => { | 
|  | 34 | +  const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript', | 
|  | 35 | +                                                               disableTypeScriptWarningFlag] }); | 
|  | 36 | + | 
|  | 37 | +  const [err] = await once(w, 'error'); | 
|  | 38 | +  assert.strictEqual(err.name, 'SyntaxError'); | 
|  | 39 | +  assert.match(err.message, /Cannot use import statement outside a module/); | 
|  | 40 | +}); | 
|  | 41 | + | 
|  | 42 | +test('Worker eval module typescript with --input-type=module', async () => { | 
|  | 43 | +  const w = new Worker(esmHelloWorld, { eval: true, execArgv: ['--input-type=module', | 
|  | 44 | +                                                               disableTypeScriptWarningFlag] }); | 
|  | 45 | +  const [err] = await once(w, 'error'); | 
|  | 46 | +  assert.strictEqual(err.name, 'SyntaxError'); | 
|  | 47 | +  assert.match(err.message, /Missing initializer in const declaration/); | 
|  | 48 | +}); | 
|  | 49 | + | 
|  | 50 | +test('Worker eval commonjs typescript without input-type', async () => { | 
|  | 51 | +  const w = new Worker(cjsHelloWorld, { eval: true, execArgv: [disableTypeScriptWarningFlag] }); | 
|  | 52 | +  assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']); | 
|  | 53 | +}); | 
|  | 54 | + | 
|  | 55 | +test('Worker eval commonjs typescript with --input-type=commonjs-typescript', async () => { | 
|  | 56 | +  const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=commonjs-typescript', | 
|  | 57 | +                                                               disableTypeScriptWarningFlag] }); | 
|  | 58 | +  assert.deepStrictEqual(await once(w, 'message'), ['Hello, World!']); | 
|  | 59 | +}); | 
|  | 60 | + | 
|  | 61 | +test('Worker eval commonjs typescript with --input-type=module-typescript', async () => { | 
|  | 62 | +  const w = new Worker(cjsHelloWorld, { eval: true, execArgv: ['--input-type=module-typescript', | 
|  | 63 | +                                                               disableTypeScriptWarningFlag] }); | 
|  | 64 | +  const [err] = await once(w, 'error'); | 
|  | 65 | +  assert.strictEqual(err.name, 'ReferenceError'); | 
|  | 66 | +  assert.match(err.message, /require is not defined in ES module scope, you can use import instead/); | 
|  | 67 | +}); | 
0 commit comments