{"version":3,"file":"index.test.mjs","names":["SwcServerlessPlugin"],"sources":["../../../src/tests/index.test.ts"],"sourcesContent":["import fs from 'fs-extra';\nimport type Serverless from 'serverless';\nimport type Service from 'serverless/classes/Service';\n\nimport SwcServerlessPlugin from '../index';\nimport type { ImprovedServerlessOptions } from '../types';\n\njest.mock('fs-extra');\n\nconst mockProvider: Service['provider'] = {\n  name: 'aws',\n  region: 'us-east-1',\n  stage: 'dev',\n  runtime: 'nodejs18.x',\n  compiledCloudFormationTemplate: {\n    Resources: {},\n  },\n  versionFunctions: true,\n};\n\nconst mockGetFunction = jest.fn();\n\nconst packageIndividuallyService: Partial<Service> = {\n  functions: {\n    hello1: { handler: 'hello1.handler', events: [], package: { artifact: 'hello1' } },\n    hello2: { handler: 'hello2.handler', events: [], package: { artifact: 'hello2' } },\n  },\n  package: { individually: true },\n  provider: mockProvider,\n  getFunction: mockGetFunction,\n};\n\nconst packageService: Partial<Service> = {\n  functions: {\n    hello1: { handler: 'hello1.handler', events: [] },\n    hello2: { handler: 'hello2.handler', events: [] },\n  },\n  package: { artifact: 'hello' },\n  provider: mockProvider,\n  getFunction: mockGetFunction,\n};\n\nconst patternsService: Partial<Service> = {\n  functions: {\n    hello1: { handler: 'hello1.handler', events: [] },\n    hello2: { handler: 'hello2.handler', events: [], package: {} },\n    hello3: { handler: 'hello3.handler', events: [], package: { patterns: ['excluded-by-default.json'] } },\n  },\n  package: { patterns: ['!excluded-by-default.json'] },\n  provider: mockProvider,\n  getFunction: mockGetFunction,\n};\n\nconst mockServerlessConfig = (serviceOverride?: Partial<Service>): Serverless => {\n  const service = {\n    ...packageIndividuallyService,\n    ...serviceOverride,\n  } as Service;\n\n  const mockCli = {\n    log: jest.fn(),\n  };\n\n  return {\n    service,\n    config: {\n      servicePath: '/workDir',\n      serviceDir: '/workDir',\n    },\n    configSchemaHandler: {\n      defineCustomProperties: jest.fn(),\n      defineFunctionEvent: jest.fn(),\n      defineFunctionEventProperties: jest.fn(),\n      defineFunctionProperties: jest.fn(),\n      defineProvider: jest.fn(),\n      defineTopLevelProperty: jest.fn(),\n    },\n    cli: mockCli,\n  } as Partial<Serverless> as Serverless;\n};\n\nconst mockOptions: ImprovedServerlessOptions = {\n  region: 'us-east-1',\n  stage: 'dev',\n};\n\nafterEach(() => {\n  jest.resetAllMocks();\n});\n\ndescribe('Move Artifacts', () => {\n  it('should copy files from the swc folder to the serverless folder', async () => {\n    const plugin = new SwcServerlessPlugin(mockServerlessConfig(), mockOptions);\n\n    plugin.hooks.initialize?.();\n\n    await plugin.moveArtifacts();\n\n    expect(fs.copy).toHaveBeenCalledWith('/workDir/.swc/.serverless', '/workDir/.serverless');\n  });\n\n  describe('function option', () => {\n    it('should update the selected functions base path to the serverless folder', async () => {\n      mockGetFunction.mockReturnValue(packageIndividuallyService.functions?.hello1);\n      const plugin = new SwcServerlessPlugin(mockServerlessConfig(), {\n        ...mockOptions,\n        function: 'hello1',\n      });\n\n      plugin.hooks.initialize?.();\n\n      await plugin.moveArtifacts();\n\n      expect(plugin.functions).toMatchInlineSnapshot(`\n        {\n          \"hello1\": {\n            \"events\": [],\n            \"handler\": \"hello1.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello1\",\n            },\n          },\n        }\n      `);\n    });\n  });\n\n  describe('package individually', () => {\n    it('should update function package artifacts base path to the serverless folder', async () => {\n      const plugin = new SwcServerlessPlugin(mockServerlessConfig(), mockOptions);\n\n      plugin.hooks.initialize?.();\n\n      await plugin.moveArtifacts();\n\n      expect(plugin.functions).toMatchInlineSnapshot(`\n        {\n          \"hello1\": {\n            \"events\": [],\n            \"handler\": \"hello1.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello1\",\n            },\n          },\n          \"hello2\": {\n            \"events\": [],\n            \"handler\": \"hello2.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello2\",\n            },\n          },\n        }\n      `);\n    });\n\n    it('should only update the base path of node functions', async () => {\n      const plugin = new SwcServerlessPlugin(\n        mockServerlessConfig({\n          functions: {\n            ...packageIndividuallyService.functions,\n            hello3: { handler: 'hello3.handler', events: [], runtime: 'python2.7' },\n          },\n        }),\n        mockOptions\n      );\n\n      plugin.hooks.initialize?.();\n\n      await plugin.moveArtifacts();\n\n      expect(plugin.functions).toMatchInlineSnapshot(`\n        {\n          \"hello1\": {\n            \"events\": [],\n            \"handler\": \"hello1.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello1\",\n            },\n          },\n          \"hello2\": {\n            \"events\": [],\n            \"handler\": \"hello2.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello2\",\n            },\n          },\n        }\n      `);\n    });\n\n    it('should skip function if skipSwc is set to true', async () => {\n      jest.mocked(fs.existsSync).mockReturnValue(true);\n\n      const hello3 = { handler: 'hello3.handler', events: [], skipSwc: true };\n      const plugin = new SwcServerlessPlugin(\n        mockServerlessConfig({\n          functions: {\n            ...packageIndividuallyService.functions,\n            hello3,\n          },\n        }),\n        mockOptions\n      );\n      plugin.hooks.initialize?.();\n\n      await plugin.moveArtifacts();\n\n      expect(plugin.functions).toMatchInlineSnapshot(`\n        {\n          \"hello1\": {\n            \"events\": [],\n            \"handler\": \"hello1.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello1\",\n            },\n          },\n          \"hello2\": {\n            \"events\": [],\n            \"handler\": \"hello2.handler\",\n            \"package\": {\n              \"artifact\": \".serverless/hello2\",\n            },\n          },\n          \"hello3\": {\n            \"events\": [],\n            \"handler\": \"hello3.handler\",\n            \"skipSwc\": true,\n          },\n        }\n      `);\n\n      expect(plugin.functionEntries).toEqual(\n        expect.arrayContaining([\n          {\n            entry: expect.stringContaining('/hello1.ts'),\n            func: {\n              events: [],\n              handler: 'hello1.handler',\n              package: {\n                artifact: '.serverless/hello1',\n              },\n            },\n            functionAlias: 'hello1',\n          },\n          {\n            entry: expect.stringContaining('/hello2.ts'),\n            func: {\n              events: [],\n              handler: 'hello2.handler',\n              package: {\n                artifact: '.serverless/hello2',\n              },\n            },\n            functionAlias: 'hello2',\n          },\n        ])\n      );\n      expect(plugin.functionEntries).not.toContain(\n        expect.objectContaining({\n          functionAlias: 'hello3',\n        })\n      );\n    });\n  });\n\n  describe('service package', () => {\n    it('should update the service package artifact base path to the serverless folder', async () => {\n      const plugin = new SwcServerlessPlugin(mockServerlessConfig(packageService), mockOptions);\n\n      plugin.hooks.initialize?.();\n\n      await plugin.moveArtifacts();\n\n      expect(plugin.serverless.service.package.artifact).toBe('.serverless/hello');\n    });\n  });\n});\n\ndescribe('Prepare', () => {\n  describe('function package', () => {\n    it('should set package patterns on functions only if supplied', () => {\n      const plugin = new SwcServerlessPlugin(mockServerlessConfig(patternsService), mockOptions);\n\n      plugin.hooks.initialize?.();\n\n      plugin.prepare();\n\n      expect(plugin.functions).toMatchInlineSnapshot(`\n        {\n          \"hello1\": {\n            \"events\": [],\n            \"handler\": \"hello1.handler\",\n            \"package\": {},\n          },\n          \"hello2\": {\n            \"events\": [],\n            \"handler\": \"hello2.handler\",\n            \"package\": {},\n          },\n          \"hello3\": {\n            \"events\": [],\n            \"handler\": \"hello3.handler\",\n            \"package\": {\n              \"patterns\": [\n                \"excluded-by-default.json\",\n              ],\n            },\n          },\n        }\n      `);\n    });\n\n    it('should copy the previous build resources if skipBuild is true', async () => {\n      const skipBuildServerlessConfig = {\n        ...patternsService,\n        custom: {\n          swc: {\n            skipBuild: true,\n          },\n        },\n      };\n      const plugin = new SwcServerlessPlugin(mockServerlessConfig(skipBuildServerlessConfig), mockOptions);\n      const copyPreBuiltResourcesSpy = jest.spyOn(plugin, 'copyPreBuiltResources');\n      const prepareSpy = jest.spyOn(plugin, 'prepare');\n\n      plugin.hooks.initialize?.();\n      expect(copyPreBuiltResourcesSpy).toHaveBeenCalled();\n      expect(prepareSpy).toHaveBeenCalled();\n    });\n  });\n});\n"],"mappings":";;;;;;AAOA,KAAK,KAAK,WAAW;AAErB,MAAM,eAAoC;CACxC,MAAM;CACN,QAAQ;CACR,OAAO;CACP,SAAS;CACT,gCAAgC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