Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError When Running AlgoliaSearch inside Jest #1303

Open
KempfCreative opened this issue Sep 3, 2021 · 1 comment
Open

TypeError When Running AlgoliaSearch inside Jest #1303

KempfCreative opened this issue Sep 3, 2021 · 1 comment

Comments

@KempfCreative
Copy link

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.

@Haroenv
Copy link
Contributor

Haroenv commented Sep 6, 2021

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

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

my guess of the stack trace:


hosts: hosts.map(host => createStatelessHost(host)),


hosts: ([
{ url: `${appId}-dsn.algolia.net`, accept: CallEnum.Read },
{ url: `${appId}.algolia.net`, accept: CallEnum.Write },
] as readonly HostOptions[]).concat(
shuffle([
{ url: `${appId}-1.algolianet.com` },
{ url: `${appId}-2.algolianet.com` },
{ url: `${appId}-3.algolianet.com` },
])
),
or

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants