|
| 1 | +/* eslint-env jest */ |
| 2 | + |
| 3 | +import { join } from 'path' |
| 4 | +import { findPort, launchApp, killApp } from 'next-test-utils' |
| 5 | +import { promises as fs } from 'fs' |
| 6 | + |
| 7 | +jest.setTimeout(1000 * 60 * 2) |
| 8 | + |
| 9 | +const appDir = join(__dirname, '..') |
| 10 | +const appTypeDeclarations = join(appDir, 'next-env.d.ts') |
| 11 | + |
| 12 | +describe('TypeScript App Type Declarations', () => { |
| 13 | + it('should write a new next-env.d.ts if none exist', async () => { |
| 14 | + const prevContent = await fs.readFile(appTypeDeclarations, 'utf8') |
| 15 | + try { |
| 16 | + await fs.unlink(appTypeDeclarations) |
| 17 | + const appPort = await findPort() |
| 18 | + let app |
| 19 | + try { |
| 20 | + app = await launchApp(appDir, appPort, {}) |
| 21 | + const content = await fs.readFile(appTypeDeclarations, 'utf8') |
| 22 | + expect(content).toEqual(prevContent) |
| 23 | + } finally { |
| 24 | + await killApp(app) |
| 25 | + } |
| 26 | + } finally { |
| 27 | + await fs.writeFile(appTypeDeclarations, prevContent) |
| 28 | + } |
| 29 | + }) |
| 30 | + |
| 31 | + it('should overwrite next-env.d.ts if an incorrect one exists', async () => { |
| 32 | + const prevContent = await fs.readFile(appTypeDeclarations, 'utf8') |
| 33 | + try { |
| 34 | + await fs.writeFile(appTypeDeclarations, prevContent + 'modification') |
| 35 | + const appPort = await findPort() |
| 36 | + let app |
| 37 | + try { |
| 38 | + app = await launchApp(appDir, appPort, {}) |
| 39 | + const content = await fs.readFile(appTypeDeclarations, 'utf8') |
| 40 | + expect(content).toEqual(prevContent) |
| 41 | + } finally { |
| 42 | + await killApp(app) |
| 43 | + } |
| 44 | + } finally { |
| 45 | + await fs.writeFile(appTypeDeclarations, prevContent) |
| 46 | + } |
| 47 | + }) |
| 48 | + |
| 49 | + it('should not touch an existing correct next-env.d.ts', async () => { |
| 50 | + const prevStat = await fs.stat(appTypeDeclarations) |
| 51 | + const appPort = await findPort() |
| 52 | + let app |
| 53 | + try { |
| 54 | + app = await launchApp(appDir, appPort, {}) |
| 55 | + const stat = await fs.stat(appTypeDeclarations) |
| 56 | + expect(stat.mtime).toEqual(prevStat.mtime) |
| 57 | + } finally { |
| 58 | + await killApp(app) |
| 59 | + } |
| 60 | + }) |
| 61 | +}) |
0 commit comments