|
| 1 | +import { describe, expect, it, vi } from 'vitest' |
| 2 | +import { Context, ifDirective, MessageDirective } from '../src' |
| 3 | + |
| 4 | +describe('message', () => { |
| 5 | + it('should log error message', () => { |
| 6 | + const context = new Context({ |
| 7 | + // @ts-expect-error ignore |
| 8 | + directives: [MessageDirective], |
| 9 | + }) |
| 10 | + |
| 11 | + const loggerErrorSpy = vi.spyOn(context.logger, 'error') |
| 12 | + const code = '// #error This is an error message' |
| 13 | + |
| 14 | + context.transform(code, 'test.js') |
| 15 | + |
| 16 | + expect(loggerErrorSpy).toHaveBeenCalledWith('This is an error message', { timestamp: true }) |
| 17 | + }) |
| 18 | + |
| 19 | + it('should log warning message', () => { |
| 20 | + const context = new Context({ |
| 21 | + // @ts-expect-error ignore |
| 22 | + directives: [MessageDirective], |
| 23 | + }) |
| 24 | + |
| 25 | + const loggerWarnSpy = vi.spyOn(context.logger, 'warn') |
| 26 | + const code = '// #warning This is a warning message' |
| 27 | + |
| 28 | + context.transform(code, 'test.js') |
| 29 | + |
| 30 | + expect(loggerWarnSpy).toHaveBeenCalledWith('This is a warning message', { timestamp: true }) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should log info message', () => { |
| 34 | + const context = new Context({ |
| 35 | + // @ts-expect-error ignore |
| 36 | + directives: [MessageDirective], |
| 37 | + }) |
| 38 | + |
| 39 | + const loggerInfoSpy = vi.spyOn(context.logger, 'info') |
| 40 | + const code = '// #info This is an info message' |
| 41 | + |
| 42 | + context.transform(code, 'test.js') |
| 43 | + |
| 44 | + expect(loggerInfoSpy).toHaveBeenCalledWith('This is an info message', { timestamp: true }) |
| 45 | + }) |
| 46 | + |
| 47 | + it('with if directive', () => { |
| 48 | + const context = new Context({ |
| 49 | + // @ts-expect-error ignore |
| 50 | + directives: [ifDirective, MessageDirective], |
| 51 | + }) |
| 52 | + context.env.DEV = true |
| 53 | + |
| 54 | + const loggerInfoSpy = vi.spyOn(context.logger, 'info') |
| 55 | + const code = ` |
| 56 | + // #if DEV |
| 57 | + // #info DEV mode is on |
| 58 | + // #endif |
| 59 | +
|
| 60 | + // #if !DEV |
| 61 | + // #info DEV mode is off |
| 62 | + // #endif |
| 63 | + ` |
| 64 | + |
| 65 | + context.transform(code, 'test.js') |
| 66 | + expect(loggerInfoSpy).toHaveBeenCalledWith('DEV mode is on', { timestamp: true }) |
| 67 | + }) |
| 68 | +}) |
0 commit comments