How do you mock 'setTimeout' on a mocked function

Viewed 12

How would I go about mocking setTimeout when calling Express?

I keep getting this error TypeError: Cannot read property 'setTimeout' of undefined however, I'm not sure if I need to wrap the test with a setTimeout or somehow mock it.

const server = express();
server.use(Router);
const http = server.listen(process.env.PORT || 3000);
http.setTimeout(1000 * 60 * 5);
const app = {
    use: jest.fn(),
    listen: jest.fn(),
    enable: jest.fn(),
};

jest.mock('express', () => jest.fn().mockImplementation(() => app));

const setup = require('./setup');

describe('setup > index', () => {
    describe('setup', () => {
        const OLD_ENV = process.env;

        beforeEach(() => {
            jest.resetModules();
            process.env = { ...OLD_ENV };
        });

        it('configures app correctly', async () => {
            const callback = jest.fn();

            await setup(callback);

            expect(app.use).toHaveBeenCalledTimes(1);
        });
    });
});
0 Answers
Related