|
| 1 | +import { Options } from "@swc/core"; |
| 2 | +import handleCompile from "../dirWorker"; |
| 3 | +import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options"; |
| 4 | +import * as utilModule from '../util'; |
| 5 | + |
| 6 | +type HandleCompileOptions = { |
| 7 | + cliOptions: CliOptions; |
| 8 | + swcOptions: Options; |
| 9 | + sync: false, |
| 10 | + outDir: "outDir", |
| 11 | + filename: string, |
| 12 | + outFileExtension?: string; |
| 13 | +} |
| 14 | + |
| 15 | +const createHandleCompileOptions = (options?: Partial<HandleCompileOptions>): HandleCompileOptions => ({ |
| 16 | + cliOptions: { |
| 17 | + outDir: "", |
| 18 | + outFile: "", |
| 19 | + filename: "", |
| 20 | + stripLeadingPaths: false, |
| 21 | + filenames: [], |
| 22 | + sync: false, |
| 23 | + workers: undefined, |
| 24 | + sourceMapTarget: undefined, |
| 25 | + extensions: [], |
| 26 | + watch: false, |
| 27 | + copyFiles: false, |
| 28 | + outFileExtension: "", |
| 29 | + includeDotfiles: false, |
| 30 | + deleteDirOnStart: false, |
| 31 | + quiet: true, |
| 32 | + only: [], |
| 33 | + ignore: [], |
| 34 | + }, |
| 35 | + swcOptions: {}, |
| 36 | + sync: false, |
| 37 | + outDir: "outDir", |
| 38 | + filename: "", |
| 39 | + ...options, |
| 40 | +}); |
| 41 | + |
| 42 | +jest.mock('../util', () => ({ |
| 43 | + ...jest.requireActual("../util"), |
| 44 | + compile: jest.fn(), |
| 45 | +})); |
| 46 | + |
| 47 | +describe("dirWorker", () => { |
| 48 | + it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => { |
| 49 | + const filename = 'test'; |
| 50 | + const options = createHandleCompileOptions({ |
| 51 | + filename: `${filename}.ts` |
| 52 | + }); |
| 53 | + |
| 54 | + try { |
| 55 | + await handleCompile(options); |
| 56 | + } catch (err) { |
| 57 | + // We don't care about the error in this test, we want to make sure that "compile" was called |
| 58 | + } |
| 59 | + |
| 60 | + // Assert that subFunction was called with the correct parameter |
| 61 | + expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${DEFAULT_OUT_FILE_EXTENSION}`); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("dirWorker", () => { |
| 66 | + it('should call "compile" with "outFileExtension" when undefined', async () => { |
| 67 | + const filename = 'test'; |
| 68 | + const options = createHandleCompileOptions({ |
| 69 | + filename: `${filename}.ts`, |
| 70 | + outFileExtension: 'cjs' |
| 71 | + }); |
| 72 | + |
| 73 | + try { |
| 74 | + await handleCompile(options); |
| 75 | + } catch (err) { |
| 76 | + // We don't care about the error in this test, we want to make sure that "compile" was called |
| 77 | + } |
| 78 | + |
| 79 | + // Assert that subFunction was called with the correct parameter |
| 80 | + expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${options.outFileExtension}`); |
| 81 | + }); |
| 82 | +}); |
0 commit comments