Skip to content

Commit 5d00cfd

Browse files
committed
fix(create-storybook): drop contextual dependencies
We've had this TODO for a while. Basically, we no longer pull dependencies from the context, so we don't need to mock it or populate it anymore.
1 parent 109d310 commit 5d00cfd

File tree

2 files changed

+74
-116
lines changed

2 files changed

+74
-116
lines changed

code/lib/create-storybook/src/ink/steps/checks/vitestConfigFiles.test.ts

Lines changed: 32 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import * as fs from 'node:fs/promises';
2-
3-
import { describe, expect, it } from 'vitest';
1+
import { describe, expect, it, vi } from 'vitest';
42

53
import * as babel from 'storybook/internal/babel';
64

75
import { findUp } from 'find-up';
86

97
import { vitestConfigFiles } from './vitestConfigFiles';
108

11-
const liveContext: any = { babel, findUp, fs };
9+
vi.mock('find-up', () => ({
10+
findUp: vi.fn().mockImplementation(([name]) => name),
11+
findUpSync: vi.fn(),
12+
}));
1213

1314
const fileMocks = {
1415
'vitest.config.ts': `
@@ -70,13 +71,11 @@ const fileMocks = {
7071
`,
7172
};
7273

73-
const mockContext: any = {
74-
...liveContext,
75-
findUp: async ([name]: string[]) => name,
76-
fs: {
77-
readFile: async (path: keyof typeof fileMocks) => fileMocks[path],
78-
},
79-
};
74+
vi.mock('node:fs/promises', () => ({
75+
readFile: vi.fn().mockImplementation((filePath) => fileMocks[filePath as keyof typeof fileMocks]),
76+
}));
77+
78+
const mockContext: any = {};
8079

8180
const coerce =
8281
(from: string, to: string) =>
@@ -87,64 +86,44 @@ const state: any = {
8786
directory: '.',
8887
};
8988

