用玩笑嘲笑导入的打字稿中的类

时间:2018-11-27 14:36:48

标签: typescript unit-testing testing jestjs

我正在尝试使用开玩笑来模拟Typescript类中的导入类,以下代码用于主程序(我从函数内部删除了一些代码,但仍应清楚我正在尝试做什么)

import * as SocketIO from "socket.io";

import {AuthenticatedDao} from "../../dao/authenticated.dao";

export default class AuthenticationService {
    private readonly _authenticatedDao: AuthenticatedDao = AuthenticatedDao.Instance;
    private readonly _io;

    constructor(socketIo: SocketIO.Server) {
        this._io = socketIo;
    }

    public authenticateUser(username: string, password: string, clientSocketId: string): void {
        this._authenticatedDao.authenticateUser(username, password).then((authenticatedUser) => {

        }).catch(rejected => {

        });
    }
}


import {createServer, Server} from 'http';
import * as express from 'express';
import * as socketIo from 'socket.io';
import {LogincredentialsDto} from "./models/dto/logincredentials.dto";
import {config} from './config/config';
import AuthenticationService from "./services/implementation/authentication.service";
import {Logger} from "./helperclasses/logger";
import {format} from "util";

export class ClassA {
    private readonly _configPort = config.socketServerPort;

    private readonly _logger: Logger = Logger.Instance;
    private _app: express.Application;
    private _server: Server;
    private _io: socketIo.Server;
    private _socketServerPort: string | number;
    private _authenticationService: AuthenticationService;


    constructor() {
        this.configure();
        this.socketListener();
    }

    private configure(): void {
        this._app = express();

        //this._server = createServer(config.sslCredentials, this._app);
        this._server = createServer(this._app);

        this._socketServerPort = process.env.PORT || this._configPort;
        this._io = socketIo(this._server);

        this._server.listen(this._socketServerPort, () => {
            this._logger.log(format('Server is running on port: %s', this._socketServerPort));
        });

        this._authenticationService = new AuthenticationService(this._io);
    }


    private socketListener(): void {
        this._io.on('connection', (client) => {
                client.on('authenticate', (loginCreds: LogincredentialsDto) => {
                    console.log(loginCreds.username, loginCreds.password, client.id);
                    this._authenticationService.authenticateUser(loginCreds.username, loginCreds.password, client.id);
                });
            }
        );
    }
}

我试图在“ AuthenticationService”中模拟函数“ authenticateUser”,而不是调用普通的代码来模拟Promise。我尝试使用https://jestjs.io/docs/en/es6-class-mocks中提供的示例,但是当我尝试执行以下操作时:

import AuthenticationService from '../src/services/implementation/authentication.service';
jest.mock('./services/implementation/authentication.service');

beforeEach(() => {
    AuthenticationService.mockClear();
});

it('test', () => {

    // mock.instances is available with automatic mocks:
    const authServerInstance = AuthenticationService.mock.instances[0];

我收到此错误:     错误:(62、31)TS2339:类型“ AuthenticationService”类型上不存在属性“模拟”。

我在这里做错了什么?因为使用了promises,我应该对类/函数进行不同的模拟吗?

1 个答案:

答案 0 :(得分:3)

问题

AuthenticationService的键入不包含mock属性,因此TypeScript会引发错误。


详细信息

jest.mock创建模块的automatic mock,该模块“将ES6类替换为模拟构造函数,并将其所有方法替换为始终返回undefined的模拟函数”。

在这种情况下,default的{​​{1}}导出是ES6类,因此将其替换为模拟构造函数。

模拟构造函数具有authentication.service.ts属性,但是TypeScript对此不了解,并且仍将mock视为原始类型。


解决方案

使用AuthenticationService使TypeScript知道由jest.Mocked引起的键入更改:

jest.mock
相关问题