Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use next.js internal paths instead of *.actual paths #26844

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,13 +412,7 @@

#### @storybook/nextjs requires specific path aliases to be setup

In order to properly mock the `next/router`, `next/header`, `next/navigation` and `next/cache` APIs, the `@storybook/nextjs` framework includes internal Webpack aliases to those modules. If you use portable stories in your Jest configuration, you will run into the following issue:

```
Cannot find module 'next/navigation.actual' from 'node_modules/@storybook/nextjs/dist/export-mocks/navigation/index.js'
```

To fix it, you should set the aliases in your Jest config files `moduleNameMapper` property using the `getPackageAliases` helper from `@storybook/nextjs/export-mocks`:
In order to properly mock the `next/router`, `next/header`, `next/navigation` and `next/cache` APIs, the `@storybook/nextjs` framework includes internal Webpack aliases to those modules. If you use portable stories in your Jest tests, you should set the aliases in your Jest config files `moduleNameMapper` property using the `getPackageAliases` helper from `@storybook/nextjs/export-mocks`:

```js
const nextJest = require("next/jest.js");
Expand All @@ -432,6 +426,8 @@ const customJestConfig = {
module.exports = createJestConfig(customJestConfig);
```

This will make sure you end using the correct implementation of the packages and avoid having issues in your tests.

## From version 7.x to 8.0.0

### Portable stories
Expand Down
4 changes: 0 additions & 4 deletions code/frameworks/nextjs/src/cache/index.ts

This file was deleted.

19 changes: 14 additions & 5 deletions code/frameworks/nextjs/src/export-mocks/cache/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import { fn } from '@storybook/test';

// re-exports of the actual module
export * from 'next/cache.actual';
import { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache';
import { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store';

// mock utilities/overrides (as of Next v14.2.0)
export const revalidatePath = fn().mockName('revalidatePath');
export const revalidateTag = fn().mockName('revalidateTag');
const revalidatePath = fn().mockName('revalidatePath');
const revalidateTag = fn().mockName('revalidateTag');

const cacheExports = {
unstable_cache,
revalidateTag,
revalidatePath,
unstable_noStore,
};

export default cacheExports;
export { unstable_cache, revalidateTag, revalidatePath, unstable_noStore };
4 changes: 2 additions & 2 deletions code/frameworks/nextjs/src/export-mocks/headers/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { fn } from '@storybook/test';
import * as originalHeaders from 'next/headers.actual';
import * as originalHeaders from 'next/dist/client/components/headers';

// re-exports of the actual module
export * from 'next/headers.actual';
export * from 'next/dist/client/components/headers';

// mock utilities/overrides (as of Next v14.2.0)
export { headers } from './headers';
Expand Down
4 changes: 2 additions & 2 deletions code/frameworks/nextjs/src/export-mocks/navigation/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Mock } from '@storybook/test';
import { fn } from '@storybook/test';
import { NextjsRouterMocksNotAvailable } from '@storybook/core-events/preview-errors';
import * as originalNavigation from 'next/navigation.actual';
import * as originalNavigation from 'next/dist/client/components/navigation';

let navigationAPI: {
push: Mock;
Expand Down Expand Up @@ -53,7 +53,7 @@ const getRouter = () => {
};

// re-exports of the actual module
export * from 'next/navigation.actual';
export * from 'next/dist/client/components/navigation';

// mock utilities/overrides (as of Next v14.2.0)
const redirect = fn().mockName('redirect');
Expand Down
4 changes: 2 additions & 2 deletions code/frameworks/nextjs/src/export-mocks/router/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Mock } from '@storybook/test';
import { fn } from '@storybook/test';
import { NextjsRouterMocksNotAvailable } from '@storybook/core-events/preview-errors';
import type { NextRouter, SingletonRouter } from 'next/router';
import singletonRouter, * as originalRouter from 'next/router.actual';
import singletonRouter, * as originalRouter from 'next/dist/client/router';

const defaultRouterState = {
route: '/',
Expand Down Expand Up @@ -106,7 +106,7 @@ const getRouter = () => {
};

// re-exports of the actual module
export * from 'next/router.actual';
export * from 'next/dist/client/router';
export default singletonRouter;

// mock utilities/overrides (as of Next v14.2.0)
Expand Down
5 changes: 0 additions & 5 deletions code/frameworks/nextjs/src/export-mocks/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,15 @@ export const getPackageAliases = ({ useESM = false }: { useESM?: boolean } = {})
const routerPath = join(packageLocation, `/dist/export-mocks/router/index.${extension}`);

return {
// "*.actual" paths are used as reexports of the original module
'next/headers.actual': require.resolve('next/headers'),
'next/headers': headersPath,
'@storybook/nextjs/headers.mock': headersPath,

'next/navigation.actual': require.resolve('next/navigation'),
'next/navigation': navigationPath,
'@storybook/nextjs/navigation.mock': navigationPath,

'next/router.actual': require.resolve('next/router'),
'next/router': routerPath,
'@storybook/nextjs/router.mock': routerPath,

'next/cache.actual': require.resolve('next/cache'),
'next/cache': cachePath,
'@storybook/nextjs/cache.mock': cachePath,
};
Expand Down
12 changes: 0 additions & 12 deletions code/frameworks/nextjs/src/globals.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
declare module 'next/dist/compiled';
declare module 'next/dist/compiled/babel/plugin-transform-modules-commonjs';
declare module 'next/dist/compiled/babel/plugin-syntax-jsx';
declare module 'next/navigation.actual' {
export * from 'next/navigation';
}
declare module 'next/router.actual' {
export * from 'next/router';
}
declare module 'next/cache.actual' {
export * from 'next/cache';
}
declare module 'next/headers.actual' {
export * from 'next/headers';
}
Loading