|
7 | 7 |
|
8 | 8 | /* eslint-env jest */
|
9 | 9 |
|
10 |
| -const {replaceUrlPatterns} = require('@lhci/utils/src/saved-reports.js'); |
| 10 | +const fs = require('fs'); |
| 11 | +const path = require('path'); |
| 12 | +const {withTmpDir} = require('../../cli/test/test-utils.js'); |
| 13 | +const {replaceUrlPatterns, saveLHR} = require('@lhci/utils/src/saved-reports.js'); |
11 | 14 |
|
12 |
| -describe('replaceUrlPatterns', () => { |
| 15 | +describe('#replaceUrlPatterns', () => { |
13 | 16 | it('should replace basic patterns', () => {
|
14 | 17 | const patterns = ['s/foo/bar/'];
|
15 | 18 | expect(replaceUrlPatterns('https://foo.com', patterns)).toEqual('https://bar.com');
|
@@ -45,3 +48,30 @@ describe('replaceUrlPatterns', () => {
|
45 | 48 | expect(() => replaceUrlPatterns('foo', ['nothing'])).toThrow();
|
46 | 49 | });
|
47 | 50 | });
|
| 51 | + |
| 52 | +describe('#saveLHR', () => { |
| 53 | + it('should save the lhr to json', async () => { |
| 54 | + await withTmpDir(dir => { |
| 55 | + saveLHR(JSON.stringify({lighthouseVersion: '5.6.0'}), dir); |
| 56 | + const files = fs.readdirSync(dir); |
| 57 | + expect(files.map(name => name.replace(/-\d+/, '-XXX'))).toContain('lhr-XXX.json'); |
| 58 | + |
| 59 | + const jsonFilePath = path.join(dir, files.find(f => f.endsWith('.json'))); |
| 60 | + const contents = fs.readFileSync(jsonFilePath, 'utf8'); |
| 61 | + expect(contents).toEqual(`{"lighthouseVersion":"5.6.0"}`); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should save the lhr to html', async () => { |
| 66 | + await withTmpDir(dir => { |
| 67 | + saveLHR(JSON.stringify({lighthouseVersion: '5.6.0'}), dir); |
| 68 | + const files = fs.readdirSync(dir); |
| 69 | + expect(files.map(name => name.replace(/-\d+/, '-XXX'))).toContain('lhr-XXX.html'); |
| 70 | + |
| 71 | + const jsonFilePath = path.join(dir, files.find(f => f.endsWith('.html'))); |
| 72 | + const contents = fs.readFileSync(jsonFilePath, 'utf8'); |
| 73 | + expect(contents).toMatch(/<!DOCTYPE html>/i); |
| 74 | + expect(contents).toMatch(/__LIGHTHOUSE_JSON__ = /); |
| 75 | + }); |
| 76 | + }); |
| 77 | +}); |
0 commit comments