Typescript Jest mock: xx.default не является конструктором: невозможно создать экземпляр mock - PullRequest
0 голосов
/ 22 апреля 2020

У меня возникают проблемы при попытке смоделировать класс и конструктор.

У меня есть класс App.ts, который я хочу проверить:

class App {
  public server: Express;

  constructor() {
    this.server = new Express();
    this.server.init();
  }
}

export default App;

Для тестового сценария - > Как только я создаю экземпляр класса App, он должен: - убедиться, что сделан новый экземпляр класса Express - убедиться, что выполнен вызов функции init

Итак, у меня есть файл App.test :

import App from '../App';

let mockedExp: jest.Mock;
jest.mock('../Express', () => {
  return {
    default: jest.fn().mockImplementation(() => {
      return {
        init: mockedExp,
      };
    }),
  };
});

describe('App', () => {
  beforeEach(() => {
    mockedExp = jest.fn().mockImplementation();
    mockedExp.mockClear();
  });

  it('Should call init from express with initialize', () => {
    new App();
    expect(mockedExp).toBeCalled();
  });
});

Когда я запускаю тест, я получаю следующее:

   TypeError: Express_1.default is not a constructor

       8 | 
       9 |   constructor() {
    > 10 |     this.server = new Express();
         |                   ^
      11 |     this.server.init();
      12 |   }

Express класс:

import express from 'express';
import Boom from 'express-boom';
import morgan from 'morgan';

import Locals from './Locals';
import Middleware from './Middleware';
import Logger from './Logger';

export default class Express {
  public app: express.Application;

  constructor() {
    this.app = express();

    // disable the x-powered-by header
    this.app.disable('x-powered-by');

    this.app.locals = Locals.getConfig();
    // add boom
    this.app.use(Boom());

    this.app.set('logger', Logger);

    // morgan logger
    /* instanbul ignore next */
    if (this.app.locals.env === 'production') this.app.use(morgan('combined'));
    else {
      this.app.use(morgan('dev'));
    }
  }

  public init(): void {
    const mid = new Middleware(this.app);
    mid.addLogRoutes();
  }

  public start(): void {
    const server = this.app.listen(this.app.locals.PORT, (error: Error) => {
      if (error) {
        throw new Error(`Unable to start server ${error}`);
      }
      /* istanbul ignore next */
      console.log(`Server starting on ${server.address().port}`);
    });
  }
}

Я использую следующие правила машинописи:

"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"noUnusedLocals": true /* Report errors on unused locals. */,
"noUnusedParameters": true /* Report errors on unused parameters. */,
"noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
"strict": true /* Enable all strict type-checking options. */,
"noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
"strictNullChecks": true,
"strictFunctionTypes": true /* Enable strict checking of function types. */,
"strictBindCallApply": true /* Enable strict 'bind', 'call', and 'apply' methods on functions. */,
"noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
"alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */

Так что я делаю не так?

1 Ответ

1 голос
/ 23 апреля 2020

Вы должны указать __esModule: true в возвращаемом объекте

jest.mock('../Express', () => {
  return {
    __esModule: true,
    default: jest.fn().mockImplementation(() => {
      return {
        init: mockedExp,
      };
    }),
  };
});

В качестве альтернативы, если экспорт по умолчанию является единственным экспортом, его можно вернуть непосредственно с завода:

jest.mock('../Express', () => function() { // arrow function cannot be invoked with `new`
  return { init: mockedExp };
});

// or, if you want to spy on the constructor

jest.mock('../Express', () => jest.fn().mockImplementation(() => ({
  init: mockedExp
})));
...