|
| 1 | +const fs = require("fs"); |
| 2 | +const path = require("path"); |
| 3 | +const rimraf = require("rimraf"); |
| 4 | +const build = require("./build"); |
| 5 | + |
| 6 | +jest.mock("./config", () => { |
| 7 | + const path = require("path"); |
| 8 | + return { |
| 9 | + load: jest.fn(() => ({ |
| 10 | + build: { functions: path.join(".temp", "build", "lambda") }, |
| 11 | + })), |
| 12 | + loadContext: jest.fn(() => ({ environment: {} })), |
| 13 | + }; |
| 14 | +}); |
| 15 | + |
| 16 | +const buildTemp = path.join(".temp", "build"); |
| 17 | +const functions = path.join(buildTemp, "functions"); |
| 18 | + |
| 19 | +const setupFunction = (script, filename) => { |
| 20 | + fs.mkdirSync(functions, { recursive: true }); |
| 21 | + fs.writeFileSync(path.join(functions, filename), script); |
| 22 | +}; |
| 23 | + |
| 24 | +describe("build", () => { |
| 25 | + const functionsBuildOutputDir = require("./config").load().build.functions; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + fs.mkdirSync(buildTemp, { recursive: true }); |
| 29 | + }); |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + rimraf.sync(buildTemp); |
| 33 | + }); |
| 34 | + |
| 35 | + describe("run", () => { |
| 36 | + it("should return webpack stats on successful build", async () => { |
| 37 | + const script = `module.exports = () => console.log("hello world")`; |
| 38 | + setupFunction(script, "index.js"); |
| 39 | + |
| 40 | + const stats = await build.run(functions); |
| 41 | + expect(stats.compilation.errors).toHaveLength(0); |
| 42 | + expect( |
| 43 | + fs.existsSync(path.join(functionsBuildOutputDir, "index.js")) |
| 44 | + ).toEqual(true); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should throw error on complication errors", async () => { |
| 48 | + const script = `module.exports = () => console.log("hello`; |
| 49 | + setupFunction(script, "index.js"); |
| 50 | + |
| 51 | + expect.assertions(1); |
| 52 | + |
| 53 | + await expect(build.run(functions)).rejects.toHaveLength(1); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should throw error on invalid config", async () => { |
| 57 | + const script = `module.exports = () => console.log("hello world")`; |
| 58 | + setupFunction(script, "index.js"); |
| 59 | + |
| 60 | + expect.assertions(1); |
| 61 | + |
| 62 | + await expect( |
| 63 | + build.run(functions, { |
| 64 | + userWebpackConfig: "non-existing-webpack-config.js", |
| 65 | + }) |
| 66 | + ).rejects.toThrow("Cannot find module"); |
| 67 | + }); |
| 68 | + }); |
| 69 | +}); |
0 commit comments