2023-12-19 20:26:13 +00:00
import { fileURLToPath } from 'node:url'
2024-05-21 08:18:36 +00:00
import { afterEach , describe , expect , it , vi } from 'vitest'
2023-12-19 20:26:13 +00:00
import { normalize } from 'pathe'
import { withoutTrailingSlash } from 'ufo'
2024-05-21 08:18:36 +00:00
import { readPackageJSON } from 'pkg-types'
import { inc } from 'semver'
2023-12-19 20:26:13 +00:00
import { loadNuxt } from '../src'
2024-05-21 08:18:36 +00:00
import { version } from '../package.json'
2023-12-19 20:26:13 +00:00
const repoRoot = withoutTrailingSlash ( normalize ( fileURLToPath ( new URL ( '../../../' , import . meta . url ) ) ) )
2024-05-21 08:18:36 +00:00
vi . stubGlobal ( 'console' , {
. . . console ,
error : vi.fn ( console . error ) ,
warn : vi.fn ( console . warn ) ,
} )
vi . mock ( 'pkg-types' , async ( og ) = > {
const originalPkgTypes = ( await og < typeof import ( ' pkg - types ' ) > ( ) )
return {
. . . originalPkgTypes ,
readPackageJSON : vi.fn ( originalPkgTypes . readPackageJSON ) ,
}
} )
afterEach ( ( ) = > {
vi . clearAllMocks ( )
} )
2023-12-19 20:26:13 +00:00
describe ( 'loadNuxt' , ( ) = > {
it ( 'respects hook overrides' , async ( ) = > {
let hookRan = false
const nuxt = await loadNuxt ( {
cwd : repoRoot ,
ready : true ,
overrides : {
hooks : {
2024-03-09 06:48:15 +00:00
ready ( ) {
2023-12-19 20:26:13 +00:00
hookRan = true
2024-04-05 18:08:32 +00:00
} ,
} ,
} ,
2023-12-19 20:26:13 +00:00
} )
await nuxt . close ( )
expect ( hookRan ) . toBe ( true )
} )
} )
2024-05-21 08:18:36 +00:00
describe ( 'dependency mismatch' , ( ) = > {
it ( 'expect mismatched dependency to log a warning' , async ( ) = > {
vi . mocked ( readPackageJSON ) . mockReturnValue ( Promise . resolve ( {
version : '3.0.0' ,
} ) )
const nuxt = await loadNuxt ( {
cwd : repoRoot ,
} )
2024-06-27 14:19:59 +00:00
// @nuxt/kit is explicitly installed in repo root but @nuxt/schema isn't, so we only
// get warnings about @nuxt/schema
2024-05-21 08:18:36 +00:00
expect ( console . warn ) . toHaveBeenCalledWith ( ` [nuxt] Expected \` @nuxt/kit \` to be at least \` ${ version } \` but got \` 3.0.0 \` . This might lead to unexpected behavior. Check your package.json or refresh your lockfile. ` )
vi . mocked ( readPackageJSON ) . mockRestore ( )
await nuxt . close ( )
} )
it . each ( [
{
name : 'nuxt version is lower' ,
depVersion : inc ( version , 'minor' ) ,
} ,
{
name : 'version matches' ,
depVersion : version ,
} ,
] ) ( 'expect no warning when $name.' , async ( { depVersion } ) = > {
vi . mocked ( readPackageJSON ) . mockReturnValue ( Promise . resolve ( {
depVersion ,
} ) )
const nuxt = await loadNuxt ( {
cwd : repoRoot ,
} )
expect ( console . warn ) . not . toHaveBeenCalled ( )
await nuxt . close ( )
vi . mocked ( readPackageJSON ) . mockRestore ( )
} )
} )