Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

261
Vistas
Can I put my jest mock in a separate file? manual mock

In my get-sites.spec.js I created a mock like this which works perfectly fine:

jest.mock('@aws-sdk/client-secrets-manager', () => {
    const SecretsManagerClient = jest.fn().mockImplementation(() => {
        const result = {
            SecretString: "{\"server\":\"host\",\"database\":\"database\",\"user\":\"userName\",\"password\":\"password\"}"
        };

        return {
            send: jest.fn().mockResolvedValue(result)
        };
    });

    const GetSecretValueCommand = jest.fn().mockImplementation(() => {
        return {}
    });

    return {
        SecretsManagerClient,
        GetSecretValueCommand
    }
});

I've tried creating the following directory structure and moved the mock code to the client-secrets-manager.js file.

__tests__/
|- get-sites.spec.js
__mocks__/
|- @aws-sdk/
|-- client-secrets-manager.js
src/
|- get-sites.js

Then in my get-sites.spec.js file I changed the mock code to jest.mock('@aws-sdk/client-secrets-manager');

When I run the test I get an error: TypeError: Cannot read properties of undefined (reading 'SecretString'). Is there some way to move my mock to a separate file to make it available to all my unit tests that will preserve the functionality?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Turns out I was close. I changed the client-secrets-manager.js file in the mocks folder to look like this:

const SecretsManagerClient = jest.fn().mockImplementation(() => {
    return {
        send: jest.fn()
    };
});

const GetSecretValueCommand = jest.fn().mockImplementation(() => {
    return {}
});

module.exports = {
    SecretsManagerClient,
    GetSecretValueCommand
}

Then in my get-sites.spec.js file I added this line to the test:

const sendResponse = {
    SecretString: "{\"server\":\"host\",\"database\":\"database\",\"user\":\"userName\",\"password\":\"password\"}"
};
jest.spyOn(SecretsManagerClient.prototype, 'send').mockResolvedValue(sendResponse);

Then I was able to assert it was called like this:

expect(SecretsManagerClient).toHaveBeenCalledTimes(1);
expect(SecretsManagerClient.prototype.send.mock.calls.length).toEqual(1);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda