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

Jest Difference Between toBeCalledWith and toHaveBeenCalledWith

From the docs https://jestjs.io/docs/en/expect#tohavebeencalledwitharg1-arg2- .toHaveBeenCalledWith(arg1, arg2, …) under the alias: .toBeCalledWith() From the source code: https://github.com/facebook/jest/blob/b7cb5221bb06b6fe63c1a5e725ddbc1aaa82d306/packages/expect/src/spyMatchers.ts#L1128 https://github.com/facebook/jest/blob/b7cb5221bb06b6fe63c1a5e725ddbc1aaa82d306/packages/expect/src/spyMatchers.ts#L1131 //… toBeCalledWith: createToBeCalledWithMatcher(‘toBeCalledWith’), toHaveBeenCalled: createToBeCalledMatcher(‘toHaveBeenCalled’), toHaveBeenCalledTimes: createToBeCalledTimesMatcher(‘toHaveBeenCalledTimes’), toHaveBeenCalledWith: createToBeCalledWithMatcher(‘toHaveBeenCalledWith’), //… They are created by the createToBeCalledWithMatcher function with only a different name. So, they are the same. UPDATE: Here is my personal understanding of why jestjs provide these … Read more

Testing two environments with jest

EDIT (Jan 2018): It is now possible to do so (since Jest v20), and the option is called projects. Read more about it the docs. Basically you can define an array of your projects you want Jest to be run within: { “projects”: [“<rootDir>/client”, “<rootDir>/server”, “<rootDir>/some-glob/*”] } Just remember every project needs to have its … Read more

Is there a jest config that will fail tests on console.warn?

You can use this simple override : let error = console.error console.error = function (message) { error.apply(console, arguments) // keep default behaviour throw (message instanceof Error ? message : new Error(message)) } You can make it available across all tests using Jest setupFiles. In package.json : “jest”: { “setupFiles”: [ “./tests/jest.overrides.js” ] } Then put … Read more

Jest coverage: How can I get a total percentage of coverage?

Thanks to Teneff’s answer, I go with the coverageReporter=”json-summary”. jest –coverage –coverageReporters=”json-summary” This generates a coverage-summary.json file which can easily be parsed. I get the total values directly from the json: “total”: { “lines”: { “total”: 21777, “covered”: 65, “skipped”: 0, “pct”: 0.3 }, “statements”: { “total”: 24163, “covered”: 72, “skipped”: 0, “pct”: 0.3 }, … Read more

Jest+React Native Testing Library: How to test an image src?

If you’re using @testing-library/react rather than @testing-library/react-native, and you have an alt attribute on your image, you can avoid using getByDataTestId and instead use getByAltText. it(‘uses correct src’, async () => { const { getByAltText } = await render(<MyComponent />); const image = getByAltText(‘the_alt_text’); expect(image.src).toContain(‘the_url’); // or expect(image).toHaveAttribute(‘src’, ‘the_url’) }); Documentation. Unfortunately, it appears that … Read more

How to publish Jest Unit Test Results in VSTS tests?

I’ve used a different approach, b/c after some research I found that the Jest testResultsProcessor property is deprecated. I’m using the jest-junit package for test reports (which has been worked on more recently than the jest-trx-results-processor, fwiw): Add jest-junit to package.json Eg yarn add -D jest-junit or npm add –save-dev jest-junit Add a VSTS task … Read more

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