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

134
Visualizações
Test nested promises functions with JEST framework

My dynamo helper is like this:

helper.ts

export const save = async (order: TkOrderModel): Promise<void> => {
    const params = {
        ...defaultParams,
        ReturnValues: 'ALL_OLD',
        Item: order
    };
    await docClient.put(params, async (err, data) => {
        if (err)
            return err;
        else
            return data;
    }).promise();
};

test file:

jest.mock('aws-sdk', () => {
    const fakePromise = {
        promise: jest.fn(),
    };
    const mDocumentClient = {
        put: fakePromise.promise,
    };
    const mDynamoDB = { DocumentClient: jest.fn(() => mDocumentClient) };
    return { DynamoDB: mDynamoDB };
});
const mDynamoDb = new AWS.DynamoDB.DocumentClient();

const tableName = 'orders';

const defaultParams = {
    TableName: tableName,
};

afterAll(() => {
    jest.resetAllMocks();
});
const fakePromise = {
    promise: jest.fn(),
};

describe('dynamo helper', () => {
    test('should save items', async () => {
        const order = {
            cartId: 'any_cartId',
            orderId: 'any_orderId',
        };
        const params = {
            ...defaultParams,
            ReturnValues: 'ALL_OLD',
            Item: order,
        };
        const cb = fakePromise.promise.mockImplementation(() =>
            Promise.resolve(order)
        );
        mDynamoDb.put(params, cb);
        await orderRepository.save(order);
        expect(mDynamoDb.put).toBeCalledTimes(1);
    });
});

jest is returning the following error: TypeError: Cannot read property 'promise' of undefined do i need to mock the promise in a different way to not return this compilation error? I've tried to do it in different ways and have not been successful. Any suggestion?

about 4 years ago · Juan Pablo Isaza
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