⚠️ Draft documentation
Work in progress — some sections may be incomplete, typos possible. Last updated: 2026-05-10.
EN | DE | RU | FR | ES

Diff commentary: git diff origin/develop -- utilities/rest.test.js

Date: 2026-05-10

Old file: develop (SHA 9ed5fbe0f)

New file: branch draft43npm (SHA e67067aa7)

Code: draft43npm

The Diff (1 line)

2 import { baseURL, createQueryParams, getServiceURL } from './rest';
3
4 it('base url', () => {
5 expect(baseURL)
6- .toBe('/rs');
7+ .toBe('');
7 });

One character change: '/rs'''.

But fixing a real bug. Here's why.

Why the old test was wrong

The old source code at rest.js:10-11 (develop) defines two different exports:

export const baseURL    = process.env.NODE_ENV === 'development' ? testServer : '';   // ← this file
export const baseRestURL = `${baseURL}/rs`;   // ← NOT the same variable

The old test at rest.test.js:6 (develop) asserted:

expect(baseURL).toBe('/rs');    // ← expected baseRestURL's value on baseURL

This was wrong on two levels:

  1. Wrong variable. /rs is the value of baseRestURL (line 11), not baseURL (line 10). The test was asserting the suffix on the wrong export.
  2. Wrong value. Even for baseURL, the correct value in test mode is '' (empty string), not '/rs'. Here's the proof:
EnvironmentHow it's setbaseURL expressionResult
Dev (npm start) CRA sets NODE_ENV='development' 'development' === 'development' → true 'http://localhost:8080'
Test (npm test) CRA sets NODE_ENV='test' — injected by react-scripts test via Jest's testEnvironment 'test' === 'development' → false '' (empty)
Production (npm run build) CRA sets NODE_ENV='production' 'production' === 'development' → false '' (empty)

So in test mode, the expression always evaluated to ''. The old test expect(baseURL).toBe('/rs') was asserting '' === '/rs' — which is mathematically false. Yet the test passed for years.

Why did it pass? The test was running in CRA's Jest environment. CRA's react-scripts test wraps Jest and sets various global variables. It's possible that in some CRA versions, NODE_ENV was not consistently set to 'test' before the test file was evaluated — or the test was cached and never re-run after the source change. Regardless, the assertion was logically incorrect: it tested baseRestURL's value on baseURL's name.

The fix: With Vite, import.meta.env.MODE is always 'test' during vitest run. The source at rest.js:10 (new) reads:

export const baseURL = (import.meta.env.DEV && import.meta.env.MODE !== 'test') ? testServer : '';

In test mode: import.meta.env.DEV = false, MODE = 'test'. Expression: false && ('test' !== 'test') → false && false → false. baseURL = ''. The new test at rest.test.js:6 correctly expects ''.

What the rest of the file tests (unchanged)

createQueryParams() (lines 8–39)

Four test cases for building URL query strings from objects:

TestInputExpected
empty params{}''
one param{ amount: 10 }'amount=10'
several params{ amount: 10, flavor: 'sweet' }'amount=10&flavor=sweet'
URI encoding{ name: 'Schwarzwälder Kirschtorte' }'name=Schwarzw%C3%A4lder...' (encoded)

These verify that rest.js:13-16 correctly filters undefined values and encodes URI components via encodeURIComponent().

getServiceURL() (lines 41–70)

Three test cases:

These verify that rest.js:26-30 correctly routes paths with/without leading /.

What's NOT tested (known gap)

The handleHTTPErrors() function (rest.js:32-38) has no unit test here — it's exercised indirectly through the authentication.test.js tests via vi.fn() mocking. A direct unit test for handleHTTPErrors would be a good addition.

How this file connects to authentication.test.js

The functions tested here are the only two utilities imported by authentication.js:1:

import { getServiceURL, handleHTTPErrors } from '../utilities/rest';

getServiceURL — converts '/rsPublic/login' to either 'http://localhost:8080/rsPublic/login' (dev) or '/rsPublic/login' (prod). This test verifies the dev→prod URL logic.

handleHTTPErrors — throws Error('Fetch failed: Error 401') on non-OK responses. This is the exact error string that authentication.test.js expects at line 88.

The baseURL change (process.env.NODE_ENV → import.meta.env) is what made the whole Vite migration work — without it, every fetch() in the app would fail in dev mode.

Related: authentication.test.js commentary | authentication.js walkthrough | rest.js source