Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!