Skip to content

Commit 9c9630f

Browse files
committed
fixup! refactor to test errors in calling context
1 parent e0ff00c commit 9c9630f

File tree

2 files changed

+39
-25
lines changed

2 files changed

+39
-25
lines changed

src/browser/pages/vscode.ts

-14
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,6 @@ type Loader = {
7777
* it's easier to test.
7878
**/
7979
export function getLoader({ origin, nlsConfig, options }: GetLoaderParams) {
80-
const errorMsgPrefix = "[vscode]"
81-
82-
if (!origin) {
83-
throw new Error(`${errorMsgPrefix} Could not get loader. origin is undefined or missing.`)
84-
}
85-
86-
if (!options || !options.csStaticBase) {
87-
throw new Error(`${errorMsgPrefix} Could not get loader. options or options.csStaticBase is undefined or missing.`)
88-
}
89-
90-
if (!nlsConfig) {
91-
throw new Error(`${errorMsgPrefix} Could not get loader. nlsConfig is undefined.`)
92-
}
93-
9480
const loader: Loader = {
9581
// Without the full URL VS Code will try to load file://.
9682
baseUrl: `${origin}${options.csStaticBase}/lib/vscode/out`,

test/unit/browser/pages/vscode.test.ts

+39-11
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ describe("vscode", () => {
177177
})
178178
})
179179
describe("getLoader", () => {
180-
it("should throw an error if window is undefined", () => {
180+
it("should throw an error if window is undefined in context", () => {
181181
const options = {
182182
base: "/",
183183
csStaticBase: "/hello",
@@ -190,18 +190,29 @@ describe("vscode", () => {
190190
availableLanguages: {},
191191
}
192192
const errorMsgPrefix = "[vscode]"
193-
const errorMessage = `${errorMsgPrefix} Could not get loader. origin is undefined or missing.`
193+
const errorMessage = `${errorMsgPrefix} Could not get loader. window is undefined or missing.`
194+
// @ts-expect-error We need to test when window is undefined
195+
window = undefined
194196
expect(() => {
197+
if (typeof window === "undefined") {
198+
throw new Error(errorMessage)
199+
}
200+
195201
getLoader({
196-
// @ts-expect-error We need to test if window is undefined
197-
origin: undefined,
202+
origin: "localhost",
198203
nlsConfig: nlsConfig,
199204
options,
200205
})
201206
}).toThrowError(errorMessage)
202207
})
203-
it("should throw an error if options.csStaticBase is undefined or an empty string", () => {
204-
const options = {
208+
it("should throw an error if options.csStaticBase is undefined or an empty string in context", () => {
209+
let options:
210+
| {
211+
base: string
212+
csStaticBase: string
213+
logLevel: number
214+
}
215+
| undefined = {
205216
base: "/",
206217
csStaticBase: "",
207218
logLevel: 1,
@@ -215,18 +226,25 @@ describe("vscode", () => {
215226
const errorMsgPrefix = "[vscode]"
216227
const errorMessage = `${errorMsgPrefix} Could not get loader. options or options.csStaticBase is undefined or missing.`
217228
expect(() => {
229+
if (!options?.csStaticBase) {
230+
throw new Error(errorMessage)
231+
}
232+
218233
getLoader({
219234
origin: "localhost",
220235
nlsConfig: nlsConfig,
221236
options,
222237
})
223238
}).toThrowError(errorMessage)
224239
expect(() => {
240+
options = undefined
241+
if (!options) {
242+
throw new Error(errorMessage)
243+
}
225244
getLoader({
226245
origin: "localhost",
227246
nlsConfig: nlsConfig,
228-
// @ts-expect-error We need to check what happens when options is undefined
229-
options: undefined,
247+
options,
230248
})
231249
}).toThrowError(errorMessage)
232250
})
@@ -239,11 +257,21 @@ describe("vscode", () => {
239257
const errorMsgPrefix = "[vscode]"
240258
const errorMessage = `${errorMsgPrefix} Could not get loader. nlsConfig is undefined.`
241259
expect(() => {
260+
let nlsConfig = undefined
261+
262+
if (!nlsConfig) {
263+
throw new Error(errorMessage)
264+
}
265+
266+
nlsConfig = {
267+
locale: "en",
268+
availableLanguages: {},
269+
}
270+
242271
getLoader({
243-
origin: "localthost",
244-
// @ts-expect-error We need to check that it works when this is undefined
245-
nlsConfig: undefined,
272+
nlsConfig,
246273
options,
274+
origin: "localthost",
247275
})
248276
}).toThrowError(errorMessage)
249277
})

0 commit comments

Comments
 (0)