algoliasearch-client-javascript icon indicating copy to clipboard operation
algoliasearch-client-javascript copied to clipboard

TypeError When Running AlgoliaSearch inside Jest

Open KempfCreative opened this issue 4 years ago • 1 comments

When running jest tests on components which are using the default export of algoliasearch, algolia appears to try and make requests out to the web to pull down its scripts. This causes a TypeError as the test environment is not supporting this:

FAIL  tissues/navigation-header/test.js
  ● Test suite failed to run

    TypeError: Cannot read property 'protocol' of undefined

      4 | const apiKey = process.env.NEXT_PUBLIC_ALGOLIA_SEARCH_API_KEY;
      5 |
    > 6 | export default algoliasearch(appId, apiKey);
        |                ^
      7 |

      at createStatelessHost (node_modules/@algolia/transporter/dist/transporter.cjs.js:71:27)
      at transporter.hosts.hosts.map.host (node_modules/@algolia/transporter/dist/transporter.cjs.js:251:34)
          at Array.map (<anonymous>)
      at Object.createTransporter (node_modules/@algolia/transporter/dist/transporter.cjs.js:251:22)
      at Object.createSearchClient (node_modules/@algolia/client-search/dist/client-search.cjs.js:49:39)
      at algoliasearch (node_modules/algoliasearch/dist/algoliasearch.cjs.js:43:25)
      at Object.<anonymous> (utils/algolia.js:6:16)
      at Object.<anonymous> (tissues/algolia-search/index.js:24:1)
      at Object.<anonymous> (tissues/navigation-header/index.js:13:1)
      at Object.<anonymous> (tissues/navigation-header/test.js:7:1)

This is currently blocking us from accurately testing algoliasearch components.

KempfCreative avatar Sep 03 '21 20:09 KempfCreative

Can you reproduce this in a standalone project? I think protocol only exists on the hosts:

https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/transporter/src/types/HostOptions.ts#L19

So my guess is that you call algoliasearch('', '', { hosts: [undefined] }) or a similar form to that

my guess of the stack trace: https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/transporter/src/createStatelessHost.ts#L13 https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/transporter/src/createTransporter.ts#L36 https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/transporter/src/createTransporter.ts#L21 https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/client-search/src/createSearchClient.ts#L26-L35 or https://github.com/algolia/algoliasearch-client-javascript/blob/3aec216a9704f818b4faaa8b97d988ec2794f9da/packages/client-search/src/createSearchClient.ts#L36

Haroenv avatar Sep 06 '21 07:09 Haroenv