|
| 1 | +import fs from "fs-extra" |
| 2 | +import { prepareFunction } from "../lambda-handler" |
| 3 | +import { join, relative } from "path" |
| 4 | +import { slash } from "gatsby-core-utils/path" |
| 5 | + |
| 6 | +const writeFileSpy = jest |
| 7 | + .spyOn(fs, `writeFile`) |
| 8 | + .mockImplementation(async () => {}) |
| 9 | +const writeJsonSpy = jest |
| 10 | + .spyOn(fs, `writeJSON`) |
| 11 | + .mockImplementation(async () => {}) |
| 12 | + |
| 13 | +const fixturePath = join( |
| 14 | + relative(process.cwd(), __dirname), |
| 15 | + `fixtures`, |
| 16 | + `lambda-handler` |
| 17 | +) |
| 18 | +const pathToEntryPoint = join(fixturePath, `entry.js`) |
| 19 | +const requiredFile = join(fixturePath, `included.js`) |
| 20 | + |
| 21 | +test(`produced handler is correct`, async () => { |
| 22 | + await prepareFunction({ |
| 23 | + functionId: `test`, |
| 24 | + name: `test`, |
| 25 | + pathToEntryPoint, |
| 26 | + requiredFiles: [requiredFile], |
| 27 | + }) |
| 28 | + const handlerCode = writeFileSpy.mock.calls[0][1] |
| 29 | + // expect require in produced code (this is to mostly to make sure handlerCode is actual handler code) |
| 30 | + expect(handlerCode).toMatch(/require\(["'][^"']*["']\)/) |
| 31 | + // require paths should not have backward slashes (win paths) |
| 32 | + expect(handlerCode).not.toMatch(/require\(["'][^"']*\\[^"']*["']\)/) |
| 33 | + |
| 34 | + expect(writeJsonSpy).toBeCalledWith( |
| 35 | + expect.any(String), |
| 36 | + expect.objectContaining({ |
| 37 | + config: expect.objectContaining({ |
| 38 | + name: `test`, |
| 39 | + generator: expect.stringContaining(`gatsby-adapter-netlify`), |
| 40 | + includedFiles: [slash(requiredFile)], |
| 41 | + externalNodeModules: [`msgpackr-extract`], |
| 42 | + }), |
| 43 | + version: 1, |
| 44 | + }) |
| 45 | + ) |
| 46 | +}) |
0 commit comments