From 672dfc75da804487d230b9205b80a20996a8d2b9 Mon Sep 17 00:00:00 2001 From: SukkaW Date: Fri, 23 Dec 2022 10:45:40 +0800 Subject: [PATCH] test: removed exprimental option warning helper --- packages/next/server/config.ts | 2 +- .../warn-removed-experimental-config.test.ts | 98 +++++++++++++++++++ 2 files changed, 99 insertions(+), 1 deletion(-) create mode 100644 test/unit/warn-removed-experimental-config.test.ts diff --git a/packages/next/server/config.ts b/packages/next/server/config.ts index 58e26e1245c5..ce09ba1e577c 100644 --- a/packages/next/server/config.ts +++ b/packages/next/server/config.ts @@ -126,7 +126,7 @@ function setFontLoaderDefaults(config: NextConfigComplete) { } catch {} } -function warnOptionHasBeenMovedOutOfExperimental( +export function warnOptionHasBeenMovedOutOfExperimental( config: NextConfig, oldKey: string, newKey: string, diff --git a/test/unit/warn-removed-experimental-config.test.ts b/test/unit/warn-removed-experimental-config.test.ts new file mode 100644 index 000000000000..479a457ee609 --- /dev/null +++ b/test/unit/warn-removed-experimental-config.test.ts @@ -0,0 +1,98 @@ +import { warnOptionHasBeenMovedOutOfExperimental } from 'next/dist/server/config' + +describe('warnOptionHasBeenMovedOutOfExperimental', () => { + let spy: jest.SpyInstance + beforeAll(() => { + spy = jest.spyOn(console, 'warn').mockImplementation(() => {}) + }) + + it('should not log warning message without experimental config', () => { + warnOptionHasBeenMovedOutOfExperimental( + {}, + 'skipTrailingSlashRedirect', + 'skipTrailingSlashRedirect', + 'next.config.js' + ) + + warnOptionHasBeenMovedOutOfExperimental( + { + experimental: {}, + }, + 'skipTrailingSlashRedirect', + 'skipTrailingSlashRedirect', + 'next.config.js' + ) + + expect(spy).not.toBeCalled() + }) + + it('should log warning message with removed experimental config', () => { + warnOptionHasBeenMovedOutOfExperimental( + { + experimental: { + skipTrailingSlashRedirect: true, + }, + } as any, + 'skipTrailingSlashRedirect', + 'skipTrailingSlashRedirect', + 'next.config.js' + ) + + expect(spy).toHaveBeenCalledWith( + expect.stringContaining('warn'), + '`skipTrailingSlashRedirect` has been moved out of `experimental`. Please update your next.config.js file accordingly.' + ) + }) + + it('should log warning message with removed experimental config - complex key', () => { + warnOptionHasBeenMovedOutOfExperimental( + { + experimental: { + relay: true, + }, + } as any, + 'relay', + 'compiler.relay', + 'next.config.js' + ) + + expect(spy).toHaveBeenCalledWith( + expect.stringContaining('warn'), + '`relay` has been moved out of `experimental` and into `compiler.relay`. Please update your next.config.js file accordingly.' + ) + }) + + it('should update removed experimental config into new config', () => { + const config = { + experimental: { + skipTrailingSlashRedirect: true, + }, + } as any + warnOptionHasBeenMovedOutOfExperimental( + config, + 'skipTrailingSlashRedirect', + 'skipTrailingSlashRedirect', + 'next.config.js' + ) + + expect(config.experimental.skipTrailingSlashRedirect).toBe(true) + expect(config.skipTrailingSlashRedirect).toBe(true) + }) + + it('should update removed experimental config into new config - complex key', () => { + const config = { + experimental: { + foo: 'bar', + }, + } as any + warnOptionHasBeenMovedOutOfExperimental( + config, + 'foo', + 'deep.prop.baz', + 'next.config.js' + ) + + expect(config.experimental.foo).toBe('bar') + expect(config.deep.prop.baz).toBe('bar') + }) +})