Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions webview-ui/src/utils/__tests__/sourceMapInitializer.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { exposeSourceMapsForDebugging } from "../sourceMapInitializer"

describe("sourceMapInitializer", () => {
describe("exposeSourceMapsForDebugging", () => {
let originalNodeEnv: string | undefined

beforeEach(() => {
originalNodeEnv = process.env.NODE_ENV
// Clear any existing debugging functions
delete (window as any).__testSourceMaps
delete (window as any).__applySourceMaps
delete (window as any).__checkSourceMap
})

afterEach(() => {
process.env.NODE_ENV = originalNodeEnv
// Clean up any debugging functions
delete (window as any).__testSourceMaps
delete (window as any).__applySourceMaps
delete (window as any).__checkSourceMap
})

it("should not expose debugging functions in production mode", () => {
// Set production mode
process.env.NODE_ENV = "production"

// Call the function
exposeSourceMapsForDebugging()

// Verify that debugging functions are NOT exposed
expect((window as any).__testSourceMaps).toBeUndefined()
expect((window as any).__applySourceMaps).toBeUndefined()
expect((window as any).__checkSourceMap).toBeUndefined()
})

it("should expose debugging functions in development mode", () => {
// Set development mode
process.env.NODE_ENV = "development"

// Call the function
exposeSourceMapsForDebugging()

// Verify that debugging functions ARE exposed
expect((window as any).__testSourceMaps).toBeDefined()
expect((window as any).__applySourceMaps).toBeDefined()
expect((window as any).__checkSourceMap).toBeDefined()
expect(typeof (window as any).__testSourceMaps).toBe("function")
expect(typeof (window as any).__applySourceMaps).toBe("function")
expect(typeof (window as any).__checkSourceMap).toBe("function")
})

it("should expose debugging functions in test mode", () => {
// Set test mode
process.env.NODE_ENV = "test"

// Call the function
exposeSourceMapsForDebugging()

// Verify that debugging functions ARE exposed
expect((window as any).__testSourceMaps).toBeDefined()
expect((window as any).__applySourceMaps).toBeDefined()
expect((window as any).__checkSourceMap).toBeDefined()
})

it("should not throw errors when called multiple times in production", () => {
// Set production mode
process.env.NODE_ENV = "production"

// Should not throw when called multiple times
expect(() => {
exposeSourceMapsForDebugging()
exposeSourceMapsForDebugging()
exposeSourceMapsForDebugging()
}).not.toThrow()

// Functions should still not be exposed
expect((window as any).__testSourceMaps).toBeUndefined()
expect((window as any).__applySourceMaps).toBeUndefined()
expect((window as any).__checkSourceMap).toBeUndefined()
})
})
})
2 changes: 1 addition & 1 deletion webview-ui/src/utils/sourceMapInitializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export function initializeSourceMaps(): void {
* Expose source maps on the window object for debugging
*/
export function exposeSourceMapsForDebugging(): void {
if (process.env.NODE_ENV !== "production") {
if (process.env.NODE_ENV === "production") {
return
}

Expand Down