mswjs / data

Data modeling and relation library for testing JavaScript applications.

Home Page:https://npm.im/@mswjs/data

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error when calling findFirst even though nothing has changed

camchenry opened this issue · comments

First, thanks for the great library. This solves the exact problem I have been working on, and we're already using MSW so this is a natural fit.

I have been running into an issue where the relations seem to "disappear" from the database, even though nothing has changed? I created a CodeSandbox which consistently crashes:
https://codesandbox.io/s/mswjs-relation-undefined-error-wkcfp?file=/src/App.tsx

import { factory, oneOf, primaryKey } from "@mswjs/data";
import * as faker from "faker";

const db = factory({
  user: {
    id: primaryKey(faker.datatype.uuid),
    firstName: String
  },
  userObject: {
    id: primaryKey(faker.datatype.uuid),
    data: String,
    user: oneOf("user")
  }
});

// Seed the database
const seededUser = db.user.create({
  id: "ab4f631c-cca4-498f-a5aa-4828352a7c69",
  firstName: "Test"
});
db.userObject.create({
  user: seededUser,
  data: "test data 1 - associated with user"
});

const queryObject = () => {
  const object = db.userObject.findFirst({
    where: {
      user: {
        id: {
          equals: "ab4f631c-cca4-498f-a5aa-4828352a7c69"
        }
      }
    }
  });
  console.log("query:", { object, user: object.user });
};

// This will work OK, user is not undefined
queryObject();

// This will NOT work ok
setTimeout(() => {
  console.log("deferred query");
  queryObject();
}, 1000);

I make the same queryObject call twice: once immediately after creation, and another 1 second after seeding the database. My expectation is that both invocations of the function should do exactly the same thing. However, what happens is that the first call works as expected, while the second invocation crashes with the error TypeError: can't access property "__type", actualValue is undefined.

Willing to help out with this, let me know what questions you have. Thanks in advance.

(I apologize for all of the issues, but I am desperately trying to figure out this issue, and I have had no luck. I'd love to use this project.)

Hi @camchenry thanks for raising the issue :).

I think that making the property enumerable should solve the issue. I have already done it in the same PR as for the other issue because I need it for deleting internal properties.

@marcosvega91 Sounds great! I will look forward to when that releases then.

Thanks for raising this, @camchenry. You've been immensely helpful in uncovering these behaviors. That's indeed an issue and we should fix it in the upcoming release.

@marcosvega91, I'll take a look at the internal properties removal pull request once more (#67), would love to get it merged. It's a superb piece of work already, so huge thanks! I have one concern, but we'll figure it out.

The issue itself is addressed in #67, but we need to add a test for it. @camchenry, please, would you be interested in contributing the relevant test? We can support you throughout the process and have this not only fixed but bullet-proofed against any future regressions?

@kettanaito Sure! I'll try to get around to it today and put up a PR.

With the test added, is there anything left to address to mark this as done, @camchenry?

@kettanaito i think with the tests written and the manual testing I've done, I think the original issue should be fixed 🤞

@kettanaito Any chance this will make it into a new release sometime soon? This was somewhat of a blocker in my project, but I'd like to give it another go and see if I can uncover any more issues.

Released in 0.3.0. Please give it a try, your feedback is highly valuable!