Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

116
Visualizações
Mocking my database functions globally with jest

See original post here for more context.
tl;dr, I had a database table with some functions (createOne, findOne etc) that I managed to mock in order to do some unit testing of my API.

Now I have multiple tables and a lot more tests to write, and the solution I have now generates a lot of code duplication when mocking the database functions in every test file. Therefore, I would now like to have the mocks available globally in all my test files. I have read a lot about this on all different forums as well as checking the official docs but without success.

i have the following structure rn:

__tests__
  pages
    api
      <api tests>
src
  lib
    database
      table1.js
      table2.js
      __mocks__ (as per the official docs, this doesn't seem to work)
        table1.js
        table2.js
  pages
    api
      <files under test that calls database functions>

and then example mock:

// __mocks__/table1.js
const table1 = jest.createMockFromModule("../table1");
table1.createOne = jest.fn().mockImplementation(async () => return {id: 1});
export default table1;

// __tests__/pages/api/route1
jest.mock("src/lib/database/table1");
import {createOne} from "src/lib/database/table1";

/* rest of testing like the original post */

But the mocks aren't running correctly, and createOne is returning undefined. how do I make the mocks work globally?

about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda