Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker_threads cannot load typescript file #3234

Closed
6 tasks done
vafanassieff opened this issue Apr 22, 2023 · 2 comments
Closed
6 tasks done

worker_threads cannot load typescript file #3234

vafanassieff opened this issue Apr 22, 2023 · 2 comments

Comments

@vafanassieff
Copy link

Describe the bug

Hey thanks for the awesone tool !

I'm new to the typescript world, i'm trying to test a node js app using node:worker_threads
It seems tsx run the code if i'm passing a .ts file to the new Worker constructor but i cannot make it work with vitest am i missing something ?

Is there a better way to test apps that leverage node:worker_threads ?

Best !

 DEV  v0.30.1 /Users/afa/code

stderr | src/tmp/index.test.ts > worker > worker
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/afa/code/src/tmp/worker.ts
    at new NodeError (node:internal/errors:399:5)
    at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
    at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
    at defaultLoad (node:internal/modules/esm/load:81:20)
    at nextLoad (node:internal/modules/esm/loader:163:28)
    at ESMLoader.load (node:internal/modules/esm/loader:605:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
    at new ModuleJob (node:internal/modules/esm/module_job:64:26)
    at #createModuleJob (node:internal/modules/esm/loader:480:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:434:34) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Reproduction

index.ts

import { Worker } from 'node:worker_threads'

export const testWorker = () => {
  const worker = new Worker(new URL('worker.ts', import.meta.url))

  worker.once('error', console.error)
  worker.once('online', () => {
    worker.postMessage('hello from main')
  })
  worker.on('message', console.log)

  return worker
}

worker.ts

import { parentPort, workerData } from 'node:worker_threads'

console.log(workerData)

parentPort?.on('message', (message) => {
  console.log(message)
  parentPort?.postMessage(message)
})

index.test.ts

import { describe, test } from 'vitest'

import { testWorker } from './index.js'

describe('worker', () => {
  test('worker', async ({ helpers, dependencies }) => {
    const worker = await testWorker()
    await new Promise((resolve) => setTimeout(resolve, 1000))
  })
})

vitest.config.ts

import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    singleThread: true,
    threads: false,
    environment: 'node'
  }
})

System Info

System:
    OS: macOS 13.3.1
    CPU: (12) arm64 Apple M2 Max
    Memory: 14.26 GB / 64.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 18.15.0 - ~/Library/Caches/fnm_multishells/16671_1682177604979/bin/node
    npm: 9.5.0 - ~/Library/Caches/fnm_multishells/16671_1682177604979/bin/npm
  Browsers:
    Chrome: 112.0.5615.49
    Firefox: 111.0
    Firefox Developer Edition: 112.0
    Safari: 16.4
  npmPackages:
    @vitest/coverage-c8: 0.30.1 => 0.30.1 
    vitest: 0.30.1 => 0.30.1

Used Package Manager

pnpm

Validations

@sheremet-va
Copy link
Member

Vitest doesn't process those and probably won't because of how module resolution works. You can try running Vitest with Node loader manually though:

node ./node_modules/vitest/vitest.mjs --loader tsx

(I did not try this, but something like this should work.)

@vafanassieff
Copy link
Author

Hello thanks for your quick reply !

The hint for the loader was right, i just need to start the worker thread like this

  const worker = new Worker(new URL('worker.ts', import.meta.url), {
      execArgv: process.env.VITEST ? ['--loader', 'tsx'] : undefined,
  })

@github-actions github-actions bot locked and limited conversation to collaborators Jun 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants