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