开玩笑期望与fct问题

时间:2020-03-06 15:16:10

标签: node.js unit-testing testing logging jestjs


我对jest有问题。测试winston中对createLogger函数的调用的测试不会触发。
当我导出logger结果填充的createLogger变量时,我不明白为什么我的expect无法正常工作。有人可以告诉我问题出在哪里吗?
感谢:D。

这是我的代码:

// Some code before
const logger = winston.createLogger({
  level: 'debug',
  format: winston.format.combine(
    winston.format.json(),
    winston.format.colorize(),
    winston.format.simple(),
  ),
  transports: process.env.NODE_ENV === 'production' ? [
    new winston.transports.Console(),
    loggingWinston, // Don't worry about this => coming from @google-cloud/logging-winston
  ] : [
    new winston.transports.Console(),
  ],
});
module.exports = logger;

我在runkit上的测试

错误:


● default behavior with production env

    expect(jest.fn()).toHaveBeenCalledTimes(expected)

    Expected number of calls: 1
    Received number of calls: 0

      51 |   process.env.NODE_ENV = 'production';
      52 |
    > 53 |   expect(winston.createLogger).toHaveBeenCalledTimes(1);
         |                                ^
      54 | });
      55 |
      56 | test('default behavior with development env', () => {

      at Object.<anonymous> (src/lib/__tests__/logger.test.js:53:32)

1 个答案:

答案 0 :(得分:1)

无法复制。它对我有用。

logger.js

const winston = require('winston');

const logger = winston.createLogger({
  level: 'debug',
  format: winston.format.combine(winston.format.json(), winston.format.colorize(), winston.format.simple()),
  transports:
    process.env.NODE_ENV === 'production' ? [new winston.transports.Console()] : [new winston.transports.Console()],
});

module.exports = logger;

logger.test.js

jest.mock('winston');
const winston = require('winston');

let mockJson;
let mockColorize;
let mockSimple;
let mockCombine;
let mockConsole;
let mockTransports;
let mockFormat;

beforeEach(() => {
  mockJson = jest.fn(() => 'mock json');
  mockColorize = jest.fn(() => 'mock colorize');
  mockSimple = jest.fn(() => 'mock simple');
  mockCombine = jest.fn();
  mockConsole = jest.fn();
  mockTransports = {
    Console: mockConsole,
  };
  mockFormat = {
    json: mockJson,
    colorize: mockColorize,
    combine: mockCombine,
    simple: mockSimple,
  };
  winston.format = mockFormat;
  winston.transports = mockTransports;
  winston.createLogger.mockImplementation(() => ({ info: jest.fn() }));

  require('./logger');
});

test('default behavior', () => {
  expect(mockTransports.Console).toHaveBeenCalledTimes(1);
  expect(mockFormat.json).toHaveBeenCalledTimes(1);
  expect(mockFormat.colorize).toHaveBeenCalledTimes(1);
  expect(mockFormat.simple).toHaveBeenCalledTimes(1);
  expect(mockFormat.combine).toHaveBeenCalledTimes(1);
  expect(mockFormat.combine).toHaveBeenCalledWith('mock json', 'mock colorize', 'mock simple');
});

test('default behavior with production env', () => {
  process.env.NODE_ENV = 'production';

  expect(winston.createLogger).toHaveBeenCalledTimes(1);
});

test('default behavior with development env', () => {
  process.env.NODE_ENV = 'development';

  expect(winston.createLogger).toHaveBeenCalledTimes(1);
});

具有覆盖率报告的单元测试结果:

 PASS  stackoverflow/60566915/logger.test.js (9.303s)
  ✓ default behavior (776ms)
  ✓ default behavior with production env
  ✓ default behavior with development env (1ms)

-----------|---------|----------|---------|---------|-------------------
File       | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
-----------|---------|----------|---------|---------|-------------------
All files  |     100 |       50 |     100 |     100 |                   
 logger.js |     100 |       50 |     100 |     100 | 7                 
-----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       3 passed, 3 total
Snapshots:   0 total
Time:        10.625s

源代码:https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60566915