-
Notifications
You must be signed in to change notification settings - Fork 44
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
Comments
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 It would be helpful if I could mock the |
Thank you @jordanarldt for the example, that's really helpful. Can you clarify in this particular case, does |
Sure, in my web app, 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 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 |
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. |
Hey again @fwouts I think originally I had my Thanks! |
@fwouts I think you should be able to close this issue since the alias functionality seems to work! 👍 |
Awesome to hear! I'll leave this open until I've written some docs mentioning this approach 😊 |
See #950 for an example where this would be useful, reported by @tvaughan73.
The text was updated successfully, but these errors were encountered: