|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | + |
| 3 | +import { Parser } from '../parser.js'; |
| 4 | + |
| 5 | +const fs = vi.hoisted(() => ({ |
| 6 | + existsSync: vi.fn(), |
| 7 | + readFileSync: vi.fn() |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('node:fs', () => fs); |
| 11 | + |
| 12 | +describe('Parser', () => { |
| 13 | + let parser: Parser; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + parser = new Parser(); |
| 17 | + fs.existsSync.mockReturnValue(true); |
| 18 | + }); |
| 19 | + |
| 20 | + describe('parseUserConfig', () => { |
| 21 | + it('should return an error if the provided file does not exist', () => { |
| 22 | + fs.existsSync.mockReturnValueOnce(false); |
| 23 | + expect(parser.parseUserConfig('config.ts')).toMatchObject({ |
| 24 | + error: { |
| 25 | + message: `Config file does not exist: config.ts` |
| 26 | + } |
| 27 | + }); |
| 28 | + }); |
| 29 | + it('should return an error if the source code does not contain a default export', () => { |
| 30 | + const sourceCode = "const config = { value: 'foo' };"; |
| 31 | + fs.readFileSync.mockReturnValueOnce(sourceCode); |
| 32 | + expect(parser.parseUserConfig('config.ts')).toMatchObject({ |
| 33 | + error: { |
| 34 | + message: "Source file 'config.ts' does not include a default export symbol" |
| 35 | + } |
| 36 | + }); |
| 37 | + }); |
| 38 | + it('should return an error if the default export does not reference anything', () => { |
| 39 | + const sourceCode = 'export default config;'; |
| 40 | + fs.readFileSync.mockReturnValueOnce(sourceCode); |
| 41 | + expect(parser.parseUserConfig('config.ts')).toMatchObject({ |
| 42 | + error: { |
| 43 | + message: "Default export symbol in 'config.ts' has no declarations" |
| 44 | + } |
| 45 | + }); |
| 46 | + }); |
| 47 | + it('should return an error if the default export has multiple references', () => { |
| 48 | + const sourceCode = 'var config = {}; var config = {}; export default config;'; |
| 49 | + fs.readFileSync.mockReturnValueOnce(sourceCode); |
| 50 | + expect(parser.parseUserConfig('config.ts')).toMatchObject({ |
| 51 | + error: { |
| 52 | + message: "Default export symbol in 'config.ts' has multiple declarations (2)" |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + it('should return ok if the config can be parsed', () => { |
| 57 | + const sourceCode = 'const config = {}; export default config;'; |
| 58 | + fs.readFileSync.mockReturnValueOnce(sourceCode); |
| 59 | + const result = parser.parseUserConfig('config.ts'); |
| 60 | + expect(result.isOk()).toBe(true); |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
0 commit comments