Repository: storybook
Issue: [Bug]: Addon-jest fails on Safari/WebKit versions below v16.4
Labels: bug,addon: jest,sev:S3

### Describe the bug

`@storybook/addon-jest` fails to render any stories when opening in Safari 16.3 or below. It fails with the following error in the preview:

```
Invalid regular expression: invalid group specifier name


RegExp@[native code]
toUnix@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:475:31
map@[native code]
@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:485:28
@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:643:110
find@[native code]
@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:643:70
map@[native code]
emitAddTests@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:648:106
@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/@storybook_addon-jest.js?v=544a63f9:654:46
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
hookified@http://localhost:6006/sb-preview/runtime.js:36:10849
@http://localhost:6006/sb-preview/runtime.js:36:11659
renderWithHooks@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:12171:35
mountIndeterminateComponent@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:14921:36
beginWork$1@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:19749:31
performUnitOfWork@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:19194:31
workLoopSync@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:19133:30
renderRootSync@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:19112:27
recoverFromConcurrentError@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:18732:42
performConcurrentWorkOnRoot@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:18680:56
performConcurrentWorkOnRoot@[native code]
workLoop@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:197:50
flushWork@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:176:30
performWorkUntilDeadline@http://localhost:6006/node_modules/.cache/storybook/1c3385a5d25e538d10b518b310c74d3ca2690b6aaffeadccd74da79736171f86/sb-vite/deps/chunk-HTOLHPKM.js?v=544a63f9:384:50
```

It uses the `upath` library to sanitize paths, however that library uses [the lookbehind regex syntax](https://github.com/anodynos/upath/blob/master/source/code/upath.coffee#L13-L16) which is [not supported until Safari 16.4 an onwards](https://caniuse.com/js-regexp-lookbehind).

ESBuild should compile syntaxes to support older target (Storybook 7 should support Safari 15) but [it doesn't support compiling regexes](https://github.com/evanw/esbuild/issues/3262).
It [_should_ however compile regexes from `/regex/` to `new RegExp("regex")`](https://esbuild.github.io/content-types/) but it doesn't currently for some reason.

### To Reproduce

See reproduction at https://github.com/JReinhold/storybook-reproduction-25472

1. Clone the repo
2. `pnpm install`
3. `pnpm storybook --no-open`
4. Open the Button/Primary story using Safari/WebKit 16.3 or below (this will be hard, I don't know of a way to get old Safari versions other than creating a VM).
5. See the following error:
![image](https://github.com/storybookjs/storybook/assets/5678122/03f678e3-fcf5-48e9-895c-a8369de1019d)

Note the reproduction requires the workaround described in https://github.com/storybookjs/storybook/issues/14856, it seems that `addon-jest` is completely broken in Webpack5/Vite based projects without it.

### System

```bash
Storybook Environment Info:

  System:
    OS: macOS 13.1
    CPU: (10) arm64 Apple M1 Pro
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.18.2 - ~/Library/Caches/fnm_multishells/90078_1704442956065/bin/node
    Yarn: 1.22.19 - ~/Library/Caches/fnm_multishells/90078_1704442956065/bin/yarn
    npm: 9.8.1 - ~/Library/Caches/fnm_multishells/90078_1704442956065/bin/npm <----- active
    pnpm: 8.9.2 - ~/Library/Caches/fnm_multishells/90078_1704442956065/bin/pnpm
  Browsers:
    Chrome: 119.0.6045.123
    Edge: 120.0.2210.91
    Safari: 16.2
  npmPackages:
    @storybook/addon-essentials: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/addon-interactions: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/addon-jest: 8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/addon-links: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/addon-onboarding: ^1.0.10 => 1.0.10
    @storybook/blocks: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/react: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/react-vite: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    @storybook/test: ^8.0.0-alpha.7 => 8.0.0-alpha.7
    storybook: ^8.0.0-alpha.7 => 8.0.0-alpha.7
```


### Additional context

Given that Chromatic internally currently runs Safari 16.0, using `@storybook/addon-jest` will cause all Safari snapshots to fail.

A workaround for this would be to conditionally apply the `withTests` decorator only when `isChromatic()` is false, to disable the jest addon during snapshots.

This workaround can be seen in the reproduction:

https://github.com/JReinhold/storybook-reproduction-25472/blob/main/.storybook/preview.js#L15-L18

User reports suggests this bug was introduced in [v7.2.3](https://github.com/storybookjs/storybook/releases/tag/v7.2.3) but I've been unable to confirm this, using v7.2.2 fails with the same error locally.