browserslist/browserslist-useragent-regexp

Unable to import from Jest tests: Cannot find module 'browserslist-useragent-regexp'

intgr opened this issue · 2 comments

intgr commented

Describe your question

With browserslist-useragent-regexp version 3.0.2, I was able to import and use the API in a Jest unit test directly:

import {getUserAgentRegExp} from 'browserslist-useragent-regexp';

I notice that the function name changed in 4.0.0, but also importing it no longer works from Jest:

import {getUserAgentRegex} from 'browserslist-useragent-regexp';

Output:

  ● Test suite failed to run

    Cannot find module 'browserslist-useragent-regexp' from 'src/.../supportedBrowsers.test.ts'

      2 |
    > 3 | import {getUserAgentRegex} from 'browserslist-useragent-regexp';
        | ^
      4 |
      5 | /**

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (.../supportedBrowsers.test.ts:3:1)

Nor did using different syntax const {getUserAgentRegex} = await import('browserslist-useragent-regexp'); make it work.

However, using Node REPL it seems fine:

Welcome to Node.js v18.11.0.
Type ".help" for more information.
> const {getUserAgentRegex} = await import('browserslist-useragent-regexp');
undefined
> 
> getUserAgentRegex
[Function: getUserAgentRegex]

I am not sure if there is an issue with browserslist-useragent-regexp or whether this is something weird in my project, but in any case this issue appeared with the version change.

@intgr Hi. browserslist-useragent-regexp v4 is ESM-only package. You should follow this doc to enable ESM support in jest. Or, I can recommend you migrate to vitest. Vitest has ESM support out of the box and has almost the same API as jest. My example of migration: reactchartjs/react-chartjs-2@7f3ec96

intgr commented

Thanks for the reply. Vitest seems worth considering.