【问题标题】:Jest implementation is executed but function is never calledJest 实现已执行,但从未调用函数
【发布时间】:2018-05-06 04:48:32
【问题描述】:

我是个玩笑的新手,遇到了一些奇怪的事情:

我得到了这个测试:

let mockedLogger = require('../util/testLogger');
jest.mock('../../handler/util/winstonLogHandler', () => {
    return {
        getLogger: function() {
            return mockedLogger;
        },
    };
});

const sendMock = jest
    .fn()
    .mockImplementationOnce(() => {
        return new Promise(function(resolve, reject) {
            resolve('something');
        });
    })
    .mockImplementationOnce(() => {
        return new Promise(function(resolve, reject) {
            reject('some error');
        });
    });
const message = {
    channel: {
        send: sendMock,
    },
};

const ping = require('../.././commands/util/ping');

describe('Testing the ping command', () => {
    test('it should have properties', () => {
        expect(ping.name).not.toBe(undefined);
        expect(ping.name).not.toBe(undefined);
        expect(ping.name).not.toBe(undefined);
    });
    describe('it should execute', () => {
        test('and send a message', () => {
            ping.execute(null, message);
            expect(sendMock).toBeCalled();
        });
        test('or log an error', () => {
            ping.execute(null, message);
            expect(mockedLogger.error).toBeCalledWith(`Ping: Error sending message: some error`);
        });
    });
});

测试记录器:

const errorLogMock = jest
    .fn()
    .mockImplementation((log) => console.error(log))
    .mockName('errorLog');

module.exports = {
    error: errorLogMock,
};

对于这个模块:

const winstonLogHandler = require('../../handler/util/winstonLogHandler');
const logger = winstonLogHandler.getLogger();

module.exports = {
    name: 'ping',
    description: 'pong!',
    disabled: false,
    requireDB: false,
    execute(client, message) {
        message.channel.send('pong!').catch(error => {
            logger.error(`Ping: Error sending message: ${error}`);
        });
    },
};

现在,如果我执行测试,我会得到我的 logError Mock 函数从未被调用的错误。尽管在控制台中我看到执行已执行。那么如果从未调用过该函数,如何执行实现呢?我错过了什么吗?

【问题讨论】:

  • 您正在测试的任何代码是异步的吗?如果是这样,您可能需要使用 async/await 或在测试中手动返回 Promise 来等待它。
  • 是的,我在实现中手动返回了 Promise。在检查模拟之前更改测试以等待所有 Promise 完成执行是解决方案!非常感谢您的提示!
  • 酷,您可以将其添加为您的答案,以帮助其他用户解决此问题。

标签: javascript jestjs


【解决方案1】:

将测试改为:

describe('Testing the ping command', () => {
    test('it should have properties', () => {
        expect(ping.name).not.toBe(undefined);
        expect(ping.name).not.toBe(undefined);
        expect(ping.name).not.toBe(undefined);
    });
    describe('it should execute', () => {
        test('and send a message', () => {
            await ping.execute(null, message);
            expect(sendMock).toBeCalled();
        });
        test('or log an error', () => {
            await ping.execute(null, message);
            expect(mockedLogger.error).toBeCalledWith(`Ping: Error sending message: some error`);
        });
    });
});

解决了这个问题。 Jest 没有函数调用,因为异步代码没有完成执行。

【讨论】:

    猜你喜欢
    • 2019-07-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-09
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多