Skip to content

Commit

Permalink
fix: Fix an error occurs when challengeFileExisting is specified.
Browse files Browse the repository at this point in the history
  • Loading branch information
aiji42 committed Jul 5, 2021
1 parent 1fd5ba9 commit 637155a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/prepare-split-challenge.spec.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -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', () => {
Expand All @@ -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',
Expand All @@ -48,24 +48,24 @@ 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', () => {
process.env = { ...process.env, VERCEL_ENV: 'production' }
;(findPagesDir as jest.Mock).mockImplementation(() => {
throw new Error('some error.')
})
prepareSplitChallenge({})
prepareSplitChallenge()
expect(writeFileSync).not.toBeCalled()
expect(mockExit).toBeCalledWith(1)
})
Expand Down
10 changes: 5 additions & 5 deletions src/prepare-split-challenge.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
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 })
writeFileSync(`${dir}/[__key].js`, scriptText)
} 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}`)
Expand Down
2 changes: 1 addition & 1 deletion src/split-challenge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const sticky = (
): ReturnType<typeof setCookie> =>
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
Expand Down
1 change: 0 additions & 1 deletion src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ export type Rewrites =
| Rewrite[]

export type SplitOptions = {
challengeFileExisting?: boolean
[keyName: string]: {
path: string
hosts: {
Expand Down
45 changes: 45 additions & 0 deletions src/with-split.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
5 changes: 3 additions & 2 deletions src/with-split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { makeRuntimeConfig } from './makeRuntimeConfig'

type WithSplitArgs = {
splits?: SplitOptions
challengeFileExisting?: boolean
rewrites?: () => Promise<Rewrites>
assetPrefix?: string
serverRuntimeConfig?: {
Expand All @@ -22,7 +23,7 @@ type WithSplitResult = Omit<Required<WithSplitArgs>, 'splits'> & {
}

export const withSplit = (args: WithSplitArgs): WithSplitResult => {
const { splits = {}, ...nextConfig } = args
const { splits = {}, challengeFileExisting, ...nextConfig } = args

if (
Object.keys(splits).length > 0 &&
Expand All @@ -38,7 +39,7 @@ export const withSplit = (args: WithSplitArgs): WithSplitResult => {
)
}

prepareSplitChallenge(splits)
prepareSplitChallenge(challengeFileExisting)

return {
...nextConfig,
Expand Down

0 comments on commit 637155a

Please sign in to comment.