diff --git a/src/prepare-split-challenge.spec.ts b/src/prepare-split-challenge.spec.ts index a6b8f81..7081c24 100644 --- a/src/prepare-split-challenge.spec.ts +++ b/src/prepare-split-challenge.spec.ts @@ -1,7 +1,6 @@ import { prepareSplitChallenge } from './prepare-split-challenge' import { writeFileSync, existsSync, mkdirSync } from 'fs' import { findPagesDir } from 'next/dist/lib/find-pages-dir' -import { SplitOptions } from './types' jest.mock('fs', () => ({ writeFileSync: jest.fn(), @@ -14,10 +13,11 @@ jest.mock('next/dist/lib/find-pages-dir', () => ({ })) jest.spyOn(console, 'error').mockImplementation((mes) => console.log(mes)) -// eslint-disable-next-line @typescript-eslint/ban-ts-comment -// @ts-ignore + const mockExit = jest .spyOn(process, 'exit') + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-ignore .mockImplementation((code) => console.log('exit: ', code)) describe('prepareSplitChallenge', () => { @@ -34,7 +34,7 @@ describe('prepareSplitChallenge', () => { process.env = { ...process.env, VERCEL_ENV: 'production' } ;(findPagesDir as jest.Mock).mockReturnValue('pages') ;(existsSync as jest.Mock).mockReturnValue(false) - prepareSplitChallenge({}) + prepareSplitChallenge() expect(mkdirSync).toBeCalled() expect(writeFileSync).toBeCalledWith( 'pages/_split-challenge/[__key].js', @@ -48,16 +48,16 @@ export default SplitChallenge process.env = { ...process.env, VERCEL_ENV: 'production' } ;(findPagesDir as jest.Mock).mockReturnValue('pages') ;(existsSync as jest.Mock).mockReturnValue(true) - prepareSplitChallenge({}) + prepareSplitChallenge() expect(mkdirSync).not.toBeCalled() }) it('must not work when runs on not production', () => { - prepareSplitChallenge({}) + prepareSplitChallenge() expect(writeFileSync).not.toBeCalled() }) it('must not work when challenge file is existing', () => { process.env = { ...process.env, VERCEL_ENV: 'production' } - prepareSplitChallenge({ challengeFileExisting: true } as SplitOptions) + prepareSplitChallenge(true) expect(writeFileSync).not.toBeCalled() }) it('must request a self-creation when an exception is raised', () => { @@ -65,7 +65,7 @@ export default SplitChallenge ;(findPagesDir as jest.Mock).mockImplementation(() => { throw new Error('some error.') }) - prepareSplitChallenge({}) + prepareSplitChallenge() expect(writeFileSync).not.toBeCalled() expect(mockExit).toBeCalledWith(1) }) diff --git a/src/prepare-split-challenge.ts b/src/prepare-split-challenge.ts index f53b58d..d69d21f 100644 --- a/src/prepare-split-challenge.ts +++ b/src/prepare-split-challenge.ts @@ -1,15 +1,15 @@ import { findPagesDir } from 'next/dist/lib/find-pages-dir' import { writeFileSync, existsSync, mkdirSync } from 'fs' -import { SplitOptions } from './types' const scriptText = `export { getServerSideProps } from 'next-with-split' const SplitChallenge = () => null export default SplitChallenge ` -export const prepareSplitChallenge = (options: SplitOptions): void => { - if (process.env.VERCEL_ENV !== 'production' || options.challengeFileExisting) - return +export const prepareSplitChallenge = ( + challengeFileExisting?: boolean +): void => { + if (process.env.VERCEL_ENV !== 'production' || challengeFileExisting) return try { const dir = `${findPagesDir('')}/_split-challenge` if (!existsSync(dir)) mkdirSync(dir, { recursive: true }) @@ -17,7 +17,7 @@ export const prepareSplitChallenge = (options: SplitOptions): void => { } catch (e) { console.error(e.message) console.log(`> Could not create the necessary file for the split test. -Create the file yourself and set \`splits.challengeFileExisting: true\`. +Create the file yourself and set \`challengeFileExisting: true\`. The code in the file should look like this // pages/_split-challenge/[__key].js ${scriptText}`) diff --git a/src/split-challenge.ts b/src/split-challenge.ts index c83bf81..9e9b937 100644 --- a/src/split-challenge.ts +++ b/src/split-challenge.ts @@ -39,7 +39,7 @@ export const sticky = ( ): ReturnType => setCookie(ctx, cookieKey(splitKey), config.branch, config.cookie) -export const getPath = (config: SplitConfig, query: ParsedUrlQuery) => { +export const getPath = (config: SplitConfig, query: ParsedUrlQuery): string => { const keys: { name: string prefix: string diff --git a/src/types.d.ts b/src/types.d.ts index 51eb44f..0a4f1f4 100644 --- a/src/types.d.ts +++ b/src/types.d.ts @@ -10,7 +10,6 @@ export type Rewrites = | Rewrite[] export type SplitOptions = { - challengeFileExisting?: boolean [keyName: string]: { path: string hosts: { diff --git a/src/with-split.spec.ts b/src/with-split.spec.ts index d895f33..50df496 100644 --- a/src/with-split.spec.ts +++ b/src/with-split.spec.ts @@ -104,6 +104,51 @@ describe('withSplit', () => { }) }) }) + it('return split test config when challenge file existing', () => { + process.env = { + ...process.env, + VERCEL_URL: 'vercel.example.com', + VERCEL_ENV: 'production' + } + const conf = withSplit({ + splits: { + test1: { + hosts: { + branch1: 'https://branch1.example.com', + branch2: 'https://branch2.example.com' + }, + path: '/foo/:path*' + } + }, + challengeFileExisting: true + }) + expect(conf.serverRuntimeConfig).toEqual({ + splits: { + test1: { + branch1: { + host: 'https://branch1.example.com', + path: '/foo/:path*', + cookie: { path: '/', maxAge: 60 * 60 * 24 } + }, + branch2: { + host: 'https://branch2.example.com', + path: '/foo/:path*', + cookie: { path: '/', maxAge: 60 * 60 * 24 } + } + } + } + }) + return conf.rewrites().then((res) => { + expect(res).toEqual({ + beforeFiles: [ + { + source: '/foo/:path*', + destination: '/_split-challenge/test1' + } + ] + }) + }) + }) it('return empty rewrite rules when runs on not production', () => { process.env = { ...process.env, VERCEL_ENV: 'preview' } const conf = withSplit({ diff --git a/src/with-split.ts b/src/with-split.ts index 05221c2..ad22c1a 100644 --- a/src/with-split.ts +++ b/src/with-split.ts @@ -5,6 +5,7 @@ import { makeRuntimeConfig } from './makeRuntimeConfig' type WithSplitArgs = { splits?: SplitOptions + challengeFileExisting?: boolean rewrites?: () => Promise assetPrefix?: string serverRuntimeConfig?: { @@ -22,7 +23,7 @@ type WithSplitResult = Omit, 'splits'> & { } export const withSplit = (args: WithSplitArgs): WithSplitResult => { - const { splits = {}, ...nextConfig } = args + const { splits = {}, challengeFileExisting, ...nextConfig } = args if ( Object.keys(splits).length > 0 && @@ -38,7 +39,7 @@ export const withSplit = (args: WithSplitArgs): WithSplitResult => { ) } - prepareSplitChallenge(splits) + prepareSplitChallenge(challengeFileExisting) return { ...nextConfig,