@@ -47,13 +47,25 @@ jest.mock('detect-package-manager', () => ({
47
47
getNpmVersion : ( ) => '3.1.1' ,
48
48
} ) ) ;
49
49
50
+ const originalSep = path . sep ;
51
+
50
52
describe ( 'sanitizeAddonName' , ( ) => {
51
- const originalSep = path . sep ;
53
+ let cwdSpy : jest . SpyInstance ;
54
+
52
55
beforeEach ( ( ) => {
53
56
// @ts -expect-error the property is read only but we can change it for testing purposes
54
57
path . sep = originalSep ;
55
58
} ) ;
56
59
60
+ afterAll ( ( ) => {
61
+ // @ts -expect-error the property is read only but we can change it for testing purposes
62
+ path . sep = originalSep ;
63
+ } ) ;
64
+
65
+ afterEach ( ( ) => {
66
+ cwdSpy ?. mockRestore ( ) ;
67
+ } ) ;
68
+
57
69
test ( 'special addon names' , ( ) => {
58
70
const addonNames = [
59
71
'@storybook/preset-create-react-app' ,
@@ -82,7 +94,7 @@ describe('sanitizeAddonName', () => {
82
94
// @ts -expect-error the property is read only but we can change it for testing purposes
83
95
path . sep = '\\' ;
84
96
const cwdMockPath = `C:\\Users\\username\\storybook-app` ;
85
- jest . spyOn ( process , `cwd` ) . mockImplementationOnce ( ( ) => cwdMockPath ) ;
97
+ cwdSpy = jest . spyOn ( process , `cwd` ) . mockReturnValueOnce ( cwdMockPath ) ;
86
98
87
99
expect ( sanitizeAddonName ( `${ cwdMockPath } \\local-addon\\themes.js` ) ) . toEqual (
88
100
'$SNIP\\local-addon\\themes'
@@ -93,15 +105,138 @@ describe('sanitizeAddonName', () => {
93
105
// @ts -expect-error the property is read only but we can change it for testing purposes
94
106
path . sep = '/' ;
95
107
const cwdMockPath = `/Users/username/storybook-app` ;
96
- jest . spyOn ( process , `cwd` ) . mockImplementationOnce ( ( ) => cwdMockPath ) ;
108
+ cwdSpy = jest . spyOn ( process , `cwd` ) . mockReturnValue ( cwdMockPath ) ;
97
109
98
110
expect ( sanitizeAddonName ( `${ cwdMockPath } /local-addon/themes.js` ) ) . toEqual (
99
111
'$SNIP/local-addon/themes'
100
112
) ;
101
113
} ) ;
102
114
} ) ;
103
115
104
- describe ( 'await computeStorybookMetadata' , ( ) => {
116
+ describe ( 'computeStorybookMetadata' , ( ) => {
117
+ beforeEach ( ( ) => {
118
+ // @ts -expect-error the property is read only but we can change it for testing purposes
119
+ path . sep = originalSep ;
120
+ } ) ;
121
+
122
+ afterAll ( ( ) => {
123
+ // @ts -expect-error the property is read only but we can change it for testing purposes
124
+ path . sep = originalSep ;
125
+ } ) ;
126
+
127
+ describe ( 'pnp paths' , ( ) => {
128
+ let cwdSpy : jest . SpyInstance ;
129
+
130
+ afterEach ( ( ) => {
131
+ cwdSpy ?. mockRestore ( ) ;
132
+ } ) ;
133
+
134
+ test ( 'should parse pnp paths for known frameworks' , async ( ) => {
135
+ const unixResult = await computeStorybookMetadata ( {
136
+ packageJson : packageJsonMock ,
137
+ mainConfig : {
138
+ ...mainJsMock ,
139
+ framework : {
140
+ name : '/Users/foo/storybook/.yarn/__virtual__/@storybook-react-vite-virtual-769c990b9/0/cache/@storybook-react-vite-npm-7.1.0-alpha.38-512b-a23.zip/node_modules/@storybook/react-vite' ,
141
+ options : {
142
+ fastRefresh : false ,
143
+ } ,
144
+ } ,
145
+ } ,
146
+ } ) ;
147
+
148
+ expect ( unixResult . framework ) . toEqual ( {
149
+ name : '@storybook/react-vite' ,
150
+ options : { fastRefresh : false } ,
151
+ } ) ;
152
+
153
+ const windowsResult = await computeStorybookMetadata ( {
154
+ packageJson : packageJsonMock ,
155
+ mainConfig : {
156
+ ...mainJsMock ,
157
+ framework : {
158
+ name : 'C:\\Users\\foo\\storybook\\.yarn\\__virtual__\\@storybook-react-vite-virtual-769c990b9\\0\\cache\\@storybook-react-vite-npm-7.1.0-alpha.38-512b-a23.zip\\node_modules\\@storybook\\react-vite' ,
159
+ options : {
160
+ fastRefresh : false ,
161
+ } ,
162
+ } ,
163
+ } ,
164
+ } ) ;
165
+
166
+ expect ( windowsResult . framework ) . toEqual ( {
167
+ name : '@storybook/react-vite' ,
168
+ options : { fastRefresh : false } ,
169
+ } ) ;
170
+ } ) ;
171
+
172
+ test ( 'should parse pnp paths for unknown frameworks' , async ( ) => {
173
+ const unixResult = await computeStorybookMetadata ( {
174
+ packageJson : packageJsonMock ,
175
+ mainConfig : {
176
+ ...mainJsMock ,
177
+ framework : {
178
+ name : '/Users/foo/my-project/.yarn/__virtual__/@storybook-react-vite-virtual-769c990b9/0/cache/@storybook-react-rust-npm-7.1.0-alpha.38-512b-a23.zip/node_modules/storybook-react-rust' ,
179
+ } ,
180
+ } ,
181
+ } ) ;
182
+
183
+ expect ( unixResult . framework ) . toEqual ( {
184
+ name : 'storybook-react-rust' ,
185
+ } ) ;
186
+
187
+ const windowsResult = await computeStorybookMetadata ( {
188
+ packageJson : packageJsonMock ,
189
+ mainConfig : {
190
+ ...mainJsMock ,
191
+ framework : {
192
+ name : 'C:\\Users\\foo\\my-project\\.yarn\\__virtual__\\@storybook-react-vite-virtual-769c990b9\\0\\cache\\@storybook-react-rust-npm-7.1.0-alpha.38-512b-a23.zip\\node_modules\\storybook-react-rust' ,
193
+ } ,
194
+ } ,
195
+ } ) ;
196
+
197
+ expect ( windowsResult . framework ) . toEqual ( {
198
+ name : 'storybook-react-rust' ,
199
+ } ) ;
200
+ } ) ;
201
+
202
+ test ( 'should sanitize pnp paths for local frameworks' , async ( ) => {
203
+ // @ts -expect-error the property is read only but we can change it for testing purposes
204
+ path . sep = '/' ;
205
+ cwdSpy = jest . spyOn ( process , 'cwd' ) . mockReturnValue ( '/Users/foo/my-project' ) ;
206
+
207
+ const unixResult = await computeStorybookMetadata ( {
208
+ packageJson : packageJsonMock ,
209
+ mainConfig : {
210
+ ...mainJsMock ,
211
+ framework : {
212
+ name : '/Users/foo/my-project/.storybook/some-local-framework' ,
213
+ } ,
214
+ } ,
215
+ } ) ;
216
+
217
+ expect ( unixResult . framework ) . toEqual ( {
218
+ name : '$SNIP/.storybook/some-local-framework' ,
219
+ } ) ;
220
+
221
+ // @ts -expect-error the property is read only but we can change it for testing purposes
222
+ path . sep = '\\' ;
223
+ cwdSpy = jest . spyOn ( process , 'cwd' ) . mockReturnValue ( 'C:\\Users\\foo\\my-project' ) ;
224
+ const windowsResult = await computeStorybookMetadata ( {
225
+ packageJson : packageJsonMock ,
226
+ mainConfig : {
227
+ ...mainJsMock ,
228
+ framework : {
229
+ name : 'C:\\Users\\foo\\my-project\\.storybook\\some-local-framework' ,
230
+ } ,
231
+ } ,
232
+ } ) ;
233
+
234
+ expect ( windowsResult . framework ) . toEqual ( {
235
+ name : '$SNIP\\.storybook\\some-local-framework' ,
236
+ } ) ;
237
+ } ) ;
238
+ } ) ;
239
+
105
240
test ( 'should return frameworkOptions from mainjs' , async ( ) => {
106
241
const reactResult = await computeStorybookMetadata ( {
107
242
packageJson : packageJsonMock ,
0 commit comments