Skip to content

Commit cca97c6

Browse files
authored
test: run cpa against create-next-app latest and canary (#14583)
Run create-payload-app against create-next-app latest and canary. Previously, we had this set to `canary` only (hangover from beta). This allows us to catch issues on the bleeding edge. Related to #14581 Also took the opportunity to fix up some TS errors.
1 parent bb520d6 commit cca97c6

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

test/create-payload-app/int.spec.ts

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,14 @@ const writeFile = promisify(fs.writeFile)
1717
const commonNextCreateParams =
1818
'--typescript --eslint --no-tailwind --app --import-alias="@/*" --turbo --yes'
1919

20-
const nextCreateCommands: Partial<Record<'noSrcDir' | 'srcDir', string>> = {
21-
noSrcDir: `pnpm create next-app@canary . ${commonNextCreateParams} --no-src-dir`,
22-
srcDir: `pnpm create next-app@canary . ${commonNextCreateParams} --src-dir`,
20+
const commandKeys = ['srcDir', 'noSrcDir', 'srcDirCanary', 'noSrcDirCanary'] as const
21+
type NextCmdKey = (typeof commandKeys)[number]
22+
23+
const nextCreateCommands: Record<NextCmdKey, string> = {
24+
srcDir: `pnpm create next-app@latest . ${commonNextCreateParams} --src-dir`,
25+
noSrcDir: `pnpm create next-app@latest . ${commonNextCreateParams} --no-src-dir`,
26+
srcDirCanary: `pnpm create next-app@canary . ${commonNextCreateParams} --src-dir`,
27+
noSrcDirCanary: `pnpm create next-app@latest . ${commonNextCreateParams} --no-src-dir`,
2328
}
2429

2530
describe('create-payload-app', () => {
@@ -28,7 +33,7 @@ describe('create-payload-app', () => {
2833
shelljs.exec('pnpm build:create-payload-app')
2934
})
3035

31-
describe.each(Object.keys(nextCreateCommands))(`--init-next with %s`, (nextCmdKey) => {
36+
describe.each(commandKeys)(`--init-next with %s`, (nextCmdKey) => {
3237
const projectDir = tempy.directory()
3338
beforeEach(async () => {
3439
if (fs.existsSync(projectDir)) {
@@ -44,7 +49,7 @@ describe('create-payload-app', () => {
4449
console.log(`Running: ${nextCreateCommands[nextCmdKey]} in ${projectDir}`)
4550
const [cmd, ...args] = nextCreateCommands[nextCmdKey].split(' ')
4651
console.log(`Running: ${cmd} ${args.join(' ')}`)
47-
const { exitCode, stderr } = await execa(cmd, [...args], {
52+
const { exitCode, stderr } = await execa(cmd as string, [...args], {
4853
cwd: projectDir,
4954
stdio: 'inherit',
5055
})
@@ -82,14 +87,14 @@ describe('create-payload-app', () => {
8287
expect(firstResult.success).toEqual(false)
8388

8489
// Move all files from app to top-level directory named `(app)`
85-
if (firstResult.success === false && 'nextAppDir' in firstResult) {
86-
fs.mkdirSync(path.resolve(firstResult.nextAppDir, '(app)'))
87-
fs.readdirSync(path.resolve(firstResult.nextAppDir)).forEach((file) => {
88-
if (file === '(app)') return
89-
fs.renameSync(
90-
path.resolve(firstResult.nextAppDir, file),
91-
path.resolve(firstResult.nextAppDir, '(app)', file),
92-
)
90+
if (firstResult.success === false && firstResult.nextAppDir) {
91+
const nextAppDir = firstResult.nextAppDir
92+
fs.mkdirSync(path.resolve(nextAppDir, '(app)'))
93+
fs.readdirSync(path.resolve(nextAppDir)).forEach((file) => {
94+
if (file === '(app)') {
95+
return
96+
}
97+
fs.renameSync(path.resolve(nextAppDir, file), path.resolve(nextAppDir, '(app)', file))
9398
})
9499
}
95100

@@ -102,7 +107,7 @@ describe('create-payload-app', () => {
102107
useDistFiles: true, // create-payload-app/dist/app/(payload)
103108
})
104109

105-
expect(result.success).toEqual(true)
110+
assertAndExpectToBeTrue(result.success) // Narrowing for TS
106111
expect(result.nextAppDir).toEqual(
107112
path.resolve(projectDir, result.isSrcDir ? 'src/app' : 'app'),
108113
)
@@ -124,7 +129,7 @@ describe('create-payload-app', () => {
124129
}
125130

126131
// Check that `@payload-config` path is added to tsconfig
127-
expect(userTsConfig.compilerOptions.paths?.['@payload-config']).toStrictEqual([
132+
expect(userTsConfig.compilerOptions?.paths?.['@payload-config']).toStrictEqual([
128133
`./${result.isSrcDir ? 'src/' : ''}payload.config.ts`,
129134
])
130135

@@ -141,3 +146,8 @@ describe('create-payload-app', () => {
141146
})
142147
})
143148
})
149+
150+
// Expect and assert that actual is true for type narrowing
151+
function assertAndExpectToBeTrue(actual: unknown): asserts actual is true {
152+
expect(actual).toBe(true)
153+
}

0 commit comments

Comments
 (0)