diff --git a/packages/rstack/src/config.ts b/packages/rstack/src/config.ts index 56c7507..3cfa9a5 100644 --- a/packages/rstack/src/config.ts +++ b/packages/rstack/src/config.ts @@ -103,23 +103,26 @@ export const define: Define = { export const loadRstackConfig = async (): Promise => { const state = getConfigState(); + state.configs = {}; - await loadConfig({ - loader: 'native', - exportName: false, - ...(state.configPath !== undefined - ? { path: state.configPath } - : { - configFileNames: [ - 'rstack.config.ts', - 'rstack.config.js', - 'rstack.config.mts', - 'rstack.config.mjs', - ], - }), - }); + try { + await loadConfig({ + loader: 'native', + exportName: false, + ...(state.configPath !== undefined + ? { path: state.configPath } + : { + configFileNames: [ + 'rstack.config.ts', + 'rstack.config.js', + 'rstack.config.mts', + 'rstack.config.mjs', + ], + }), + }); - const result = state.configs; - state.configs = {}; - return result; + return state.configs; + } finally { + state.configs = {}; + } }; diff --git a/packages/rstack/test/config/load-state/index.test.ts b/packages/rstack/test/config/load-state/index.test.ts new file mode 100644 index 0000000..1f4e34f --- /dev/null +++ b/packages/rstack/test/config/load-state/index.test.ts @@ -0,0 +1,17 @@ +import path from 'node:path'; +import { expect, test } from 'rstack/test'; +import { getConfigState, loadRstackConfig } from '../../../src/config.ts'; + +test('should reset config state before and after loading', async () => { + const state = getConfigState(); + state.configs = { app: {} }; + state.configPath = path.join(import.meta.dirname, 'rstack.config.ts'); + + try { + await expect(loadRstackConfig()).rejects.toThrow('test config error'); + expect(state.configs).toEqual({}); + } finally { + state.configs = {}; + delete state.configPath; + } +}); diff --git a/packages/rstack/test/config/load-state/rstack.config.ts b/packages/rstack/test/config/load-state/rstack.config.ts new file mode 100644 index 0000000..7720867 --- /dev/null +++ b/packages/rstack/test/config/load-state/rstack.config.ts @@ -0,0 +1,4 @@ +import { define } from 'rstack'; + +define.app({}); +throw new Error('test config error');