使用Jest,如何检查模拟函数的参数是否是函数?

bvjveswy  于 5个月前  发布在  Jest
关注(0)|答案(3)|浏览(62)

我在试这个:

expect(AP.require).toBeCalledWith('messages', () => {})

字符串
其中AP.require是一个模拟函数,它应该接收一个字符串和一个函数作为第二个参数。
测试失败,并显示消息:

Expected mock function to have been called with:
  [Function anonymous] as argument 2, but it was called with [Function anonymous]

uttx8gqw

uttx8gqw1#

要Assert任何函数,可以使用expect.any(constructor)
以你为例,它会是这样的:

expect(AP.require).toBeCalledWith('messages', expect.any(Function))

字符串

8yparm6h

8yparm6h2#

问题是函数是一个对象,如果它们不是同一个示例,那么在JavaScript中比较对象将失败

() => 'test' !== () => 'test'

字符串
要解决这个问题,您可以使用mock.calls单独检查参数

const call = AP.require.mock.calls[0] // will give you the first call to the mock
expect(call[0]).toBe('message')
expect(typeof call[1]).toBe('function')

3zwjbxry

3zwjbxry3#

describe('DiariesService', () => {
  let service: DiariesService;

  let diaryModel = {
    countDocuments: jest.fn(),
    find: jest.fn(),
    create: jest.fn(),
    updateOne: jest.fn(),
  }

  let diaryInputModel = {
    create: jest.fn(),
    findOne: jest.fn(),
    find: jest.fn()
  }

  let cs = {
    get: jest.fn()
  }

  let contactsService = {
    findByUser: jest.fn()
  }

  let infoUsersService = {
    findOneByUser: jest.fn(),
  }

  let usersService = {}

  beforeEach(async () => {

    const module: TestingModule = await Test.createTestingModule({
      providers: [DiariesService,
      {
        provide:getModelToken(Diary.name),
        useValue: diaryModel
      },
      {
        provide:getModelToken(DiaryInput.name),
        useValue: diaryInputModel
      },
      {
        provide:ContactsService,
        useValue: contactsService
      },
      {
        provide: ConfigService,
        useValue: cs
      },
      {
        provide:InfoUsersService,
        useValue: infoUsersService
      },
      {
        provide:UsersService,
        useValue: usersService
      },

    ],
    }).compile();

    service = module.get<DiariesService>(DiariesService);
  });

  const userObject : User = {
    _id: "",
    firebase_token: "",
    authenticator_id: "", 
    created_at: new Date(),
    updated_at: new Date(), 
    deleted_at: null, 
  };

  const userAuthOutPut: AuthOutput = {
    "accessToken": "",
    "user": {
      "id": "",
      "avatar": "",
      "username": "",
      "email": "",
      "phone": "",
      "firstName": "",
      "lastName": ""
    },
    "userLocal": userObject
  }
  

  it('getDiaryInput', async () => {
    const filter = {
      userId: userObject._id,
      state: StatusType.OPEN,
      deleted_at: { $exists: false },
    }

    const filterCreate = {state: StatusType.OPEN, userId: userObject._id }
    diaryInputModel.create.mockResolvedValue(filterCreate)
    diaryInputModel.findOne.mockImplementation(() =>  {
      return {
        exec: jest.fn().mockReturnValue(filter),
      };
  })

    await service.getDiaryInput(userObject)   

    expect(diaryInputModel.create).toHaveBeenCalledWith(filterCreate);
    expect(diaryInputModel.findOne).toHaveBeenCalledWith(filter);

  });
});

字符串
[在此输入图像描述][1]
me da ese error cuando corro el test,quien me dice que es [1]:https://i.stack.imgur.com/EyMVS.png

相关问题