Best JavaScript code snippet using jest-extended
request.test.js
Source:request.test.js  
...36    afterAll(() => {37        jest.restoreAllMocks();38    });39    test('Requesting partners data calls HttpClient fetch with correct method, route, and image population parameter', async () => {40        await expect(requestService.getPartners()).resolves.toContainKeys(['data', 'meta']);41        expect(httpClientFetchMock).toBeCalledTimes(1);42        43        // test calling with 'id' argument44        await requestService.getPartners(1);45        expect(httpClientFetchMock).toBeCalledTimes(2);46        expect(httpClientFetchMock).nthCalledWith(1, 47            expect.stringMatching(/\/api\/partners\?.*?populate=.*?image/), { method: 'GET' });48        expect(httpClientFetchMock).nthCalledWith(2, 49            expect.stringMatching(/\/api\/partners\/1\?.*?populate=.*?image/), { method: 'GET' });50    });51    test('Requesting projects data calls HttpClient fetch with correct method, route, and image population parameter', async () => {52        await expect(requestService.getProjects()).resolves.toContainKeys(['data', 'meta']);53        expect(httpClientFetchMock).toBeCalledTimes(1);54        // test calling with 'id' argument55        await requestService.getProjects(1);56        expect(httpClientFetchMock).toBeCalledTimes(2);57        expect(httpClientFetchMock).nthCalledWith(1, 58            expect.stringMatching(/\/api\/projects\?.*?populate=.*?image/), { method: 'GET' });59        expect(httpClientFetchMock).nthCalledWith(2, 60            expect.stringMatching(/\/api\/projects\/1\?.*?populate=.*?image/), { method: 'GET' });61    });62    test('Requesting teams data calls HttpClient fetch with correct method, route, and image population parameter', async () => {63        await expect(requestService.getTeams()).resolves.toContainKeys(['data', 'meta']);64        expect(httpClientFetchMock).toBeCalledTimes(1);65        // test calling with 'id' argument66        await requestService.getTeams(1);67        expect(httpClientFetchMock).toBeCalledTimes(2);68        expect(httpClientFetchMock).nthCalledWith(1, 69            expect.stringMatching(/\/api\/teams\?.*?populate=.*?image/), { method: 'GET' });70        expect(httpClientFetchMock).nthCalledWith(2, 71            expect.stringMatching(/\/api\/teams\/1\?.*?populate=.*?image/), { method: 'GET' });72    });73    test('Requesting events data calls HttpClient fetch with correct method, route, and image population parameter', async () => {74        await expect(requestService.getEvents()).resolves.toContainKeys(['data', 'meta']);75        expect(httpClientFetchMock).toBeCalledTimes(1);76        // test calling with 'id' argument77        await requestService.getEvents(1);78        expect(httpClientFetchMock).toBeCalledTimes(2);79        expect(httpClientFetchMock).nthCalledWith(1, 80            expect.stringMatching(/\/api\/events\?.*?populate=.*?image/), { method: 'GET' });81        expect(httpClientFetchMock).nthCalledWith(2, 82            expect.stringMatching(/\/api\/events\/1\?.*?populate=.*?image/), { method: 'GET' });83    })84    test('Requesting blogs data calls HttpClient fetch with correct method, route, and image population parameter', async () => {85        await expect(requestService.getBlogs()).resolves.toContainKeys(['data', 'meta']);86        expect(httpClientFetchMock).toBeCalledTimes(1);87        // test calling with 'id' argument88        await requestService.getBlogs(1);89        expect(httpClientFetchMock).toBeCalledTimes(2);90        expect(httpClientFetchMock).nthCalledWith(1, 91            expect.stringMatching(/\/api\/blogs\?.*?populate=.*?image/), { method: 'GET' });92        expect(httpClientFetchMock).nthCalledWith(2, 93            expect.stringMatching(/\/api\/blogs\/1\?.*?populate=.*?image/), { method: 'GET' });94    });95    test('Requesting members data calls HttpClient fetch with correct method, route, and image population parameter', async () => {96        await expect(requestService.getMembers()).resolves.toContainKeys(['data', 'meta']);97        expect(httpClientFetchMock).toBeCalledTimes(1);98        // test calling with 'id' argument99        await requestService.getMembers(1);100        expect(httpClientFetchMock).toBeCalledTimes(2);101        expect(httpClientFetchMock).nthCalledWith(1, 102            expect.stringMatching(/\/api\/members\?.*?populate=.*?image/), { method: 'GET' });103        expect(httpClientFetchMock).nthCalledWith(2, 104            expect.stringMatching(/\/api\/members\/1\?.*?populate=.*?image/), { method: 'GET' });105    });106    test('Requesting logo data calls HttpClient fetch with correct method, route, and image population parameter', async () => {107        await expect(requestService.getLogo()).resolves.toContainKeys(['data', 'meta']);108        expect(httpClientFetchMock).toBeCalledTimes(1);109        // test calling with 'id' argument110        await requestService.getLogo(1);111        expect(httpClientFetchMock).toBeCalledTimes(2);112        expect(httpClientFetchMock).nthCalledWith(1, 113            expect.stringMatching(/\/api\/logos\?.*?populate=.*?image/), { method: 'GET' });114        expect(httpClientFetchMock).nthCalledWith(2, 115            expect.stringMatching(/\/api\/logos\/1\?.*?populate=.*?image/), { method: 'GET' });116    });117});118// Unit testing http.js with mocks119describe('http.js test with mocked API call', () => {120    121    let globalFetchMock;122    test("HttpClient's fetch calls global fetch with the correct domain, method, route, image population parameter and retrieve JSON data correctly",123     async () => {124        const baseURL = process.env.REACT_APP_BASE_URL;125        // Mock global fetch to succeed126        globalFetchMock = jest.spyOn(global, 'fetch').mockImplementation(async(input, init) => {127            sleep(100);128            return {129                json: () => {130                    return fetchJsonDataMock;131                },132                status: 200,133            }134        });135        await expect(requestService.getPartners()).resolves.toContainKeys(['data','meta']);136        expect(globalFetchMock).toBeCalledTimes(1);137        expect(globalFetchMock).lastCalledWith(138            expect.stringMatching(new RegExp(baseURL + /\/api\/partners\?.*?populate=.*?image/.source)),139            expect.objectContaining({140                method: 'GET',141            })142        );143        await expect(requestService.getProjects()).resolves.toContainKeys(['data','meta']);144        expect(globalFetchMock).toBeCalledTimes(2);145        expect(globalFetchMock).lastCalledWith(146            expect.stringMatching(new RegExp(baseURL + /\/api\/projects\?.*?populate=.*?image/.source)),147            expect.objectContaining({148                method: 'GET',149            })150        );151        await expect(requestService.getEvents()).resolves.toContainKeys(['data','meta']);152        expect(globalFetchMock).toBeCalledTimes(3);153        expect(globalFetchMock).lastCalledWith(154            expect.stringMatching(new RegExp(baseURL + /\/api\/events\?.*?populate=.*?image/.source)),155            expect.objectContaining({156                method: 'GET',157            })158        );159        await expect(requestService.getTeams()).resolves.toContainKeys(['data','meta']);160        expect(globalFetchMock).toBeCalledTimes(4);161        expect(globalFetchMock).lastCalledWith(162            expect.stringMatching(new RegExp(baseURL + /\/api\/teams\?.*?populate=.*?image/.source)),163            expect.objectContaining({164                method: 'GET',165            })166        );167        await expect(requestService.getMembers()).resolves.toContainKeys(['data','meta']);168        expect(globalFetchMock).toBeCalledTimes(5);169        expect(globalFetchMock).lastCalledWith(170            expect.stringMatching(new RegExp(baseURL + /\/api\/members\?.*?populate=.*?image/.source)),171            expect.objectContaining({172                method: 'GET',173            })174        );175        await expect(requestService.getBlogs()).resolves.toContainKeys(['data','meta']);176        expect(globalFetchMock).toBeCalledTimes(6);177        expect(globalFetchMock).lastCalledWith(178            expect.stringMatching(new RegExp(baseURL + /\/api\/blogs\?.*?populate=.*?image/.source)), 179            expect.objectContaining({180                method: 'GET',181            })182        );183        await expect(requestService.getLogo()).resolves.toContainKeys(['data','meta']);184        expect(globalFetchMock).toBeCalledTimes(7);185        expect(globalFetchMock).lastCalledWith(186            expect.stringMatching(new RegExp(baseURL + /\/api\/logos\?.*?populate=.*?image/.source)),187            expect.objectContaining({188                method: 'GET',189            })190        );191    });192    193    test("Http's client fetch return undefined if response status code indicates failure", async () => {194        // Mock global fetch to fail195        globalFetchMock = jest.spyOn(global, 'fetch').mockImplementation(async(input, init) => {196            sleep(100);197            return {...main.js
Source:main.js  
...8    var object = {9      foo: 1,10      bar: 211    };12    expect(object).toContainKeys({ foo: null });13    expect(object).toContainKeys({ bar: null });14    expect(object).toContainKeys({ foo: null, bar: null });15  });16  it('toContainKeys should accept valid Arrays with keys', function() {17    var object = {18      foo: 1,19      bar: 220    };21    expect(object).toContainKeys(['foo']);22    expect(object).toContainKeys(['bar']);23    expect(object).toContainKeys(['foo','bar']);24  });25  it('toContainValues should accept valid Objects', function() {26    var object = {27      foo: 1,28      bar: 229    };30    expect(object).toContainValues({ foo: 1 });31    expect(object).toContainValues({ bar: 2 });32    expect(object).toContainValues({ foo: 1, bar: 2 });33  });34  // Direct testing of expected failure conditions35  it('toContainKeys should reject invalid Objects with keys', function() {36    var result = JasmineObjectMatchers.toContainKeys({foo: 1}, {bar: 1});37    expect(result.pass).toBe(false);38  });39  it('toContainKeys should reject invalid Arrays with keys', function() {40    var result = JasmineObjectMatchers.toContainKeys({foo: 1}, ['bar']);41    expect(result.pass).toBe(false);42  });43  it('toContainValues should reject missing keys', function() {44    var result = JasmineObjectMatchers.toContainValues({foo: 1}, {bar: 1});45    expect(result.pass).toBe(false);46  });47  it('toContainValues should reject keys with incorrect values', function() {48    var result = JasmineObjectMatchers.toContainValues({foo: 1}, {foo: 2});49    expect(result.pass).toBe(false);50  });...to-contain-key-spec.js
Source:to-contain-key-spec.js  
...4      expect({ a: 1, b: 2, c: 3 }).toContainKey('a');5      expect({ a: null }).toContainKey('a');6    });7    it("passes when the keys are contained", function() {8      expect({ a: 1, b: 2, c: 3 }).toContainKeys('a', 'b');9      expect({ a: 1, b: 2, c: 3 }).toContainKeys(['a', 'b']);10    });11    it("returns `false` when a key is missing", function() {12      expect({ a: 1, b: 2, c: 3 }).not.toContainKey('d');13      expect({ a: 1, b: 2, c: 3 }).not.toContainKeys('a', 'b', 'd');14      expect({ a: 1, b: 2, c: 3 }).not.toContainKeys(['a', 'b', 'd']);15    });16  });17  it("fails with non object", function() {18    expect([]).not.toContainKey('key');19    expect(false).not.toContainKey('0');20    expect(true).not.toContainKey('1');21  });...Using AI Code Generation
1const { toContainKeys } = require('jest-extended');2expect.extend({ toContainKeys });3const { toContainAllKeys } = require('jest-extended');4expect.extend({ toContainAllKeys });5const { toContainAnyKeys } = require('jest-extended');6expect.extend({ toContainAnyKeys });7const obj = {8};9describe('toContainKeys', () => {10  test('checks if an object has a given key', () => {11    expect(obj).toContainKeys(['name']);12  });13});14describe('toContainAllKeys', () => {15  test('checks if an object has all the given keys', () => {16    expect(obj).toContainAllKeys(['name', 'age']);17  });18});19describe('toContainAnyKeys', () => {20  test('checks if an object has any of the given keys', () => {21    expect(obj).toContainAnyKeys(['name', 'country']);22  });23});24    ✓ checks if an object has a given key (2ms)25    ✓ checks if an object has all the given keys (1ms)26    ✓ checks if an object has any of the given keys (1ms)27Recommended Posts: Jest - toBeEmpty()28Jest - toBeEmptyArray()29Jest - toBeEmptyObject()30Jest - toBeArrayOfSize()31Jest - toBeNonEmptyArray()32Jest - toBeNonEmptyObject()33Jest - toBeArray()34Jest - toBeObject()35Jest - toBeString()36Jest - toBeNumber()37Jest - toBeBoolean()38Jest - toBeTrue()39Jest - toBeFalse()40Jest - toBeNull()41Jest - toBeUndefined()42Jest - toBeNaN()43Jest - toBeInteger()Using AI Code Generation
1const { toContainKeys } = require('jest-extended');2expect.extend({ toContainKeys });3test('object contains keys', () => {4  expect({ a: 1, b: 2 }).toContainKeys(['a', 'b']);5  expect({ a: 1, b: 2 }).not.toContainKeys(['a', 'c']);6});7const { toContainAllKeys } = require('jest-extended');8expect.extend({ toContainAllKeys });9test('object contains all keys', () => {10  expect({ a: 1, b: 2 }).toContainAllKeys(['a', 'b']);11  expect({ a: 1, b: 2 }).not.toContainAllKeys(['a', 'c']);12});13const { toContainAnyKeys } = require('jest-extended');14expect.extend({ toContainAnyKeys });15test('object contains any keys', () => {16  expect({ a: 1, b: 2 }).toContainAnyKeys(['a', 'c']);17  expect({ a: 1, b: 2 }).not.toContainAnyKeys(['c', 'd']);18});19const { toHaveArrayOfSize } = require('jest-extended');20expect.extend({ toHaveArrayOfSize });21test('array has size', () => {22  expect([1, 2, 3]).toHaveArrayOfSize(3);23  expect([1, 2, 3]).not.toHaveArrayOfSize(4);24});25const { toHaveArray } = require('jest-extended');26expect.extend({ toHaveArray });27test('array has value', () => {28  expect([1, 2, 3]).toHaveArray(2);29  expect([1, 2, 3]).not.toHaveArray(4);30});31const { toHaveArrayOfBooleans } = require('jest-extended');32expect.extend({ toHaveArrayOfBooleans });33test('array has value', () => {34  expect([trueUsing AI Code Generation
1expect({a: 1, b: 2}).toContainKeys(['a', 'b']);2expect({a: 1, b: 2}).not.toContainKeys(['a', 'b', 'c']);3expect({a: 1, b: 2}).toContainKey('a');4expect({a: 1, b: 2}).not.toContainKey('c');5expect({a: 1, b: 2}).toContainAllKeys(['a', 'b']);6expect({a: 1, b: 2}).not.toContainAllKeys(['a', 'b', 'c']);7expect({a: 1, b: 2}).toContainAnyKeys(['a', 'b', 'c']);8expect({a: 1, b: 2}).not.toContainAnyKeys(['c', 'd']);9expect({a: 1, b: 2}).toContainValue(1);10expect({a: 1, b: 2}).not.toContainValue(3);11expect({a: 1, b: 2}).toContainAllValues([1, 2]);12expect({a: 1, b: 2}).not.toContainAllValues([1, 2, 3]);13expect({a: 1, b: 2}).toContainAnyValues([1, 3]);14expect({a: 1, b: 2}).not.toContainAnyValues([3, 4]);15expect({}).toBeEmpty();16expect({a: 1, b: 2}).not.toBeEmpty();17expect({a: 1, b: 2}).toBeNonEmptyObject();18expect({}).not.toBeNonEmptyObject();19expect({}).toBeObject();20expect([]).not.toBeObject();Using AI Code Generation
1const { toContainKeys } = require('jest-extended');2expect.extend({3});4test('should pass', () => {5  expect({6  }).toContainKeys(['a', 'b']);7});8test('should fail', () => {9  expect({10  }).toContainKeys(['a', 'b', 'c']);11});Using AI Code Generation
1const { toContainKeys } = require('jest-extended');2expect.extend({ toContainKeys });3test('test toContainKeys method', () => {4  const myObj = { a: 1, b: 2, c: 3 };5  expect(myObj).toContainKeys(['a', 'b']);6});7  ✓ test toContainKeys method (2ms)Using AI Code Generation
1const { toContainKeys } = require('jest-extended');2expect.extend({ toContainKeys });3const obj = {4  address: {5  },6};7expect(obj).toContainKeys(['name', 'age']);8expect(obj).toContainKeys(['name', 'age', 'address']);9expect(obj).toContainKeys(['name', 'age', 'address.city']);10expect(obj).not.toContainKeys(['name', 'age', 'address.city', 'address.country']);11✓ to contain all keys (2ms)12✓ to contain all keys (1ms)13✓ to contain all keys (1ms)14✓ to not contain all keys (1ms)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
