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

Enable mocking of specific modules #951

Open
fwouts opened this issue Aug 18, 2022 · 8 comments
Open

Enable mocking of specific modules #951

fwouts opened this issue Aug 18, 2022 · 8 comments
Labels
documentation Improvements or additions to documentation enhancement New feature or request

Comments

@fwouts
Copy link
Owner

fwouts commented Aug 18, 2022

See #950 for an example where this would be useful, reported by @tvaughan73.

@fwouts fwouts added the enhancement New feature or request label Aug 18, 2022
@jordanarldt
Copy link

jordanarldt commented Sep 3, 2022

This would be awesome!

I'm currently running into a scenario where this would be useful. Here's an example from NextJS:

import { prisma } from "#lib/db";

const NotificationPage: NextPage = () => {
  return (
    <PageError
      icon={SadFaceIcon}
      title="Invalid Notification ID"
      message="The target of the notification you are trying to view does not exist or has been removed."
    />
  );
};

export async function getServerSideProps({ query }: NextPageContext) {
  const uuid = String(query.uuid);
  const data = await prisma.notifications.findMany({
    where: {
      id: uuid,
    },
  });

  return {
    props: {
      data
    },
  };
}

export default NotificationPage;

When trying to preview this NextJS component in PreviewJS, I get an error regarding Prisma not being able to run in the browser.

It would be helpful if I could mock the prisma.notifications.findMany function to return a specific array.

@fwouts
Copy link
Owner Author

fwouts commented Sep 3, 2022

Thank you @jordanarldt for the example, that's really helpful.

Can you clarify in this particular case, does prisma come from an import or is it somehow in the global scope?

@jordanarldt
Copy link

jordanarldt commented Sep 4, 2022

Thank you @jordanarldt for the example, that's really helpful.

Can you clarify in this particular case, does prisma come from an import or is it somehow in the global scope?

Sure, in my web app, prisma comes from an import from #lib/db (alias for src/lib/db, and here's the contents:

import { PrismaClient } from "@prisma/client";

export const prisma: PrismaClient =
  global.prisma ||
  new PrismaClient({
    log: ["query"],
  });

if (process.env.NODE_ENV !== "production") global.prisma = prisma;

So I do set Prisma in global scope during development to prevent reinstantiating the client, but ultimately every use will simply come from importing the prisma const.

If I could simply mock the Prisma object exported from here, it would make things much easier 🙂

Edit: I updated my original code example to show the import

@fwouts
Copy link
Owner Author

fwouts commented Sep 4, 2022

Thanks @jordanarldt!

In this case, I wonder if setting up an alias for this particular module that "redirects it" to a mock implementation wouldn't be enough. See https://previewjs.com/docs/config/aliases

// preview.config.js

module.exports = {
  alias: {
    "#lib/db": "fake-db.js" // or maybe src/lib/db.js?
  }
};

Would this work?

@jordanarldt
Copy link

Thanks @jordanarldt!

In this case, I wonder if setting up an alias for this particular module that "redirects it" to a mock implementation wouldn't be enough. See https://previewjs.com/docs/config/aliases

// preview.config.js

module.exports = {
  alias: {
    "#lib/db": "fake-db.js" // or maybe src/lib/db.js?
  }
};

Would this work?

I did try that in my project, but it didn't seem to work. I'll try it again when I get a chance.

@jordanarldt
Copy link

Hey again @fwouts I think originally I had my preview.config.js file in the wrong location. I was trying to put it in my __previewjs__ folder instead of the app root folder. This should work!

Thanks!

@jordanarldt
Copy link

@fwouts I think you should be able to close this issue since the alias functionality seems to work! 👍

@fwouts
Copy link
Owner Author

fwouts commented Sep 18, 2022

Awesome to hear!

I'll leave this open until I've written some docs mentioning this approach 😊

@fwouts fwouts added the documentation Improvements or additions to documentation label Sep 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation enhancement New feature or request
Projects
Status: Todo
Development

No branches or pull requests

2 participants