File size: 920 Bytes
2276d59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { describe, it, expect, afterEach, vi } from 'vitest';
import request from 'supertest';
import app from '../../src/app.js';

// malformed json trips the global error handler
function badJson() {
  return request(app)
    .post('/telemetry')
    .set('Content-Type', 'application/json')
    .send('{ broken');
}

describe('global error handler', () => {
  afterEach(() => vi.unstubAllEnvs());

  it('hides internal details in production', async () => {
    vi.stubEnv('NODE_ENV', 'production');
    const res = await badJson();
    expect(res.status).toBe(500);
    expect(res.body.error).toBe('Internal Server Error');
    expect(res.body.details).toBeUndefined();
  });

  it('keeps details outside production for debugging', async () => {
    vi.stubEnv('NODE_ENV', 'development');
    const res = await badJson();
    expect(res.status).toBe(500);
    expect(typeof res.body.details).toBe('string');
  });
});