kulshekhar / ts-jest

A Jest transformer with source map support that lets you use Jest to test projects written in TypeScript.

Home Page:https://kulshekhar.github.io/ts-jest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: When test code that use Worker or ChildProcess throw SyntaxError: Cannot use import statement outside a module

MiniSuperDev opened this issue · comments

Version

29.0.3

Steps to reproduce

  1. Test this file for see the Worker error

worker.test.ts

import {isMainThread, parentPort, Worker} from 'worker_threads';

if (isMainThread) {
  class WorkerExample {
    constructor() {
      const worker = new Worker(__filename);
      worker.postMessage('hi');
    }
  }
  test('worker', () => {
    const example = new WorkerExample();
  });
} else {
  parentPort?.on('message', (message: string) => {
    console.log(message);
  });
}
  1. Test this file for see the ChildProcess error

child-process.test.ts

import {fork} from 'child_process';

if (process.argv[2] === 'child') {
  process.on('message', (message: string) => {
    console.log(message);
  });
} else {
  class ChildProcessExample {
    constructor() {
      const childProcess = fork(__filename, ['child']);
      childProcess.send('hi');
    }
  }
  test('child process', () => {
    const example = new ChildProcessExample();
  });
}

jest.config.js

/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
};

Expected behavior

Not throw an error.

Actual behavior

Worker error:

C:\bug-ts-jest\src\worker.test.ts:1
import {isMainThread, parentPort, Worker} from 'worker_threads';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:187:24)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)

ChildProcess error:

C:\bug-ts-jest\src\child-process.test.ts:1
import {fork} from 'child_process';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:187:24)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:562:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)

Debug log

None

Additional context

No response

Environment

System:
    OS: Windows 10
  Binaries:
    Node: 16.13
    npm: 8.1.2
  npmPackages:
    "jest": "29.3.1",
    "ts-jest": "29.0.3",

hello, any update on this?