Skip to content

Commit

Permalink
add test for minifying handler renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
BeeeQueue committed Jun 19, 2022
1 parent 47210c4 commit 65c2765
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,9 @@ var foo = (...rest) => {
console.log(foo(\\"test\\"));
"
`;
exports[`minification does not rename handler function 1`] = `
"
function handler(event){console.log(\\"test\\")}
"
`;
26 changes: 24 additions & 2 deletions src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import fs from "fs/promises"
import { tmpdir } from "os"
import path from "path"

import { build, BuildResult, OutputFile } from "esbuild"
import { build, BuildOptions, BuildResult, OutputFile } from "esbuild"
import { nanoid } from "nanoid"
import dedent from "ts-dedent"
import { beforeEach, describe, expect, test, TestContext } from "vitest"
Expand Down Expand Up @@ -35,7 +35,11 @@ beforeEach(async (ctx) => {
}
})

const buildFile = async (ctx: TestContext, contents: string) => {
const buildFile = async (
ctx: TestContext,
contents: string,
extraOptions?: BuildOptions,
) => {
const inputFilePath = path.join(ctx.tempDirPath, "index.ts")
await fs.writeFile(inputFilePath, dedent(contents))

Expand All @@ -45,6 +49,8 @@ const buildFile = async (ctx: TestContext, contents: string) => {

target: "es5",
format: "esm",

...extraOptions,
write: false,
})
}
Expand Down Expand Up @@ -334,3 +340,19 @@ test("does not modify crypto imports", async (ctx) => {
const output = getOutput(result)
expect(output).toMatchSnapshot()
})

test("minification does not rename handler function", async (ctx) => {
const input = dedent`
function handler(event: Record<string, unknkown>) {
console.log("test")
}
`

const result = await buildFile(ctx, input, { minify: true })

expect(result.outputFiles).toBeDefined()

const output = getOutput(result)
expect(output).toContain("handler(event)")
expect(output).toMatchSnapshot()
})

0 comments on commit 65c2765

Please sign in to comment.