tinyplex / tinybase

The reactive data store for local‑first apps.
https://tinybase.org
MIT License
3.71k stars 77 forks source link

Tinybase causes jest-expo to fail #101

Closed shaneosullivan closed 11 months ago

shaneosullivan commented 11 months ago

Describe the bug

Jest used to work just fine when running Tinybase in an Expo project, but with a recent upgrade it no longer works.

The error below is seen, SyntaxError: Unexpected token 'export'

Screenshot 2023-10-27 at 12 13 46

Your Example Website or App

No response

Steps to Reproduce the Bug or Issue

  1. Create an Expo project (it may or may not fail on other types of projects, I haven't tested
  2. Install jest-expo
  3. In package.json scripts put ` "test": "jest"
  4. Add a single simple test that imports from tinybase
    
    import { createStore } from "tinybase";

it("should create a store", () => { const store = createStore(); expect(!!store).toBe(true); });


5. Run `npm run test`

### Expected behavior

The test should run and pass with no issues

### Screenshots or Videos

<img width="644" alt="Screenshot 2023-10-27 at 12 13 46" src="https://github.com/tinyplex/tinybase/assets/49378/f57496c2-342a-4748-8b75-a08055f2775e">

### Platform

- OS: MacOS
- Browser: Terminal
- Version: 4.3.22

### Additional context

_No response_
jamesgpearce commented 11 months ago

Outside chance, but is it the same for 4.3.21?

shaneosullivan commented 11 months ago

Yes, I first saw on 4.3.21 and upgraded to 4.3.22 to see if it went away

On Fri 27 Oct 2023 at 12:53, James Pearce @.***> wrote:

Outside chance, but is it the same for 4.3.21?

— Reply to this email directly, view it on GitHub https://github.com/tinyplex/tinybase/issues/101#issuecomment-1782784872, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAMBYQNIRCVW64ACKUQ2ODYBOOCLAVCNFSM6AAAAAA6SU7IE2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBSG44DIOBXGI . You are receiving this because you authored the thread.Message ID: @.***>

jamesgpearce commented 11 months ago

I assume that 'tinybase/lib/tinybase' or 'tinybase/lib/tinybase.js' works OK? I feel like your web app is using one bundler, this another, or something. Not sure how to keep them both happy at the same time yet.

shaneosullivan commented 11 months ago

No, neither of those work. The first gives the same error, and the second cannot find the module when just .js

jamesgpearce commented 11 months ago

OK I can repro, so let me see if I can thread this needle.

jamesgpearce commented 11 months ago

Looks like this is unrelated to the 4.3.21 and 4.3.22 changes - maybe was always broken?

jamesgpearce commented 11 months ago

OK, the problem is that your Jest/Babel set up is not transpiling the TinyBase module.

You have two choices: either

1) Use tinybase/umd in the import to get a pre-compiled version of TinyBase

OR

2) Add this to your Jest configuration in package.json for example:

"transformIgnorePatterns": [
  "<rootDir>/node_modules/(?!(tinybase|@react-native|react-native)/.*)"
]

Kind of a double negative thing, but basically this transpiles node_modules/tinybase and node_modules/react-native before Jest runs.

shaneosullivan commented 11 months ago

Awesome, I just tried the second approach and it worked great. Thanks James!