Skip to content

Commit cf894b3

Browse files
feat: handle configuration file not exposing a config object (#22)
* feat: handle configuration file not exposing a config object * test: adapt * chore: fmt * chore: lint * chore: lint
1 parent cda67e0 commit cf894b3

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

plugins/plugin-tools/src/config.spec.ts

+11
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,17 @@ describe('config', () => {
189189
JunoPluginError
190190
);
191191
});
192+
193+
it('throws if config is undefined in satelliteId', async () => {
194+
vi.spyOn(configLoader, 'junoConfigExist').mockResolvedValue(true);
195+
vi.spyOn(configLoader, 'readJunoConfig').mockResolvedValue(
196+
undefined as unknown as JunoConfig
197+
);
198+
199+
await expect(() => satelliteId({params: {}, mode: 'production'})).rejects.toThrow(
200+
/No configuration exported/
201+
);
202+
});
192203
});
193204
});
194205

plugins/plugin-tools/src/config.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export const satelliteId = async (args: ConfigArgs): Promise<string> => {
3434
const junoConfigSatelliteId = async ({mode}: ConfigArgs): Promise<string> => {
3535
await assertJunoConfig();
3636

37+
const config = await readJunoConfig({mode});
38+
39+
if (config === undefined || !('satellite' in config)) {
40+
throw new JunoPluginError(`No configuration exported for ${mode}.`);
41+
}
42+
3743
const {
3844
satellite: {ids, satelliteId: deprecatedSatelliteId, id}
3945
} = await readJunoConfig({mode});
@@ -62,9 +68,15 @@ const containerSatelliteId = async ({mode}: ConfigArgs): Promise<string> => {
6268
return DOCKER_SATELLITE_ID;
6369
}
6470

71+
const config = await readJunoConfig({mode});
72+
73+
if (config == undefined || !('satellite' in config)) {
74+
return DOCKER_SATELLITE_ID;
75+
}
76+
6577
const {
6678
satellite: {ids}
67-
} = await readJunoConfig({mode});
79+
} = config;
6880

6981
return ids?.[MODE_DEVELOPMENT] ?? DOCKER_SATELLITE_ID;
7082
};

plugins/plugin-tools/src/init.spec.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -206,12 +206,19 @@ describe('init', () => {
206206
});
207207

208208
it('throws if satelliteId is missing in config in production', async () => {
209-
vi.spyOn(configLoader, 'readJunoConfig').mockResolvedValueOnce({
210-
satellite: {}
211-
} as unknown as JunoConfig);
209+
vi.spyOn(configLoader, 'readJunoConfig').mockImplementation(
210+
// eslint-disable-next-line require-await
211+
async () => ({satellite: {}}) as unknown as JunoConfig
212+
);
212213

213214
await expect(initConfig(args)).rejects.toThrow(
214215
'Your project needs a Satellite for production. Create one at https://console.juno.build and set its ID in your configuration file.'
215216
);
216217
});
218+
219+
it('throws if readJunoConfig returns undefined', async () => {
220+
vi.spyOn(configLoader, 'readJunoConfig').mockResolvedValue(undefined as unknown as JunoConfig);
221+
222+
await expect(initConfig(args)).rejects.toThrow('No configuration exported for production.');
223+
});
217224
});

0 commit comments

Comments
 (0)