Skip to content

Commit

Permalink
chore: add tests for the handleCompile
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamote committed Jan 28, 2024
1 parent 09661e6 commit 0a147a8
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions src/swc/__tests__/dirWorker.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { Options } from "@swc/core";
import handleCompile from "../dirWorker";
import { CliOptions, DEFAULT_OUT_FILE_EXTENSION } from "../options";
import * as utilModule from '../util';

type HandleCompileOptions = {
cliOptions: CliOptions;
swcOptions: Options;
sync: false,
outDir: "outDir",
filename: string,
outFileExtension?: string;
}

const createHandleCompileOptions = (options?: Partial<HandleCompileOptions>): HandleCompileOptions => ({
cliOptions: {
outDir: "",
outFile: "",
filename: "",
stripLeadingPaths: false,
filenames: [],
sync: false,
workers: undefined,
sourceMapTarget: undefined,
extensions: [],
watch: false,
copyFiles: false,
outFileExtension: "",
includeDotfiles: false,
deleteDirOnStart: false,
quiet: true,
only: [],
ignore: [],
},
swcOptions: {},
sync: false,
outDir: "outDir",
filename: "",
...options,
});

jest.mock('../util', () => ({
...jest.requireActual("../util"),
compile: jest.fn(),
}));

describe("dirWorker", () => {
it('should call "compile" with the "DEFAULT_OUT_FILE_EXTENSION" when "outFileExtension" is undefined', async () => {
const filename = 'test';
const options = createHandleCompileOptions({
filename: `${filename}.ts`
});

try {
await handleCompile(options);
} catch (err) {
// We don't care about the error in this test, we want to make sure that "compile" was called
}

// Assert that subFunction was called with the correct parameter
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${DEFAULT_OUT_FILE_EXTENSION}`);
});
});

describe("dirWorker", () => {
it('should call "compile" with "outFileExtension" when undefined', async () => {
const filename = 'test';
const options = createHandleCompileOptions({
filename: `${filename}.ts`,
outFileExtension: 'cjs'
});

try {
await handleCompile(options);
} catch (err) {
// We don't care about the error in this test, we want to make sure that "compile" was called
}

// Assert that subFunction was called with the correct parameter
expect(utilModule.compile).toHaveBeenCalledWith(options.filename, { sourceFileName: `../${options.filename}`}, options.sync, `${options.outDir}/${filename}.${options.outFileExtension}`);
});
});

0 comments on commit 0a147a8

Please sign in to comment.