Skip to content

Commit f828165

Browse files
committed
add env var test & move test
1 parent c50c133 commit f828165

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js";
2+
import { envVarRuleCreator } from "@opennextjs/aws/build/patch/patches/patchEnvVar.js";
3+
import { describe, it } from "vitest";
4+
5+
const moduleCompiledCode = `
6+
"use strict";
7+
if (process.env.NEXT_RUNTIME === 'edge') {
8+
module.exports = require('next/dist/server/route-modules/app-page/module.js');
9+
} else {
10+
if (process.env.__NEXT_EXPERIMENTAL_REACT) {
11+
if (process.env.NODE_ENV === 'development') {
12+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js');
13+
} else if (process.env.TURBOPACK) {
14+
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js');
15+
} else {
16+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js');
17+
}
18+
} else {
19+
if (process.env.NODE_ENV === 'development') {
20+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js');
21+
} else if (process.env.TURBOPACK) {
22+
module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js');
23+
} else {
24+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js');
25+
}
26+
}
27+
}
28+
`;
29+
30+
const reactJSXRuntimeCode = `
31+
'use strict';
32+
33+
if (process.env.NODE_ENV === 'production') {
34+
module.exports = require('./cjs/react-jsx-runtime.production.js');
35+
} else {
36+
module.exports = require('./cjs/react-jsx-runtime.development.js');
37+
}
38+
`;
39+
40+
describe("patch NODE_ENV", () => {
41+
it("should patch NODE_ENV in module.compiled", () => {
42+
expect(
43+
patchCode(
44+
moduleCompiledCode,
45+
envVarRuleCreator("NODE_ENV", '"production"'),
46+
),
47+
).toMatchInlineSnapshot(`
48+
""use strict";
49+
if (process.env.NEXT_RUNTIME === 'edge') {
50+
module.exports = require('next/dist/server/route-modules/app-page/module.js');
51+
} else {
52+
if (process.env.__NEXT_EXPERIMENTAL_REACT) {
53+
if ("production" === 'development') {
54+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js');
55+
} else if (process.env.TURBOPACK) {
56+
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js');
57+
} else {
58+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js');
59+
}
60+
} else {
61+
if ("production" === 'development') {
62+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js');
63+
} else if (process.env.TURBOPACK) {
64+
module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js');
65+
} else {
66+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js');
67+
}
68+
}
69+
}
70+
"`);
71+
});
72+
73+
it("should patch NODE_ENV in react/jsx-runtime", () => {
74+
expect(
75+
patchCode(
76+
reactJSXRuntimeCode,
77+
envVarRuleCreator("NODE_ENV", '"production"'),
78+
),
79+
).toMatchInlineSnapshot(`
80+
"'use strict';
81+
82+
if ("production" === 'production') {
83+
module.exports = require('./cjs/react-jsx-runtime.production.js');
84+
} else {
85+
module.exports = require('./cjs/react-jsx-runtime.development.js');
86+
}
87+
"`);
88+
});
89+
});
90+
91+
describe("patch NEXT_RUNTIME", () => {
92+
it("should patch NEXT_RUNTIME in module.compiled", () => {
93+
expect(
94+
patchCode(
95+
moduleCompiledCode,
96+
envVarRuleCreator("NEXT_RUNTIME", '"node"'),
97+
),
98+
).toMatchInlineSnapshot(`
99+
""use strict";
100+
if ("node" === 'edge') {
101+
module.exports = require('next/dist/server/route-modules/app-page/module.js');
102+
} else {
103+
if (process.env.__NEXT_EXPERIMENTAL_REACT) {
104+
if (process.env.NODE_ENV === 'development') {
105+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js');
106+
} else if (process.env.TURBOPACK) {
107+
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js');
108+
} else {
109+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js');
110+
}
111+
} else {
112+
if (process.env.NODE_ENV === 'development') {
113+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js');
114+
} else if (process.env.TURBOPACK) {
115+
module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js');
116+
} else {
117+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js');
118+
}
119+
}
120+
}
121+
"`);
122+
});
123+
});
124+
125+
describe("patch TURBOPACK", () => {
126+
it("should patch TURBOPACK in module.compiled", () => {
127+
expect(
128+
patchCode(moduleCompiledCode, envVarRuleCreator("TURBOPACK", "false")),
129+
).toMatchInlineSnapshot(`
130+
""use strict";
131+
if (process.env.NEXT_RUNTIME === 'edge') {
132+
module.exports = require('next/dist/server/route-modules/app-page/module.js');
133+
} else {
134+
if (process.env.__NEXT_EXPERIMENTAL_REACT) {
135+
if (process.env.NODE_ENV === 'development') {
136+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.dev.js');
137+
} else if (false) {
138+
module.exports = require('next/dist/compiled/next-server/app-page-turbo-experimental.runtime.prod.js');
139+
} else {
140+
module.exports = require('next/dist/compiled/next-server/app-page-experimental.runtime.prod.js');
141+
}
142+
} else {
143+
if (process.env.NODE_ENV === 'development') {
144+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.dev.js');
145+
} else if (false) {
146+
module.exports = require('next/dist/compiled/next-server/app-page-turbo.runtime.prod.js');
147+
} else {
148+
module.exports = require('next/dist/compiled/next-server/app-page.runtime.prod.js');
149+
}
150+
}
151+
}
152+
"`);
153+
});
154+
});

packages/tests-unit/tests/build/patch/patchFetchCacheISR.test.ts renamed to packages/tests-unit/tests/build/patch/patches/patchFetchCacheISR.test.ts

File renamed without changes.

packages/tests-unit/tests/build/patch/patchFetchCacheWaitUntil.test.ts renamed to packages/tests-unit/tests/build/patch/patches/patchFetchCacheWaitUntil.test.ts

File renamed without changes.

packages/tests-unit/tests/build/patch/patchNextServer.test.ts renamed to packages/tests-unit/tests/build/patch/patches/patchNextServer.test.ts

File renamed without changes.

0 commit comments

Comments
 (0)