-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26273 from storybookjs/valentin/support-typescrip…
…t-aliases React Docgen: Handle TypeScript path aliases in react-docgen loader
- Loading branch information
Showing
8 changed files
with
201 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
code/frameworks/react-vite/src/plugins/react-docgen.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getReactDocgenImporter } from './react-docgen'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
|
||
const reactDocgenMock = vi.hoisted(() => { | ||
return { | ||
makeFsImporter: vi.fn().mockImplementation((fn) => fn), | ||
}; | ||
}); | ||
|
||
const reactDocgenResolverMock = vi.hoisted(() => { | ||
return { | ||
defaultLookupModule: vi.fn(), | ||
}; | ||
}); | ||
|
||
vi.mock('./docgen-resolver', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('path')>(); | ||
return { | ||
...actual, | ||
defaultLookupModule: reactDocgenResolverMock.defaultLookupModule, | ||
}; | ||
}); | ||
|
||
vi.mock('react-docgen', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('path')>(); | ||
return { | ||
...actual, | ||
makeFsImporter: reactDocgenMock.makeFsImporter, | ||
}; | ||
}); | ||
|
||
describe('getReactDocgenImporter function', () => { | ||
it('should not map the request if a tsconfig path mapping is not available', () => { | ||
const filename = './src/components/Button.tsx'; | ||
const basedir = '/src'; | ||
const imported = getReactDocgenImporter(undefined); | ||
reactDocgenResolverMock.defaultLookupModule.mockImplementation((filen: string) => filen); | ||
const result = (imported as any)(filename, basedir); | ||
expect(result).toBe(filename); | ||
}); | ||
|
||
it('should map the request', () => { | ||
const mappedFile = './mapped-file.tsx'; | ||
const matchPath = vi.fn().mockReturnValue(mappedFile); | ||
const filename = './src/components/Button.tsx'; | ||
const basedir = '/src'; | ||
const imported = getReactDocgenImporter(matchPath); | ||
reactDocgenResolverMock.defaultLookupModule.mockImplementation((filen: string) => filen); | ||
const result = (imported as any)(filename, basedir); | ||
expect(result).toBe(mappedFile); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
code/presets/react-webpack/src/loaders/react-docgen-loader.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { getReactDocgenImporter } from './react-docgen-loader'; | ||
import { describe, it, expect, vi } from 'vitest'; | ||
|
||
const reactDocgenMock = vi.hoisted(() => { | ||
return { | ||
makeFsImporter: vi.fn().mockImplementation((fn) => fn), | ||
}; | ||
}); | ||
|
||
const reactDocgenResolverMock = vi.hoisted(() => { | ||
return { | ||
defaultLookupModule: vi.fn(), | ||
}; | ||
}); | ||
|
||
vi.mock('./docgen-resolver', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('path')>(); | ||
return { | ||
...actual, | ||
defaultLookupModule: reactDocgenResolverMock.defaultLookupModule, | ||
}; | ||
}); | ||
|
||
vi.mock('react-docgen', async (importOriginal) => { | ||
const actual = await importOriginal<typeof import('path')>(); | ||
return { | ||
...actual, | ||
makeFsImporter: reactDocgenMock.makeFsImporter, | ||
}; | ||
}); | ||
|
||
describe('getReactDocgenImporter function', () => { | ||
it('should not map the request if a tsconfig path mapping is not available', () => { | ||
const filename = './src/components/Button.tsx'; | ||
const basedir = '/src'; | ||
const imported = getReactDocgenImporter(undefined); | ||
reactDocgenResolverMock.defaultLookupModule.mockImplementation((filen: string) => filen); | ||
const result = (imported as any)(filename, basedir); | ||
expect(result).toBe(filename); | ||
}); | ||
|
||
it('should map the request', () => { | ||
const mappedFile = './mapped-file.tsx'; | ||
const matchPath = vi.fn().mockReturnValue(mappedFile); | ||
const filename = './src/components/Button.tsx'; | ||
const basedir = '/src'; | ||
const imported = getReactDocgenImporter(matchPath); | ||
reactDocgenResolverMock.defaultLookupModule.mockImplementation((filen: string) => filen); | ||
const result = (imported as any)(filename, basedir); | ||
expect(result).toBe(mappedFile); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters