jest-prisma
jest-prisma copied to clipboard
Invalid variable access: jestPrisma
In the documentation it says
/* setup-prisma.js */
jest.mock("./src/client", () => {
return {
prisma: jestPrisma.client,
};
});
When running npm test
it says:
ReferenceError: /Users/myname/w/myrepo/setup-prisma.js: The module factory of `jest.mock()` is not allowed to reference any out-of-scope variables.
Invalid variable access: jestPrisma
jest.config.js
looks like:
module.exports = {
clearMocks: true,
preset: './jest-preset',
testEnvironment: "@quramy/jest-prisma/environment",
testEnvironmentOptions: {
verboseQuery: true,
},
setupFilesAfterEnv: ["./setup-prisma.js"],
}
It seems there is a problem setting up jestPrisma
to be accessible from the 'setup.prisma.js' file?
I tried adding const { jestPrisma } = require('@quramy/jest-prisma');
to the top of 'setup.prisma.js' and it still gave the same error.
const { jestPrisma } = require('@quramy/jest-prisma');
jest.mock("./lib/prisma", () => {
return {
prisma: jestPrisma.client,
};
});
I changed setup-prisma.js
to:
jest.mock("./lib/prisma", () => {
const { jestPrisma } = require('@quramy/jest-prisma');
return {
prisma: jestPrisma.client,
};
});
new error!
TypeError: Cannot read properties of undefined (reading 'client')
Try this:
const mockJestPrisma = jestPrisma
jest.mock("./src/client", () => ({
prisma: mockJestPrisma.client,
}))
When you mock a module this call is hoisted which means that locally declared variables might not be available inside the mock. However, when you a variable name starts with mock
then jest
allows it.