Skip to content

Commit 3fede9d

Browse files
committed
test(website): skip wasm integration outside browsers
1 parent 7202d47 commit 3fede9d

2 files changed

Lines changed: 22 additions & 6 deletions

File tree

website/assets/js/goscript-wasm.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33

44
let wasmReady = false
55
let wasmError = null
6+
const hasBrowserRuntime =
7+
typeof window !== 'undefined' && typeof document !== 'undefined'
68

79
// Initialize the Go WASM runtime
810
async function initGoWasm() {
11+
if (!hasBrowserRuntime) {
12+
return
13+
}
14+
915
// Load wasm_exec.js if not already loaded
1016
if (!window.Go) {
1117
await new Promise((resolve, reject) => {
@@ -58,6 +64,9 @@ async function initGoWasm() {
5864

5965
// Compile Go source code to TypeScript
6066
export async function compileGoToTypeScript(goSource, packageName = 'main') {
67+
if (!hasBrowserRuntime) {
68+
throw new Error('GoScript WASM compiler requires a browser runtime')
69+
}
6170
if (!wasmReady) {
6271
if (wasmError) {
6372
throw wasmError
@@ -85,8 +94,10 @@ export function getCompilerError() {
8594
}
8695

8796
// Initialize and export a promise that resolves when ready
88-
export const ready = initGoWasm().catch((err) => {
89-
wasmError = err
90-
console.error('Failed to initialize GoScript WASM compiler:', err)
91-
throw err
92-
})
97+
export const ready = hasBrowserRuntime
98+
? initGoWasm().catch((err) => {
99+
wasmError = err
100+
console.error('Failed to initialize GoScript WASM compiler:', err)
101+
throw err
102+
})
103+
: Promise.resolve()

website/tests/wasm-integration.test.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { expect, test, describe, beforeAll } from 'vitest'
22
import { ready, compileGoToTypeScript, isCompilerReady } from '../assets/js/goscript-wasm.js'
33

4+
const describeWasm =
5+
typeof window === 'undefined' || typeof document === 'undefined'
6+
? describe.skip
7+
: describe
8+
49
// This test actually loads the WASM compiler and tests compilation
5-
describe('WASM Integration', () => {
10+
describeWasm('WASM Integration', () => {
611
beforeAll(async () => {
712
// Wait for WASM to be ready with a longer timeout
813
await ready

0 commit comments

Comments
 (0)