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

How to test with Jest and JSDom with Node.js? #89

Open
franciscop opened this issue Aug 7, 2024 · 1 comment
Open

How to test with Jest and JSDom with Node.js? #89

franciscop opened this issue Aug 7, 2024 · 1 comment
Labels
bug Something isn't working

Comments

@franciscop
Copy link

franciscop commented Aug 7, 2024

I am trying to test this with Jest (and jsdom), repro here, so I was wondering if someone got it working. The problem I'm having is that JSDom doesn't have TextEncoder/TextDecoder, which according to this guide should be polyfilled like this:

import * as util from "util";

Object.defineProperty(window, "TextEncoder", {
  writable: true,
  value: util.TextEncoder,
});
Object.defineProperty(window, "TextDecoder", {
  writable: true,
  value: util.TextDecoder,
});

However that, for some reason, makes even the simplest test code to fail, like this:

import { openKv } from "@deno/kv";

const db = await openKv("db/kv.db");
await db.set(["hello"], "world");
console.log(await db.get(["hello"]));

The error given is this:

"TypeError: The input must be a Uint8Array, a string, or an ArrayBuffer. Received a value of the type Buffer."

This seems to be some sort of mismatch between Node's util.TextEncoder and what Deno's KV is expecting. I made a repro here:

https://github.com/franciscop/jest-kv-repro

I'm not expecting a "fix" at this point, since it seems the linked Github issue is ongoing and what will eventually fix this, but I open this issue in case others are experiencing this issue and to see if anyone has/will come with a workaround!

@marvinhagemeister
Copy link

Took a quick look into this. It seems the problem stems from a difference in napi handling between Node and Deno. The tests in this repository are run with Deno and Deno returns a Uint8Array whereas Node returns a Buffer from atomicWrite():

const { status, versionstamp } = await this.atomicWrite(write);

@marvinhagemeister marvinhagemeister added the bug Something isn't working label Aug 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants