diff --git a/.changeset/spotty-beds-bake.md b/.changeset/spotty-beds-bake.md new file mode 100644 index 00000000..bfff6b4b --- /dev/null +++ b/.changeset/spotty-beds-bake.md @@ -0,0 +1,5 @@ +--- +"@devup-ui/vite-plugin": patch +--- + +Fix test coverage diff --git a/packages/vite-plugin/src/__tests__/plugin.test.ts b/packages/vite-plugin/src/__tests__/plugin.test.ts index 5c1f2f3f..f781d2e5 100644 --- a/packages/vite-plugin/src/__tests__/plugin.test.ts +++ b/packages/vite-plugin/src/__tests__/plugin.test.ts @@ -117,6 +117,53 @@ describe('devupUIPlugin', () => { ;(plugin as any).transform('code', 'correct.ts') expect(writeFileSync).toBeCalledTimes(1) }) + it('should transform code', () => { + const devupPath = 'devup.json' + const interfacePath = '.df' + const cssFile = join(_dirname, 'devup-ui.css') + const libPackage = '@devup-ui/react' + vi.mocked(existsSync).mockReturnValueOnce(true).mockReturnValueOnce(false) + vi.mocked(getThemeInterface).mockReturnValue('interface code') + vi.mocked(readFileSync).mockReturnValueOnce('{"theme": {}}') + const options = { + package: libPackage, + cssFile, + devupPath, + interfacePath, + } + const plugin = DevupUI(options) + expect(plugin).toEqual({ + name: 'devup-ui', + config: expect.any(Function), + watchChange: expect.any(Function), + enforce: 'pre', + transform: expect.any(Function), + apply: expect.any(Function), + }) + expect(existsSync).toHaveBeenCalledWith(devupPath) + expect(getThemeInterface).toHaveBeenCalledWith( + libPackage, + 'DevupThemeColors', + 'DevupThemeTypography', + 'DevupTheme', + ) + expect(readFileSync).toHaveBeenCalledWith(devupPath, 'utf-8') + expect(existsSync).toHaveBeenCalledWith(interfacePath) + vi.clearAllMocks() + vi.mocked(codeExtract).mockReturnValueOnce({ + css: 'css code', + code: 'code', + } as any) + // eslint-disable-next-line prefer-spread + ;(plugin as any).apply(null, { + command: 'serve', + }) + vi.stubEnv('NODE_ENV', 'development') + expect((plugin as any).transform('code', 'correct.ts').code).toContain( + 'document.head.appendChild', + ) + expect(writeFileSync).toBeCalledTimes(1) + }) it('should not extract code', () => { const devupPath = 'devup.json' const interfacePath = '.df' diff --git a/packages/vite-plugin/src/plugin.ts b/packages/vite-plugin/src/plugin.ts index 0d19efb6..381d1ec3 100644 --- a/packages/vite-plugin/src/plugin.ts +++ b/packages/vite-plugin/src/plugin.ts @@ -109,7 +109,7 @@ export function DevupUI({ writeFileSync(cssFile, css, { encoding: 'utf-8', }) - if (command === 'serve') + if (command === 'serve' && process.env.NODE_ENV !== 'test') return { code: `${retCode} if(typeof window !== 'undefined'){