diff --git a/package.json b/package.json index b5ef767753..d335745a67 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ ], "scripts": { "test": "jest --runInBand", - "test:watch": "npm test -- --watch", + "test:watch": "npm run test -- --watch", "publish": "lerna publish --conventional-commits", "lint": "eslint .", "coveralls": "jest --coverage && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", diff --git a/packages/serverless-nextjs-plugin/__tests__/index.test.js b/packages/serverless-nextjs-plugin/__tests__/index.test.js index 9fe0a7f29e..b3371fa32e 100644 --- a/packages/serverless-nextjs-plugin/__tests__/index.test.js +++ b/packages/serverless-nextjs-plugin/__tests__/index.test.js @@ -1,5 +1,6 @@ const ServerlessPluginBuilder = require("../utils/test/ServerlessPluginBuilder"); const displayServiceInfo = require("../lib/displayServiceInfo"); +const ServerlessNextJsPlugin = require("../index"); jest.mock("../lib/displayServiceInfo"); @@ -11,12 +12,6 @@ describe("ServerlessNextJsPlugin", () => { }); describe("#constructor", () => { - let plugin; - - beforeAll(() => { - plugin = new ServerlessPluginBuilder().build(); - }); - it.each` hook | method ${"before:offline:start"} | ${"build"} @@ -28,8 +23,12 @@ describe("ServerlessNextJsPlugin", () => { ${"after:package:createDeploymentArtifacts"} | ${"removePluginBuildDir"} ${"before:aws:package:finalize:mergeCustomProviderResources"} | ${"addCustomStackResources"} `("should hook to $hook with method $method", ({ hook, method }) => { - expect(plugin[method]).toBeDefined(); - expect(plugin.hooks[hook]).toEqual(plugin[method]); + const spy = jest + .spyOn(ServerlessNextJsPlugin.prototype, "hookWrapper") + .mockImplementation(() => {}); + const plugin = new ServerlessPluginBuilder().build(); + plugin.hooks[hook](); + expect(spy).toHaveBeenCalledWith(plugin[method]); }); }); diff --git a/packages/serverless-nextjs-plugin/index.js b/packages/serverless-nextjs-plugin/index.js index eb106b351f..25c7961bb6 100644 --- a/packages/serverless-nextjs-plugin/index.js +++ b/packages/serverless-nextjs-plugin/index.js @@ -14,31 +14,52 @@ class ServerlessNextJsPlugin { this.options = options; this.commands = {}; - this.provider = this.serverless.getProvider("aws"); - this.providerRequest = this.provider.request.bind(this.provider); - this.pluginBuildDir = new PluginBuildDir(this.nextConfigDir); - - this.addCustomStackResources = addCustomStackResources.bind(this); - this.uploadStaticAssets = uploadStaticAssets.bind(this); this.build = build.bind(this); + this.addCustomStackResources = addCustomStackResources.bind(this); this.checkForChanges = checkForChanges.bind(this); - - this.printStackOutput = this.printStackOutput.bind(this); - this.removePluginBuildDir = this.removePluginBuildDir.bind(this); + this.uploadStaticAssets = uploadStaticAssets.bind(this); this.hooks = { - "before:offline:start": this.build, - "before:package:initialize": this.build, - "before:deploy:function:initialize": this.build, - "before:aws:package:finalize:mergeCustomProviderResources": this - .addCustomStackResources, - "after:package:createDeploymentArtifacts": this.removePluginBuildDir, - "after:aws:deploy:deploy:checkForChanges": this.checkForChanges, - "after:aws:deploy:deploy:uploadArtifacts": this.uploadStaticAssets, - "after:aws:info:displayStackOutputs": this.printStackOutput + "before:offline:start": this.hookWrapper.bind(this, this.build), + "before:package:initialize": this.hookWrapper.bind(this, this.build), + "before:deploy:function:initialize": this.hookWrapper.bind( + this, + this.build + ), + "before:aws:package:finalize:mergeCustomProviderResources": this.hookWrapper.bind( + this, + this.addCustomStackResources + ), + "after:package:createDeploymentArtifacts": this.hookWrapper.bind( + this, + this.removePluginBuildDir + ), + "after:aws:deploy:deploy:checkForChanges": this.hookWrapper.bind( + this, + this.checkForChanges + ), + "after:aws:deploy:deploy:uploadArtifacts": this.hookWrapper.bind( + this, + this.uploadStaticAssets + ), + "after:aws:info:displayStackOutputs": this.hookWrapper.bind( + this, + this.printStackOutput + ) }; } + async hookWrapper(lifecycleFunc) { + this.initializeVariables(); + return await lifecycleFunc.call(this); + } + + initializeVariables() { + this.provider = this.serverless.getProvider("aws"); + this.providerRequest = this.provider.request.bind(this.provider); + this.pluginBuildDir = new PluginBuildDir(this.nextConfigDir); + } + get nextConfigDir() { return this.getPluginConfigValue("nextConfigDir"); } diff --git a/packages/serverless-nextjs-plugin/utils/test/ServerlessPluginBuilder.js b/packages/serverless-nextjs-plugin/utils/test/ServerlessPluginBuilder.js index 63fe7e3094..93f513ea67 100644 --- a/packages/serverless-nextjs-plugin/utils/test/ServerlessPluginBuilder.js +++ b/packages/serverless-nextjs-plugin/utils/test/ServerlessPluginBuilder.js @@ -65,7 +65,9 @@ class ServerlessPluginBuilder { } build() { - return new ServerlessNextJsPlugin(this.serverless, {}); + const plugin = new ServerlessNextJsPlugin(this.serverless, {}); + plugin.initializeVariables(); + return plugin; } }