1
- import { describe , expect , test , vi } from 'vitest' ;
1
+ import { beforeEach , describe , expect , test , vi } from 'vitest' ;
2
2
3
3
import { add , getVersionSpecifier } from './add' ;
4
4
5
5
const MockedConfig = vi . hoisted ( ( ) => {
6
6
return {
7
7
appendValueToArray : vi . fn ( ) ,
8
+ getFieldNode : vi . fn ( ) ,
9
+ valueToNode : vi . fn ( ) ,
10
+ appendNodeToArray : vi . fn ( ) ,
8
11
} ;
9
12
} ) ;
10
13
const MockedPackageManager = vi . hoisted ( ( ) => {
@@ -20,6 +23,12 @@ const MockedPostInstall = vi.hoisted(() => {
20
23
postinstallAddon : vi . fn ( ) ,
21
24
} ;
22
25
} ) ;
26
+ const MockWrapRequireUtils = vi . hoisted ( ( ) => {
27
+ return {
28
+ getRequireWrapperName : vi . fn ( ) ,
29
+ wrapValueWithRequireWrapper : vi . fn ( ) ,
30
+ } ;
31
+ } ) ;
23
32
const MockedConsole = {
24
33
log : vi . fn ( ) ,
25
34
warn : vi . fn ( ) ,
@@ -35,6 +44,9 @@ vi.mock('storybook/internal/csf-tools', () => {
35
44
vi . mock ( './postinstallAddon' , ( ) => {
36
45
return MockedPostInstall ;
37
46
} ) ;
47
+ vi . mock ( './automigrate/fixes/wrap-require-utils' , ( ) => {
48
+ return MockWrapRequireUtils ;
49
+ } ) ;
38
50
vi . mock ( 'storybook/internal/common' , ( ) => {
39
51
return {
40
52
getStorybookInfo : vi . fn ( ( ) => ( { mainConfig : { } , configDir : '' } ) ) ,
@@ -103,6 +115,35 @@ describe('add', () => {
103
115
} ) ;
104
116
105
117
describe ( 'add (extra)' , ( ) => {
118
+ beforeEach ( ( ) => {
119
+ vi . clearAllMocks ( ) ;
120
+ } ) ;
121
+ test ( 'should not add a "wrap require" to the addon when not needed' , async ( ) => {
122
+ MockedConfig . getFieldNode . mockReturnValue ( { } ) ;
123
+ MockWrapRequireUtils . getRequireWrapperName . mockReturnValue ( null ) ;
124
+ await add (
125
+ '@storybook/addon-docs' ,
126
+ { packageManager : 'npm' , skipPostinstall : true } ,
127
+ MockedConsole
128
+ ) ;
129
+
130
+ expect ( MockWrapRequireUtils . wrapValueWithRequireWrapper ) . not . toHaveBeenCalled ( ) ;
131
+ expect ( MockedConfig . appendValueToArray ) . toHaveBeenCalled ( ) ;
132
+ expect ( MockedConfig . appendNodeToArray ) . not . toHaveBeenCalled ( ) ;
133
+ } ) ;
134
+ test ( 'should add a "wrap require" to the addon when applicable' , async ( ) => {
135
+ MockedConfig . getFieldNode . mockReturnValue ( { } ) ;
136
+ MockWrapRequireUtils . getRequireWrapperName . mockReturnValue ( 'require' ) ;
137
+ await add (
138
+ '@storybook/addon-docs' ,
139
+ { packageManager : 'npm' , skipPostinstall : true } ,
140
+ MockedConsole
141
+ ) ;
142
+
143
+ expect ( MockWrapRequireUtils . wrapValueWithRequireWrapper ) . toHaveBeenCalled ( ) ;
144
+ expect ( MockedConfig . appendValueToArray ) . not . toHaveBeenCalled ( ) ;
145
+ expect ( MockedConfig . appendNodeToArray ) . toHaveBeenCalled ( ) ;
146
+ } ) ;
106
147
test ( 'not warning when installing the correct version of storybook' , async ( ) => {
107
148
await add (
108
149
'@storybook/addon-docs' ,
0 commit comments