繁体   English   中英

Jest fn 用作 object 属性时未定义

[英]Jest fn undefined when used as an object property

我有一个 class 我正在尝试模拟 object 及其属性

intercept(context: ExecutionContext) {
  const response = contect.switchToHttp().getResponse() // need to mock this chain

  if (response.headersSent) { // to test this path
    return true
  }

  return false
}

如果我使用普通的 object 文字和一些匿名函数“模拟”依赖项,一切都会按预期工作

const executionContext = {
  switchToHttp: () => executionContext, // calls itself to simulate 'this' chaining
  getRequest: () => {
    return {
      url: undefined,
      method: undefined,
      headers: undefined,
      connection: {
        remoteAddress: undefined,
        remotePort: undefined
      }
    }
  },
  getResponse: () => {
    return {
      headersSent: true, // but i want an easy way to change this in tests without using a factory
      append: () => undefined
    }
  }
} as unknown as ExecutionContext

it('test', () => {
  const response = myclass.intercept(executionContext);

  expect(response).toBeTrue()
});

当我尝试使用jest.fn()模拟一些属性时,我得到了奇怪的结果。

const getResponseSpy = jest.fn(() => {
  return {
    headersSent: false,
    append: () => undefined
  }
});

const executionContext = {
  switchToHttp: () => executionContext,
  getRequest: () => {
    return {
      url: undefined,
      method: undefined,
      headers: undefined,
      connection: {
        remoteAddress: undefined,
        remotePort: undefined
      }
    }
  },
  getResponse: getResponseSpy // easier way to change this
} as unknown as ExecutionContext

此时在我的代码中,我得到一个undefined的响应

TypeError: Cannot read property 'headersSent' of undefined

如果我执行getResponse: () => getResponseSpy之类的操作,那么我的代码中的response是 Jest 模拟 object 而不是模拟实现。 这当然缺少headersSent属性。

我觉得我在做一些基本的错误。 我试过使用

switchToHttp: jest.fn().mockResturnThis()

但这并没有改变任何东西。 object 内部的玩笑间谍似乎没有返回其模拟实现

我究竟做错了什么?

mockFn.mockReturnThis()应该可以工作。

例如

index.ts

interface ExecutionContext {
  switchToHttp(): ExecutionContext;
  getResponse(): ExecutionContext;
  headersSent: boolean;
}

export const myclass = {
  intercept(context: ExecutionContext) {
    const response = context.switchToHttp().getResponse();

    if (response.headersSent) {
      return true;
    }

    return false;
  },
};

index.test.ts

import { myclass } from './';

describe('67837058', () => {
  it('should pass', () => {
    const executionContext = {
      switchToHttp: jest.fn().mockReturnThis(),
      getResponse: jest.fn().mockReturnThis(),
      headersSent: true,
    };
    const response = myclass.intercept(executionContext);
    expect(response).toBeTruthy();
  });
});

测试结果:

 PASS  examples/67837058/index.test.ts (8.387 s)
  67837058
    ✓ should pass (3 ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |      80 |       50 |     100 |      80 |                   
 index.ts |      80 |       50 |     100 |      80 | 15                
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        9.273 s

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM