The Node.js® Website
at main 2.0 kB view raw
1import { formatLighthouseResults } from '..'; 2 3describe('formatLighthouseResults', () => { 4 const MOCK_VERCEL_PREVIEW_URL = `https://some.vercel.preview.url`; 5 6 const MOCK_LIGHTHOUSE_RESULT = `[ 7 { 8 "url": "${MOCK_VERCEL_PREVIEW_URL}/en", 9 "isRepresentativeRun": true, 10 "summary": { "performance": 0.99, "accessibility": 0.98, "best-practices": 1, "seo": 0.96, "pwa": 0.71 } 11 }, 12 { 13 "url": "${MOCK_VERCEL_PREVIEW_URL}/en/download", 14 "isRepresentativeRun": true, 15 "summary": { "performance": 0.49, "accessibility": 0.75, "best-practices": 1, "seo": 0.90, "pwa": 0.71 } 16 } 17 ]`; 18 19 const MOCK_LIGHTHOUSE_LINKS = `{ 20 "${MOCK_VERCEL_PREVIEW_URL}/en": "fake.url/to/result/1", 21 "${MOCK_VERCEL_PREVIEW_URL}/en/download" : "fake.url/to/result/2" 22 }`; 23 24 let mockCore, originalEnv; 25 26 beforeEach(() => { 27 mockCore = { setOutput: jest.fn() }; 28 originalEnv = process.env; 29 process.env = { 30 ...process.env, 31 LIGHTHOUSE_RESULT: MOCK_LIGHTHOUSE_RESULT, 32 LIGHTHOUSE_LINKS: MOCK_LIGHTHOUSE_LINKS, 33 VERCEL_PREVIEW_URL: MOCK_VERCEL_PREVIEW_URL, 34 }; 35 }); 36 37 afterEach(() => { 38 process.env = originalEnv; 39 }); 40 41 it('formats preview urls correctly', () => { 42 formatLighthouseResults({ core: mockCore }); 43 44 const expectations = [ 45 expect.stringContaining(`[/en](${MOCK_VERCEL_PREVIEW_URL}/en)`), 46 expect.stringContaining( 47 `[/en/download](${MOCK_VERCEL_PREVIEW_URL}/en/download)` 48 ), 49 ]; 50 51 expectations.forEach(expectation => { 52 expect(mockCore.setOutput).toBeCalledWith('comment', expectation); 53 }); 54 }); 55 56 it('formats stoplight colors correctly', () => { 57 formatLighthouseResults({ core: mockCore }); 58 59 const expectations = [ 60 expect.stringContaining(`🟢 90`), 61 expect.stringContaining(`🟠 75`), 62 expect.stringContaining(`🔴 49`), 63 ]; 64 65 expectations.forEach(expectation => { 66 expect(mockCore.setOutput).toBeCalledWith('comment', expectation); 67 }); 68 }); 69});