In jest, how do I use “toHaveBeenCalledWith” and only match part of an object in an array argument?

You can use a combination of arrayContaining and objectContaining to make this work. Reference: https://jestjs.io/docs/expect#expectarraycontainingarray https://jestjs.io/docs/expect#expectobjectcontainingobject Here is some sample code for you: function something(a, b, somefn) { somefn([{ x: a, y: b, id: ‘some-guid’ }]); } test(‘Testing something’, () => { const mockSomeFn = jest.fn(); something(2, 3, mockSomeFn); expect(mockSomeFn).toHaveBeenCalledWith( expect.arrayContaining([ expect.objectContaining({ x: 2, y: … Read more

[Vue warn]: Unknown custom element: – When running jest unit tests

This is how I was able to get rid of the annoying warning: Include RouterLinkStub, eg.: import { shallowMount, createLocalVue, RouterLinkStub } from ‘@vue/test-utils’; Map NuxtLink stub to RouterLinkStub const wrapper = shallowMount(TestItem, { … stubs: { NuxtLink: RouterLinkStub } }) And in case you were checking nuxt-link text or something, change: const link = … Read more

Jest unit test for a debounce function

Actually, you don’t need to use Sinon to test debounces. Jest can mock all timers in JavaScript code. Check out following code (it’s TypeScript, but you can easily translate it to JavaScript): import * as _ from ‘lodash’; // Tell Jest to mock all timeout functions jest.useFakeTimers(); describe(‘debounce’, () => { let func: jest.Mock; let … Read more

Pressing enter to submit form in react-testing-library does not work

The following worked for me: import userEvent from “@testing-library/user-event”; import { render } from “@testing-library/react”; test(“should submit when pressing enter”, () => { const handleSubmit = jest.fn(); const { getByLabelText } = render(<App handleSubmit={handleSubmit} />); const input = getByLabelText(“Name:”); userEvent.type(input, “abc{enter}”); expect(handleSubmit).toHaveBeenCalled(); });

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)