Garruxx / bun-module-name-mapper

2 stars 0 forks source link

error: ENOENT reading "file:path/to/mock.ts" #1

Open hussain-nz opened 4 months ago

hussain-nz commented 4 months ago

Hello, thank you for creating this plugin! I created /src/tests/bun.plugins.ts file with this content:

import { plugin } from 'bun';
import BunModuelNameMapperPlugin from 'bun-module-name-mapper';

plugin(
  BunModuelNameMapperPlugin({
    '^@/(.*)$': 'src/$1', // this is an example

    // Here is your configuration as it
    // would be in jest’s moduleNameMapper.
    '\\.(jpg|jpeg|png|gif|webp|svg)$': 'tests/mocks/fileMock.ts',
    '\\.css$': 'identity-obj-proxy',
    '^@microsoft/mgt-react': 'tests/mocks/mgtReactMocks.tsx',
    '^lottie-light-react': 'tests/mocks/lottieMocks.tsx',
    'shared/utils/tracking/appInsights': 'tests/mocks/appInsightsMocks.tsx',
    'shared/utils/tracking/appInsightsTelemetry': 'tests/mocks/appInsightsMocks.tsx',
    '^react-i18next': 'tests/mocks/i18nextReactMocks.tsx',
    '.*/useAppTranslation': 'namespaces/translation/mocks/useAppTranslationMock.ts',
    '.*/translation.api': 'src/namespaces/translation/mocks/translation.api.mock.ts',
    i18next: 'tests/mocks/i18nextMocks.ts',
    '^axios': require.resolve('axios'),
    '^uuid': require.resolve('uuid'),
  })
);

However, running a single bun test gives me an error. Bun command: bun test .\src\pages\path\FileBeingTested.test.tsx Error:

error: ENOENT reading "file:src/namespaces/translation/mocks/translation.api.mock.ts"

I tried removing the src/ from here but it has the same error:

'.*/translation.api': 'namespaces/translation/mocks/translation.api.mock.ts',

Error: error: ENOENT reading "file:namespaces/translation/mocks/translation.api.mock.ts"

Any thoughts?

Garruxx commented 4 months ago

Hi! Are you adding the path to your file in the bunfig.toml?

/bunfig.toml preload = ['./src/tests/bun.plugins.ts']

hussain-nz commented 4 months ago

Yup I've added it. I've resorted to using mock.module(param1, param2) and it's kind of working