1
- import path from 'path'
2
- import { loadNuxt } from '@nuxt/core-edge'
3
- import { getBuilder } from '@nuxt/builder-edge'
1
+ // @ts -nocheck nuxt internals not typed!
2
+ import { setupTest , getNuxt , getContext } from '@nuxt/test-utils'
4
3
5
4
const watchers : any [ ] = [ ]
6
-
7
5
jest . mock ( 'chokidar' , ( ) => ( {
8
6
watch ( ) {
9
7
return {
@@ -14,61 +12,61 @@ jest.mock('chokidar', () => ({
14
12
}
15
13
}
16
14
} ) )
17
-
18
15
const callChokidarEvent = ( eventName , filename = 'test.js' ) => Promise . all ( watchers . map ( w => w . fn ( eventName , filename ) ) )
19
16
20
- const warn = console . warn = jest . fn ( ) // eslint-disable-line no-console
21
-
22
- describe ( 'module' , ( ) => {
23
- let nuxt , builder , hookFn
24
-
25
- beforeAll ( async ( ) => {
26
- nuxt = await loadNuxt ( { rootDir : path . resolve ( 'test/fixture' ) , for : 'dev' } )
27
- builder = getBuilder ( nuxt )
28
- hookFn = jest . fn ( )
29
- nuxt . hook ( 'components:dirs' , hookFn )
30
-
31
- await builder . build ( )
32
- expect ( warn ) . toBeCalledWith ( 'Components directory not found: `~/non-existent`' )
17
+ describe ( 'My test' , ( ) => {
18
+ console . warn = jest . fn ( ) // eslint-disable-line no-console
19
+ const componentsDirsHook = jest . fn ( )
33
20
34
- builder . generateRoutesAndFiles = jest . fn ( )
35
- } , 60000 )
21
+ setupTest ( {
22
+ testDir : __dirname ,
23
+ fixture : 'fixture' ,
24
+ configFile : 'nuxt.config.ts' ,
25
+ build : true ,
26
+ config : {
27
+ dev : true ,
28
+ hooks : {
29
+ 'components:dirs' : componentsDirsHook ,
30
+ 'build:before' ( builder ) {
31
+ jest . spyOn ( builder , 'generateRoutesAndFiles' )
32
+ }
33
+ }
34
+ }
35
+ } )
36
36
37
37
test ( 'displays autoImported components' , async ( ) => {
38
- const { html } = await nuxt . server . renderRoute ( '/' )
38
+ const { html } = await getNuxt ( ) . server . renderRoute ( '/' )
39
39
expect ( html ) . toContain ( 'Foo' )
40
40
expect ( html ) . toContain ( 'Bar' )
41
41
expect ( html ) . toContain ( 'Base Button' )
42
42
expect ( html ) . toContain ( 'Icon Home' )
43
43
} )
44
44
45
45
test ( 'displays autoImported components in pug template' , async ( ) => {
46
- const { html } = await nuxt . server . renderRoute ( '/pug' )
46
+ const { html } = await getNuxt ( ) . server . renderRoute ( '/pug' )
47
47
expect ( html ) . toContain ( 'Foo' )
48
48
expect ( html ) . toContain ( 'Bar' )
49
49
expect ( html ) . toContain ( 'Base Button' )
50
50
expect ( html ) . toContain ( 'Icon Home' )
51
51
} )
52
52
53
53
test ( 'watch: rebuild on add/remove' , async ( ) => {
54
+ const { builder } = getContext ( )
54
55
builder . generateRoutesAndFiles . mockClear ( )
55
56
await callChokidarEvent ( 'add' )
56
57
expect ( builder . generateRoutesAndFiles ) . toHaveBeenCalledTimes ( 1 )
57
58
await callChokidarEvent ( 'unlink' )
58
59
expect ( builder . generateRoutesAndFiles ) . toHaveBeenCalledTimes ( 2 )
59
60
} )
60
61
61
- test . skip ( 'watch: no rebuild on other events' , async ( ) => {
62
+ test ( 'watch: no rebuild on other events' , async ( ) => {
63
+ const { builder } = getContext ( )
62
64
builder . generateRoutesAndFiles . mockClear ( )
63
65
await callChokidarEvent ( 'foo' )
64
66
expect ( builder . generateRoutesAndFiles ) . not . toHaveBeenCalled ( )
65
67
} )
66
68
67
69
test ( 'hook: components:dirs hook is called' , ( ) => {
68
- expect ( hookFn ) . toHaveBeenCalled ( )
69
- } )
70
-
71
- afterAll ( async ( ) => {
72
- await nuxt . close ( )
70
+ expect ( componentsDirsHook ) . toHaveBeenCalled ( )
73
71
} )
74
72
} )
0 commit comments