gebeto / storybook-addon-manual-mocks

Manual Mocks addon for Storybook
https://www.npmjs.com/package/storybook-addon-manual-mocks
MIT License
6 stars 0 forks source link

Relative imports from files in __mocks__ do not resolve correctly #15

Open retwere opened 7 months ago

retwere commented 7 months ago

I have several files in my __mocks__ directory that import other files. In this example, I have files src/foo.ts, src/fooHelper.ts, and src/__mocks__/foo.ts. I want to be able to use the fooHelper in my mock of foo.

src/__mocks__/foo.ts:

import { fooHelper } from '../fooHelper'
// ...

This results in an error:

Internal server error: Failed to resolve import "../fooHelper" from "src/foo.ts". Does the file exist?

The problem is fixed if I change the mock to import ./fooHelper instead of ../fooHelper:

src/__mocks__/foo.ts:

import { fooHelper } from './fooHelper'
// ...

However, this results in errors in my unit tests and typescript build.

fxsalazar commented 6 months ago

I ended up doing

// @ts-expect-error - imports are relative to parent directory, not the mock directory
import { xxx } from '../utils'

is there any other way to circumvent this "issue" ? it's worth noting that the autocomplete (lint) is broken because of this. I wonder how other devs are doing... 🤔