90-
// TODO @ghengeveld, I am in the process of removing the context
91-
describe.skip('these tests need to be updated', () => {
92-
it('should run properly with live dependencies', async () => {
93-
const result = await vitestConfigFiles.condition(liveContext, state);
94-
expect(result).toEqual({ type: 'compatible' });
95-
});
96-
89+
describe('vitestConfigFiles', () => {
9790
it('should run properly with mock dependencies', async () => {
9891
const result = await vitestConfigFiles.condition(mockContext, state);
9992
expect(result).toEqual({ type: 'compatible' });
10093
});
10194

102-
it('should disallow missing dependencies', async () => {
103-
const result = await vitestConfigFiles.condition({} as any, state);
104-
expect(result).toEqual({
105-
type: 'incompatible',
106-
reasons: ['Missing babel on context', 'Missing findUp on context', 'Missing fs on context'],
107-
});
108-
});
109-
11095
describe('Check Vitest workspace files', () => {
11196
it('should disallow JSON workspace file', async () => {
112-
const result = await vitestConfigFiles.condition(
113-
{ ...mockContext, findUp: coerce('workspace', 'vitest.workspace.json') },
114-
state
115-
);
97+
vi.mocked(findUp).mockImplementation(coerce('workspace', 'vitest.workspace.json') as never);
98+
const result = await vitestConfigFiles.condition(mockContext, state);
11699
expect(result).toEqual({
117100
type: 'incompatible',
118101
reasons: ['Cannot auto-update JSON workspace file: vitest.workspace.json'],
119102
});
120103
});
121104

122105
it('should disallow invalid workspace file', async () => {
123-
const result = await vitestConfigFiles.condition(
124-
{ ...mockContext, findUp: coerce('workspace', 'invalidWorkspace.ts') },
125-
state
126-
);
106+
vi.mocked(findUp).mockImplementation(coerce('workspace', 'invalidWorkspace.ts') as never);
107+
const result = await vitestConfigFiles.condition(mockContext, state);
127108
expect(result).toEqual({
128109
type: 'incompatible',
129110
reasons: ['Found an invalid workspace config file: invalidWorkspace.ts'],
130111
});
131112
});
132113

133114
it('should allow defineWorkspace syntax', async () => {
134-
const result = await vitestConfigFiles.condition(
135-
{ ...mockContext, findUp: coerce('workspace', 'defineWorkspace.ts') },
136-
state
137-
);
115+
vi.mocked(findUp).mockImplementation(coerce('workspace', 'defineWorkspace.ts') as never);
116+
const result = await vitestConfigFiles.condition(mockContext, state);
138117
expect(result).toEqual({
139118
type: 'compatible',
140119
});
141120
});
142121

143122
it('should disallow invalid defineWorkspace syntax', async () => {
144-
const result = await vitestConfigFiles.condition(
145-
{ ...mockContext, findUp: coerce('workspace', 'defineWorkspace-invalid.ts') },
146-
state
123+
vi.mocked(findUp).mockImplementation(
124+
coerce('workspace', 'defineWorkspace-invalid.ts') as never
147125
);
126+
const result = await vitestConfigFiles.condition(mockContext, state);
148127
expect(result).toEqual({
149128
type: 'incompatible',
150129
reasons: ['Found an invalid workspace config file: defineWorkspace-invalid.ts'],
@@ -154,63 +133,51 @@ describe.skip('these tests need to be updated', () => {
154133

155134
describe('Check Vitest config files', () => {
156135
it('should disallow CommonJS config file', async () => {
157-
const result = await vitestConfigFiles.condition(
158-
{ ...mockContext, findUp: coerce('config', 'vitest.config.cjs') },
159-
state
160-
);
136+
vi.mocked(findUp).mockImplementation(coerce('config', 'vitest.config.cjs') as never);
137+
const result = await vitestConfigFiles.condition(mockContext, state);
161138
expect(result).toEqual({
162139
type: 'incompatible',
163140
reasons: ['Cannot auto-update CommonJS config file: vitest.config.cjs'],
164141
});
165142
});
166143

167144
it('should disallow invalid config file', async () => {
168-
const result = await vitestConfigFiles.condition(
169-
{ ...mockContext, findUp: coerce('config', 'invalidConfig.ts') },
170-
state
171-
);
145+
vi.mocked(findUp).mockImplementation(coerce('config', 'invalidConfig.ts') as never);
146+
const result = await vitestConfigFiles.condition(mockContext, state);
172147
expect(result).toEqual({
173148
type: 'incompatible',
174149
reasons: ['Found an invalid Vitest config file: invalidConfig.ts'],
175150
});
176151
});
177152

178153
it('should allow existing test config option', async () => {
179-
const result = await vitestConfigFiles.condition(
180-
{ ...mockContext, findUp: coerce('config', 'testConfig.ts') },
181-
state
182-
);
154+
vi.mocked(findUp).mockImplementation(coerce('config', 'testConfig.ts') as never);
155+
const result = await vitestConfigFiles.condition(mockContext, state);
183156
expect(result).toEqual({
184157
type: 'compatible',
185158
});
186159
});
187160

188161
it('should disallow invalid test config option', async () => {
189-
const result = await vitestConfigFiles.condition(
190-
{ ...mockContext, findUp: coerce('config', 'testConfig-invalid.ts') },
191-
state
192-
);
162+
vi.mocked(findUp).mockImplementation(coerce('config', 'testConfig-invalid.ts') as never);
163+
const result = await vitestConfigFiles.condition(mockContext, state);
193164
expect(result).toEqual({
194165
type: 'incompatible',
195166
reasons: ['Found an invalid Vitest config file: testConfig-invalid.ts'],
196167
});
197168
});
198169

199170
it('should allow existing test.workspace config option', async () => {
200-
const result = await vitestConfigFiles.condition(
201-
{ ...mockContext, findUp: coerce('config', 'workspaceConfig.ts') },
202-
state
203-
);
171+
vi.mocked(findUp).mockImplementation(coerce('config', 'workspaceConfig.ts') as never);
172+
const result = await vitestConfigFiles.condition(mockContext, state);
204173
expect(result).toEqual({
205174
type: 'compatible',
206175
});
207176
});
208177

209178
it('should disallow invalid test.workspace config option', async () => {
210-
const result = await vitestConfigFiles.condition(
211-
{ ...mockContext, findUp: coerce('config', 'workspaceConfig-invalid.ts') },
212-
state
213-
);
179+
vi.mocked(findUp).mockImplementation(coerce('config', 'workspaceConfig-invalid.ts') as never);
180+
const result = await vitestConfigFiles.condition(mockContext, state);
214181
expect(result).toEqual({
215182
type: 'incompatible',
216183
reasons: ['Found an invalid Vitest config file: workspaceConfig-invalid.ts'],

code/lib/create-storybook/src/ink/steps/checks/vitestConfigFiles.tsx

Lines changed: 42 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -88,60 +88,51 @@ export const isValidWorkspaceConfigFile: (fileContents: string, babel: any) => b
8888
* - No -> exit
8989
*/
9090
export const vitestConfigFiles: Check = {
91-
condition: async (context, state) => {
92-
const deps = ['babel', 'findUp', 'fs'];
93-
if (babel && findUp && fs) {
94-
const reasons = [];
95-
96-
const projectRoot = getProjectRoot();
97-
98-
const vitestWorkspaceFile = await findUp(
99-
['ts', 'js', 'json'].flatMap((ex) => [`vitest.workspace.${ex}`, `vitest.projects.${ex}`]),
100-
{ cwd: state.directory, stopAt: projectRoot }
101-
);
102-
if (vitestWorkspaceFile?.endsWith('.json')) {
103-
reasons.push(`Cannot auto-update JSON workspace file: ${vitestWorkspaceFile}`);
104-
} else if (vitestWorkspaceFile) {
105-
const fileContents = await fs.readFile(vitestWorkspaceFile, 'utf8');
106-
if (!isValidWorkspaceConfigFile(fileContents, babel)) {
107-
reasons.push(`Found an invalid workspace config file: ${vitestWorkspaceFile}`);
108-
}
91+
condition: async (_context, state) => {
92+
const reasons = [];
93+
94+
const projectRoot = getProjectRoot();
95+
96+
const vitestWorkspaceFile = await findUp(
97+
['ts', 'js', 'json'].flatMap((ex) => [`vitest.workspace.${ex}`, `vitest.projects.${ex}`]),
98+
{ cwd: state.directory, stopAt: projectRoot }
99+
);
100+
if (vitestWorkspaceFile?.endsWith('.json')) {
101+
reasons.push(`Cannot auto-update JSON workspace file: ${vitestWorkspaceFile}`);
102+
} else if (vitestWorkspaceFile) {
103+
const fileContents = await fs.readFile(vitestWorkspaceFile, 'utf8');
104+
if (!isValidWorkspaceConfigFile(fileContents, babel)) {
105+
reasons.push(`Found an invalid workspace config file: ${vitestWorkspaceFile}`);
109106
}
107+
}
110108

111-
const vitestConfigFile = await findUp(
112-
['ts', 'js', 'tsx', 'jsx', 'cts', 'cjs', 'mts', 'mjs'].map((ex) => `vitest.config.${ex}`),
113-
{ cwd: state.directory, stopAt: projectRoot }
114-
);
115-
if (vitestConfigFile?.endsWith('.cts') || vitestConfigFile?.endsWith('.cjs')) {
116-
reasons.push(`Cannot auto-update CommonJS config file: ${vitestConfigFile}`);
117-
} else if (vitestConfigFile) {
118-
let isValidVitestConfig = false;
119-
const configContent = await fs.readFile(vitestConfigFile, 'utf8');
120-
const parsedConfig = babel.babelParse(configContent);
121-
babel.traverse(parsedConfig, {
122-
ExportDefaultDeclaration(path) {
123-
if (
124-
isDefineConfigExpression(path.node.declaration) &&
125-
isSafeToExtendWorkspace(path.node.declaration as CallExpression)
126-
) {
127-
isValidVitestConfig = true;
128-
}
129-
},
130-
});
131-
if (!isValidVitestConfig) {
132-
reasons.push(`Found an invalid Vitest config file: ${vitestConfigFile}`);
133-
}
109+
const vitestConfigFile = await findUp(
110+
['ts', 'js', 'tsx', 'jsx', 'cts', 'cjs', 'mts', 'mjs'].map((ex) => `vitest.config.${ex}`),
111+
{ cwd: state.directory, stopAt: projectRoot }
112+
);
113+
if (vitestConfigFile?.endsWith('.cts') || vitestConfigFile?.endsWith('.cjs')) {
114+
reasons.push(`Cannot auto-update CommonJS config file: ${vitestConfigFile}`);
115+
} else if (vitestConfigFile) {
116+
let isValidVitestConfig = false;
117+
const configContent = await fs.readFile(vitestConfigFile, 'utf8');
118+
const parsedConfig = babel.babelParse(configContent);
119+
babel.traverse(parsedConfig, {
120+
ExportDefaultDeclaration(path) {
121+
if (
122+
isDefineConfigExpression(path.node.declaration) &&
123+
isSafeToExtendWorkspace(path.node.declaration as CallExpression)
124+
) {
125+
isValidVitestConfig = true;
126+
}
127+
},
128+
});
129+
if (!isValidVitestConfig) {
130+
reasons.push(`Found an invalid Vitest config file: ${vitestConfigFile}`);
134131
}
135-
136-
return reasons.length
137-
? { type: CompatibilityType.INCOMPATIBLE, reasons }
138-
: { type: CompatibilityType.COMPATIBLE };
139132
}
140-
return {
141-
type: CompatibilityType.INCOMPATIBLE,
142-
reasons: deps
143-
.filter((p) => !context[p as keyof typeof context])
144-
.map((p) => `Missing ${p} on context`),
145-
};
133+
134+
return reasons.length
135+
? { type: CompatibilityType.INCOMPATIBLE, reasons }
136+
: { type: CompatibilityType.COMPATIBLE };
146137
},
147138
};

0 commit comments

Comments
 (0)