planetscale/database-js

Doesn't work with Jest

Xexr opened this issue · 5 comments

Xexr commented

Expected Behavior

"@planetscale/database" should be able to import in Jest tests.

Current Behavior
As in issues #68 & #70, this still doesn't work with Jest, both of those issues have been closed but neither have been resolved.
An error occurs and jest is not able to run.

 FAIL  ./test.test.ts
  ● Test suite failed to run

    Jest encountered an unexpected token

    Jest failed to parse a file. This happens e.g. when your code or its dependencies use non-standard JavaScript syntax, or when Jest is not configured to support such syntax.

    Out of the box Jest supports Babel, which will be used to transform your files into valid JS based on your Babel configuration.

    By default "node_modules" folder is ignored by transformers.

    Here's what you can do:
     • If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.
     • If you are trying to use TypeScript, see https://jestjs.io/docs/getting-started#using-typescript
     • To have some of your "node_modules" files transformed, you can specify a custom "transformIgnorePatterns" in your config.
     • If you need a custom transformation specify a "transform" option in your config.
     • If you simply want to mock your non-JS modules (e.g. binary assets) you can stub them out with the "moduleNameMapper" config option.

    You'll find more details and examples of these config options in the docs:
    https://jestjs.io/docs/configuration
    For information about custom transformations, see:
    https://jestjs.io/docs/code-transformation

    Details:

    /home/xexr/projects/planetscale-jest-poc/node_modules/.pnpm/@planetscale+database@1.4.0/node_modules/@planetscale/database/dist/index.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { format } from './sanitization.js';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { fn } from '.';
    > 2 | import { Connection } from '@planetscale/database';
        | ^
      3 |
      4 | describe('test', () => {
      5 |   beforeEach(() => {

      at Runtime.createScriptFromCode (node_modules/.pnpm/jest-runtime@29.5.0/node_modules/jest-runtime/build/index.js:1495:14)
      at Object.<anonymous> (test.test.ts:2:1)

What I've tried

As with #68, I've added the line below to jest.config.js:

`transformIgnorePatterns: ['node_modules/(?!(@planetscale/database))'],

However, this does not resolve the issue, it seems there is something underlying with how database-js is configured that prevents it from being imported correctly into Jest.

Steps to Reproduce
The original repo created by kenfdev still exhibits the same behaviour I'm seeing in my own usage, so serves as a good example:
https://github.com/kenfdev/planetscale-jest-poc

Install packages:

npm install

// install jest if you don't already have it
npm install --save-dev jest

// run tests
npm test

Thanks for your help

Maybe try "transformIgnorePatterns": [] ?
I got this working here: https://github.com/roelandmoors/authjs-adapter-sql/blob/main/tests/planetscale.test.ts

I believe that #122 (Add support for CJS environments) will solve this- it appears jest is expecting a CJS module and complains about the ESM-only version that this package is currently limited to:

SyntaxError: Cannot use import statement outside a module

In the meantime, I would try following the suggestion in the error message:

• If you are trying to use ECMAScript Modules, see https://jestjs.io/docs/ecmascript-modules for how to enable it.

TLDR: As of v29.5, jest ships with experimental support for ESM modules.

I have it working with jest using ESM modules, take a look!

Minimal Example:

https://github.com/OultimoCoder/kysely-planetscale-issue-20

Larger example with Cloudflare workers, Hono and miniflare:

https://github.com/OultimoCoder/cloudflare-planetscale-hono-boilerplate

If this is unrelated, apologies.

You might try again with the 1.9.0 release that includes a CommonJS build.

With 1.9.0 released, I'm going to close this one out. If you still encounter issues, please feel free to re-open it!