Quantcast
Channel: Active questions tagged svelte - Stack Overflow
Viewing all articles
Browse latest Browse all 1541

In Svelte with Vitest is there a way to mock the browser value in $app/environment

$
0
0

Seems like there should be a way to modify the value of browser. Typescript doesn't allow assignment eg browser = true; yielding error: Cannot assign to 'browser' because it is an import.ts(2632)

users.test.ts

import { describe, it, expect } from "vitest";import { isAdmin } from "./users";describe('isAdmin', () => {    it('should throw an error when called on the client side', () => {        expect(() => isAdmin("")).toThrowError('isAdmin should not be called on the client side');    });    it('should not throw when called from server', () => {        expect(() => isAdmin("")).not.toThrow();    });});

users.ts

import { browser } from '$app/environment';/** * System admin * @param userEmail  * @returns true if user is admin and can manage all events */export const isAdmin = (userEmail: string) => {    if (browser) {        throw new Error('isAdmin should not be called on the client side');    }    //...    return false;}

Viewing all articles
Browse latest Browse all 1541

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>