|
| 1 | +import { describe, expect, test, vi } from 'vitest' |
| 2 | +import { swcPlugin, type SwcPluginConfig } from './swc' |
| 3 | +import { localRequire } from '../utils' |
| 4 | + |
| 5 | +vi.mock('../utils') |
| 6 | + |
| 7 | +const getFixture = async (opts: Partial<SwcPluginConfig> = {}) => { |
| 8 | + const swc = { |
| 9 | + transformFile: vi.fn().mockResolvedValue({ |
| 10 | + code: 'source-code', |
| 11 | + map: JSON.stringify({ |
| 12 | + sources: ['file:///path/to/file.ts'], |
| 13 | + }), |
| 14 | + }), |
| 15 | + } |
| 16 | + |
| 17 | + const logger = { |
| 18 | + warn: vi.fn(), |
| 19 | + error: vi.fn(), |
| 20 | + info: vi.fn(), |
| 21 | + } |
| 22 | + |
| 23 | + const build = { |
| 24 | + initialOptions: { |
| 25 | + keepNames: true, |
| 26 | + }, |
| 27 | + onLoad: vi.fn(), |
| 28 | + } |
| 29 | + |
| 30 | + vi.mocked(localRequire).mockReturnValue(swc) |
| 31 | + |
| 32 | + const plugin = swcPlugin({ |
| 33 | + ...opts, |
| 34 | + logger: logger as never, |
| 35 | + }) |
| 36 | + |
| 37 | + await plugin.setup(build as never) |
| 38 | + |
| 39 | + const onLoad = build.onLoad.mock.calls[0][1] as Function |
| 40 | + |
| 41 | + return { swc, onLoad, logger, build } |
| 42 | +} |
| 43 | +describe('swcPlugin', () => { |
| 44 | + test('swcPlugin transforms TypeScript code with decorators and default plugin swc option', async () => { |
| 45 | + const { swc, onLoad } = await getFixture() |
| 46 | + |
| 47 | + await onLoad({ |
| 48 | + path: 'file.ts', |
| 49 | + }) |
| 50 | + |
| 51 | + expect(swc.transformFile).toHaveBeenCalledWith('file.ts', { |
| 52 | + configFile: false, |
| 53 | + jsc: { |
| 54 | + keepClassNames: true, |
| 55 | + parser: { |
| 56 | + decorators: true, |
| 57 | + syntax: 'typescript', |
| 58 | + }, |
| 59 | + target: 'es2022', |
| 60 | + transform: { |
| 61 | + decoratorMetadata: true, |
| 62 | + legacyDecorator: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + sourceMaps: true, |
| 66 | + swcrc: false, |
| 67 | + }) |
| 68 | + }) |
| 69 | + test('swcPlugin transforms TypeScript code and use given plugin swc option', async () => { |
| 70 | + const { swc, onLoad } = await getFixture({ |
| 71 | + jsc: { |
| 72 | + transform: { |
| 73 | + useDefineForClassFields: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }) |
| 77 | + |
| 78 | + await onLoad({ |
| 79 | + path: 'file.ts', |
| 80 | + }) |
| 81 | + |
| 82 | + expect(swc.transformFile).toHaveBeenCalledWith('file.ts', { |
| 83 | + configFile: false, |
| 84 | + jsc: { |
| 85 | + keepClassNames: true, |
| 86 | + parser: { |
| 87 | + decorators: true, |
| 88 | + syntax: 'typescript', |
| 89 | + }, |
| 90 | + target: 'es2022', |
| 91 | + transform: { |
| 92 | + decoratorMetadata: true, |
| 93 | + legacyDecorator: true, |
| 94 | + useDefineForClassFields: true, |
| 95 | + }, |
| 96 | + }, |
| 97 | + sourceMaps: true, |
| 98 | + swcrc: false, |
| 99 | + }) |
| 100 | + }) |
| 101 | +}) |
0 commit comments