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

TypeError when testing with Jest #886

Closed
Sethlans opened this issue Sep 21, 2022 · 2 comments
Closed

TypeError when testing with Jest #886

Sethlans opened this issue Sep 21, 2022 · 2 comments
Labels

Comments

@Sethlans
Copy link

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise): Community
  • Currently blocking your project/work? (yes/no): yes
  • Affecting a production system? (yes/no): yes

Context

  • Node.js version:
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: 2.0.1
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules): express, Jest

What are you trying to achieve or the steps to reproduce?

I have created an express middleware to parse the raw data incoming from a multipart request.
The middleware itself is working properly and doing its job.
The issue is when I test my endpoint with jest. I get the error

TypeError: Cannot set property domain of [object process] which has only a getter

when calling form.parse()
It looks like Jest put some limitations to the process object.
The issue seems to be caused by the dependency asap
requestFlush (/node_modules/asap/raw.js:83:40)

import formidable from 'formidable'
import { AppRequest } from '../../app-types'
import { NextFunction, Response } from 'express-async-router'
export const formMiddleWare = async (req: AppRequest, res: Response, next: NextFunction) => {
  const myPromise = new Promise<void>((resolve, reject) => {
    const form = formidable({
      keepExtensions: true,
      multiples: true,
    })

    const incomingPayload = {} as any
    form.onPart = (part) => {
      part.on('data', (buffer: Buffer) => {
        if (part.originalFilename && part.mimetype) {
          if (!incomingPayload.files) {
            incomingPayload.files = []
          }
          let currentFile = incomingPayload.files.find((obj: any) => {
            return obj.originalname === part.originalFilename && obj.mimetype === part.mimetype
          })
          if (!currentFile) {
            currentFile = {
              originalname: part.originalFilename,
              mimetype: part.mimetype,
              buffer: buffer,
            }
            incomingPayload.files.push(currentFile)
          } else {
            currentFile.buffer = Buffer.concat([currentFile.buffer, buffer])
          }
        } else {
          if (incomingPayload[part.name]) {
            incomingPayload[part.name] = Buffer.concat([incomingPayload[part.name], buffer])
          } else {
            incomingPayload[part.name] = buffer
          }
        }
      })
    }
    form.parse(req, async (err, fields, files) => {
      if (err) {
        console.log(err)
        req.logger.error('Could not parse incoming form', {
          payload: { ...req.body },
        })
        next(err)
        reject(err)
        return
      }
      req.body = incomingPayload
      next()
      resolve(null)
    })
  })

  return myPromise
}

What was the result you got?

Test failing due to an issue with a dependency

What result did you expect?

Test running correctly like when the service runs

@Sethlans Sethlans added the bug label Sep 21, 2022
@tunnckoCore
Copy link
Member

Hey @Sethlans, sorry for the delay.

Sounds like some jest environment problem. No idea. We are using Jest for its tests too and don't have problems.

Maybe it could be because of the onPart override. But I don't think we have anything that does anything with process.

@ziogas
Copy link

ziogas commented Mar 6, 2023

For anyone looking, downgrading to Jest@26 fixed for me the TypeError: Cannot set property domain of [object process] which has only a getter error.

The error comes from the asap package: domain.active = process.domain = null; line.

It must be something with Jest@27 breaking changes. However, couldn't pinpoint what exactly.

@GrosSacASac GrosSacASac closed this as not planned Won't fix, can't repro, duplicate, stale Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants