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