|
| 1 | +import { vol } from 'memfs'; |
| 2 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 3 | +import { executePlugin } from '@code-pushup/core'; |
| 4 | +import { |
| 5 | + auditSchema, |
| 6 | + categoryRefSchema, |
| 7 | + pluginConfigSchema, |
| 8 | +} from '@code-pushup/models'; |
| 9 | +import { MEMFS_VOLUME } from '@code-pushup/testing-utils'; |
| 10 | +import { |
| 11 | + PluginOptions, |
| 12 | + audits, |
| 13 | + create, |
| 14 | + recommendedRefs, |
| 15 | + pluginSlug as slug, |
| 16 | +} from './file-size.plugin'; |
| 17 | + |
| 18 | +const projectJson = JSON.stringify( |
| 19 | + { |
| 20 | + test: 42, |
| 21 | + arr: [1, 2, 3], |
| 22 | + obj: { |
| 23 | + test: 42, |
| 24 | + }, |
| 25 | + }, |
| 26 | + null, |
| 27 | + 2, |
| 28 | +); |
| 29 | +const testJs = ` |
| 30 | + const str = 'Hello World' |
| 31 | + const num = 42; |
| 32 | + const obj = ${projectJson}; |
| 33 | + `; |
| 34 | + |
| 35 | +describe('create', () => { |
| 36 | + const baseOptions: PluginOptions = { |
| 37 | + directory: '/', |
| 38 | + }; |
| 39 | + |
| 40 | + beforeEach(() => { |
| 41 | + vol.fromJSON( |
| 42 | + { |
| 43 | + 'project.json': projectJson, |
| 44 | + 'src/test.js': testJs, |
| 45 | + }, |
| 46 | + MEMFS_VOLUME, |
| 47 | + ); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should return valid PluginConfig', () => { |
| 51 | + const pluginConfig = create(baseOptions); |
| 52 | + expect(() => pluginConfigSchema.parse(pluginConfig)).not.toThrow(); |
| 53 | + expect(pluginConfig).toEqual({ |
| 54 | + audits, |
| 55 | + description: |
| 56 | + 'A plugin to measure and assert filesize of files in a directory.', |
| 57 | + icon: 'javascript', |
| 58 | + runner: expect.any(Function), |
| 59 | + slug, |
| 60 | + title: 'File Size', |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should return PluginConfig that executes correctly', async () => { |
| 65 | + const pluginConfig = create(baseOptions); |
| 66 | + await expect(executePlugin(pluginConfig)).resolves.toMatchObject({ |
| 67 | + description: |
| 68 | + 'A plugin to measure and assert filesize of files in a directory.', |
| 69 | + slug, |
| 70 | + title: 'File Size', |
| 71 | + duration: expect.any(Number), |
| 72 | + date: expect.any(String), |
| 73 | + audits: expect.any(Array), |
| 74 | + }); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should use pattern', async () => { |
| 78 | + const pluginConfig = create({ |
| 79 | + ...baseOptions, |
| 80 | + pattern: /\.js$/, |
| 81 | + }); |
| 82 | + const { audits: auditOutputs } = await executePlugin(pluginConfig); |
| 83 | + |
| 84 | + expect(auditOutputs).toHaveLength(1); |
| 85 | + expect(auditOutputs[0]?.score).toBe(1); |
| 86 | + expect(auditOutputs[0]?.details?.issues).toHaveLength(1); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should use budget', async () => { |
| 90 | + const pluginConfig = create({ |
| 91 | + ...baseOptions, |
| 92 | + budget: 0, |
| 93 | + }); |
| 94 | + const { audits: auditOutputs } = await executePlugin(pluginConfig); |
| 95 | + |
| 96 | + expect(auditOutputs).toHaveLength(1); |
| 97 | + expect(auditOutputs[0]?.score).toBe(0); |
| 98 | + expect(auditOutputs[0]?.details?.issues).toHaveLength(2); |
| 99 | + }); |
| 100 | +}); |
| 101 | + |
| 102 | +describe('audits', () => { |
| 103 | + it.each(audits)('should be a valid audit meta info', audit => { |
| 104 | + expect(() => auditSchema.parse(audit)).not.toThrow(); |
| 105 | + }); |
| 106 | +}); |
| 107 | + |
| 108 | +describe('recommendedRefs', () => { |
| 109 | + it.each(recommendedRefs)( |
| 110 | + 'should be a valid category reference', |
| 111 | + categoryRef => { |
| 112 | + expect(() => categoryRefSchema.parse(categoryRef)).not.toThrow(); |
| 113 | + }, |
| 114 | + ); |
| 115 | +}); |
0 commit comments