Cloudflare Workers and FaunaDB and debugging with Jest ESM mode

similar issue ==> # Cloudflare Workers and FaunaDB

similar issue comes from using jest to debug Cloudflare DurableObject. using Jest in ESM mode using miniflare

the code fails at:

The only code within the Fauna JavaScript driver involving custom is this:

var nodeUtil = util.isNodeEnv() ? require('util') : null

var customInspect = nodeUtil && nodeUtil.inspect.custom

See: faunadb-js/values.js at master · fauna/faunadb-js · GitHub

=======================

need to replace this line:
var nodeUtil = util.isNodeEnv() ? require(‘util’) : null

with this line:
var nodeUtil = null

=========================

require(‘util’) is NOT ESM supported when using JEST ESM for miniflare debug

Hi @alex_doan

What is the error that you receive? Is it a runtime issue trying to actually call require('util')?

Do you have any code you can share that reproduces the issue, so we can see the project setup?

The issue in the linked topic has been resolved. required('util') should not be called unless the util.NodeEnv() is true. It’s calculated here:

function isNodeEnv() {
  return (
    typeof window === 'undefined' &&
    typeof process !== 'undefined' &&
    process.versions != null &&
    process.versions.node != null
  )
}

Which I think should behave correctly for miniflare and avoid calling require('util'). So maybe this is particular to Jest?

Im confused. the jest.config.js is: useModules: true, and useESM true
==> what does [process.versions.node] have anything to do with ESM or commonJS

test(‘should pass’, () => {
expect(process.versions.node).toBe(‘16’);
});

the [process.versions.node] always return node version, regardless of ESM or commonJS in miniflare…
==> the logic to short-circuit the [ util.isNodeEnv() ? require(‘util’) : null ] does not work, becuase it will always call [require(‘util’) ] in ESM mode, and this is the probelm…

can you please fix the logic to NOT call require(‘util’), in ESM mode?


The code is not trying to branch on ESM vs not-ESM. It is branching (as intended) on Node vs not-Node. util is a built-in node module, so there appears to be something about Jest that is building the project without it.

I have raised an issue on github and internally to prioritize and investigate further.

Please be aware that we have no longer plan to develop and release the v5 driver, which was intended to be more ESM friendly. We are continuing to maintain the v4 driver, though. We are also working on our v10 driver, which is in beta now. We have automated tests that run the v10 driver in Cloudflare workers.

I’m trying to run Jest within the cloudflare/miniflare environment:

==> could this be the reason for this issue?
==> if so, how do we work around this problem in the jest/miniflare environment?
==> is there a simple workaround?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.