Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit b35e5be

Browse files
fix: unit and integration tests pass in windows
1 parent a0f932c commit b35e5be

12 files changed

+111
-78
lines changed

__tests__/index.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const ServerlessPluginBuilder = require("../utils/test/ServerlessPluginBuilder");
23
const parsedNextConfigurationFactory = require("../utils/test/parsedNextConfigurationFactory");
34
const uploadStaticAssetsToS3 = require("../lib/uploadStaticAssetsToS3");
@@ -172,9 +173,10 @@ describe("ServerlessNextJsPlugin", () => {
172173
});
173174

174175
it("should call uploadStaticAssetsToS3 with bucketName and next static dir", () => {
176+
const distDir = 'build';
175177
parseNextConfiguration.mockReturnValueOnce(
176178
parsedNextConfigurationFactory({
177-
distDir: "build"
179+
distDir
178180
})
179181
);
180182

@@ -184,7 +186,7 @@ describe("ServerlessNextJsPlugin", () => {
184186

185187
return plugin.uploadStaticAssets().then(() => {
186188
expect(uploadStaticAssetsToS3).toBeCalledWith({
187-
staticAssetsPath: "build/static",
189+
staticAssetsPath: path.join(distDir, 'static'),
188190
bucketName: "my-bucket",
189191
providerRequest: expect.any(Function)
190192
});

classes/PluginBuildDir.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,19 @@ const path = require("path");
22
const fs = require("fs-extra");
33
const logger = require("../utils/logger");
44

5+
const BUILD_DIR_NAME = "sls-next-build";
6+
57
class PluginBuildDir {
68
constructor(nextConfigDir) {
79
this.nextConfigDir = nextConfigDir;
810
}
911

1012
get buildDir() {
11-
return path.join(this.nextConfigDir, "sls-next-build");
13+
return path.join(this.nextConfigDir, BUILD_DIR_NAME);
14+
}
15+
16+
get posixBuildDir() {
17+
return path.posix.join(this.nextConfigDir, BUILD_DIR_NAME);
1218
}
1319

1420
setupBuildDir() {

classes/__tests__/NextPage.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const NextPage = require("../NextPage");
23

34
describe("NextPage", () => {
@@ -12,7 +13,7 @@ describe("NextPage", () => {
1213

1314
describe("When is the index page", () => {
1415
const buildDir = "build";
15-
const pagePath = `${buildDir}/index.js`;
16+
const pagePath = path.join(buildDir, 'index.js');
1617
let page;
1718

1819
beforeEach(() => {
@@ -33,7 +34,7 @@ describe("NextPage", () => {
3334

3435
describe("When is the _error page", () => {
3536
const buildDir = "build";
36-
const pagePath = `${buildDir}/_error.js`;
37+
const pagePath = path.join(buildDir, '_error.js');
3738
let page;
3839

3940
beforeEach(() => {
@@ -58,7 +59,7 @@ describe("NextPage", () => {
5859

5960
describe("When is a nested page", () => {
6061
const buildDir = "build";
61-
const pagePath = `${buildDir}/categories/fridge/fridges.js`;
62+
const pagePath = path.join(buildDir, 'categories/fridge/fridges.js');
6263
let page;
6364

6465
beforeEach(() => {
@@ -103,11 +104,11 @@ describe("NextPage", () => {
103104
});
104105

105106
it("should have pageCompatPath", () => {
106-
expect(page.pageCompatPath).toEqual(`${buildDir}/admin.compat.js`);
107+
expect(page.pageCompatPath).toEqual(path.join(buildDir, "admin.compat.js"));
107108
});
108109

109110
it("should return pageOriginalPath", () => {
110-
expect(page.pageOriginalPath).toEqual(`${buildDir}/admin.original.js`);
111+
expect(page.pageOriginalPath).toEqual(path.join(buildDir, "admin.original.js"));
111112
});
112113

113114
it("should return pageDir", () => {

classes/__tests__/PluginBuildDir.test.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const path = require('path');
12
const fs = require("fs-extra");
23
const PluginBuildDir = require("../PluginBuildDir");
34
const logger = require("../../utils/logger");
@@ -17,14 +18,19 @@ describe("PluginBuildDir", () => {
1718

1819
describe("When a new instance is created", () => {
1920
let pluginBuildDir;
21+
let nextConfigDir;
2022

2123
beforeEach(() => {
22-
const nextConfigDir = "path/to/nextApp";
24+
nextConfigDir = "path/to/nextApp";
2325
pluginBuildDir = new PluginBuildDir(nextConfigDir);
2426
});
2527

26-
it("should have buildDir one level above nextBuild", () => {
27-
expect(pluginBuildDir.buildDir).toEqual("path/to/nextApp/sls-next-build");
28+
it("should have buildDir at same level as next config.", () => {
29+
expect(pluginBuildDir.buildDir).toEqual(path.join(nextConfigDir, 'sls-next-build'));
30+
});
31+
32+
it("should have posixBuildDir regardless the platform", () => {
33+
expect(pluginBuildDir.posixBuildDir).toEqual("path/to/nextApp/sls-next-build");
2834
});
2935
});
3036

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ServerlessNextJsPlugin {
5353
const servicePackage = this.serverless.service.package;
5454

5555
servicePackage.include = servicePackage.include || [];
56-
servicePackage.include.push(path.join(pluginBuildDir.buildDir, "**"));
56+
servicePackage.include.push(path.posix.join(pluginBuildDir.posixBuildDir, "**"));
5757
return build(pluginBuildDir, this.getPluginConfigValue("pageConfig")).then(
5858
nextPages => this.setNextPages(nextPages)
5959
);

lib/__tests__/copyBuildFiles.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ describe("copyBuildFiles", () => {
1313

1414
describe("when page files are copied correctly", () => {
1515
let pluginBuildDirObj;
16-
const pluginBuildDir = "path/to/sls-next-build";
17-
const nextBuildDir = "path/to/.next";
16+
const pluginBuildDir = path.normalize("path/to/sls-next-build");
17+
const nextBuildDir = path.normalize("path/to/.next");
1818

1919
beforeEach(() => {
2020
fse.copy.mockResolvedValue(null);
@@ -39,7 +39,7 @@ describe("copyBuildFiles", () => {
3939

4040
it("should copy serverless pages folder from next build directory", () => {
4141
expect(fse.copy).toBeCalledWith(
42-
`${nextBuildDir}/serverless/pages`,
42+
path.join(nextBuildDir, 'serverless/pages'),
4343
pluginBuildDir
4444
);
4545
});
@@ -51,7 +51,7 @@ describe("copyBuildFiles", () => {
5151
it("should call fs copy with the compatLayer file", () => {
5252
expect(fse.copy).toBeCalledWith(
5353
path.join(__dirname, "..", `compatLayer.js`),
54-
`${pluginBuildDir}/compatLayer.js`
54+
path.join(pluginBuildDir, 'compatLayer.js')
5555
);
5656
});
5757
});

lib/__tests__/getCompatLayerCode.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,31 @@
1+
const path = require('path');
12
const getCompatLayerCode = require("../getCompatLayerCode");
23

34
describe("getCompatLayerCode", () => {
45
it("should require compatLayer", () => {
56
const compatHandlerContent = getCompatLayerCode(
6-
"sls-next-build/my-page.js"
7+
path.join("sls-next-build", "my-page.js")
78
);
89
expect(compatHandlerContent).toContain('require("./compatLayer")');
910
});
1011

1112
it("should require compatLayer with correct path when page is nested", () => {
1213
const compatHandlerContent = getCompatLayerCode(
13-
"sls-next-build/categories/fridge/fridges.js"
14+
path.join("sls-next-build", "categories/fridge/fridges.js")
1415
);
1516
expect(compatHandlerContent).toContain('require("../../compatLayer")');
1617
});
1718

1819
it("should require next page provided", () => {
1920
const compatHandlerContent = getCompatLayerCode(
20-
"sls-next-build/my-page.js"
21+
path.join("sls-next-build", "my-page.js")
2122
);
2223
expect(compatHandlerContent).toContain('require("./my-page.original.js")');
2324
});
2425

2526
it("should export render method", () => {
2627
const compatHandlerContent = getCompatLayerCode(
27-
"sls-next-build/my-page.js"
28+
path.join("sls-next-build", "my-page.js")
2829
);
2930
expect(compatHandlerContent).toContain("module.exports.render");
3031
});

lib/__tests__/getNextPagesFromBuildDir.test.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ describe("getNextPagesFromBuildDir", () => {
2626
it("should return an empty array when there are no pages", () => {
2727
expect.assertions(1);
2828

29-
const getPagesPromise = getNextPagesFromBuildDir("/path/to/build/dir").then(
29+
const buildDir = path.normalize("/path/to/build/dir")
30+
31+
const getPagesPromise = getNextPagesFromBuildDir(buildDir).then(
3032
nextPages => {
3133
expect(nextPages).toEqual([]);
3234
}
@@ -46,9 +48,9 @@ describe("getNextPagesFromBuildDir", () => {
4648
const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
4749
expect(nextPages).toHaveLength(2);
4850
expect(nextPages[0].pageName).toEqual("index");
49-
expect(nextPages[0].pagePath).toEqual("build/index.js");
51+
expect(nextPages[0].pagePath).toEqual(path.join('build', 'index.js'));
5052
expect(nextPages[1].pageName).toEqual("about");
51-
expect(nextPages[1].pagePath).toEqual("build/about.js");
53+
expect(nextPages[1].pagePath).toEqual(path.join('build', 'about.js'));
5254
});
5355

5456
mockedStream.emit("data", {
@@ -73,7 +75,9 @@ describe("getNextPagesFromBuildDir", () => {
7375
about: aboutPageConfigOverride
7476
};
7577

76-
const promise = getNextPagesFromBuildDir("/path/to/build", pageConfig).then(
78+
const buildDir = path.normalize('/path/to/build');
79+
80+
const promise = getNextPagesFromBuildDir(buildDir, pageConfig).then(
7781
nextPages => {
7882
expect(nextPages[0].serverlessFunctionOverrides).toEqual(
7983
indexPageConfigOverride
@@ -84,8 +88,8 @@ describe("getNextPagesFromBuildDir", () => {
8488
}
8589
);
8690

87-
mockedStream.emit("data", { path: `/path/to/build/index.js` });
88-
mockedStream.emit("data", { path: `/path/to/build/about.js` });
91+
mockedStream.emit("data", { path: path.join(buildDir, "index.js") });
92+
mockedStream.emit("data", { path: path.join(buildDir, "about.js") });
8993
mockedStream.emit("end");
9094

9195
return promise;
@@ -94,11 +98,13 @@ describe("getNextPagesFromBuildDir", () => {
9498
it("should log pages found", () => {
9599
expect.assertions(1);
96100

97-
const promise = getNextPagesFromBuildDir("/path/to/build").then(() => {
101+
const buildDir = path.normalize('/path/to/build');
102+
103+
const promise = getNextPagesFromBuildDir(buildDir).then(() => {
98104
expect(logger.log).toBeCalledWith(`Found 1 next page(s)`);
99105
});
100106

101-
mockedStream.emit("data", { path: `/path/to/build/about.js` });
107+
mockedStream.emit("data", { path: path.join(buildDir, "about.js") });
102108
mockedStream.emit("end");
103109

104110
return promise;
@@ -107,7 +113,7 @@ describe("getNextPagesFromBuildDir", () => {
107113
it("should skip _app and _document pages", () => {
108114
expect.assertions(2);
109115

110-
const buildDir = "./build";
116+
const buildDir = path.normalize("./build");
111117
const resolvedBuildDir = path.resolve(buildDir);
112118

113119
const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
@@ -130,15 +136,17 @@ describe("getNextPagesFromBuildDir", () => {
130136
it("should skip compatLayer file", () => {
131137
expect.assertions(2);
132138

133-
const promise = getNextPagesFromBuildDir("/path/to/build").then(
139+
const buildDir = path.normalize('/path/to/build');
140+
141+
const promise = getNextPagesFromBuildDir(buildDir).then(
134142
nextPages => {
135143
expect(nextPages).toHaveLength(1);
136144
expect(nextPages[0].pageName).toEqual("home");
137145
}
138146
);
139147

140-
mockedStream.emit("data", { path: `/path/to/build/compatLayer.js` });
141-
mockedStream.emit("data", { path: `/path/to/build/home.js` });
148+
mockedStream.emit("data", { path: path.join(buildDir, "compatLayer.js") });
149+
mockedStream.emit("data", { path: path.join(buildDir, "home.js") });
142150
mockedStream.emit("end");
143151

144152
return promise;
@@ -147,22 +155,22 @@ describe("getNextPagesFromBuildDir", () => {
147155
it("should handle nested pages", () => {
148156
expect.assertions(5);
149157

150-
const buildDir = "./build";
158+
const buildDir = path.normalize("./build");
151159
const resolvedBuildDir = path.resolve(buildDir);
152160

153161
const promise = getNextPagesFromBuildDir(buildDir).then(nextPages => {
154162
expect(nextPages).toHaveLength(2);
155163
expect(nextPages[0].pageName).toEqual("hello-world");
156-
expect(nextPages[0].pagePath).toEqual("build/one/hello-world.js");
164+
expect(nextPages[0].pagePath).toEqual(path.join(buildDir, "one", "hello-world.js"));
157165
expect(nextPages[1].pageName).toEqual("hello-world");
158-
expect(nextPages[1].pagePath).toEqual("build/one/two/hello-world.js");
166+
expect(nextPages[1].pagePath).toEqual(path.join(buildDir, "one", "two", "hello-world.js"));
159167
});
160168

161169
mockedStream.emit("data", {
162-
path: path.join(resolvedBuildDir, "one/hello-world.js")
170+
path: path.join(resolvedBuildDir, "one", "hello-world.js")
163171
});
164172
mockedStream.emit("data", {
165-
path: path.join(resolvedBuildDir, "one/two/hello-world.js")
173+
path: path.join(resolvedBuildDir, "one", "two", "hello-world.js")
166174
});
167175
mockedStream.emit("end");
168176

@@ -172,7 +180,7 @@ describe("getNextPagesFromBuildDir", () => {
172180
it("should skip page directories", () => {
173181
expect.assertions(1);
174182

175-
const buildDir = "./build";
183+
const buildDir = path.normalize("./build");
176184
const resolvedBuildDir = path.resolve(buildDir);
177185
fs.lstatSync.mockReturnValue({ isDirectory: () => true });
178186

@@ -181,10 +189,10 @@ describe("getNextPagesFromBuildDir", () => {
181189
});
182190

183191
mockedStream.emit("data", {
184-
path: path.join(resolvedBuildDir, "one/")
192+
path: path.join(resolvedBuildDir, "one")
185193
});
186194
mockedStream.emit("data", {
187-
path: path.join(resolvedBuildDir, "one/two/")
195+
path: path.join(resolvedBuildDir, "one", "two")
188196
});
189197
mockedStream.emit("end");
190198

0 commit comments

Comments
 (0)