|
1 | 1 | import { patchCode } from "@opennextjs/aws/build/patch/astCodePatcher.js"; |
2 | 2 | import { describe, expect, test } from "vitest"; |
3 | 3 |
|
4 | | -import { getNext14Rule, getNext15Rule } from "./instrumentation.js"; |
| 4 | +import { getNext14Rule, getNext15Rule, getNext154Rule } from "./instrumentation.js"; |
5 | 5 |
|
6 | 6 | describe("LoadInstrumentationModule (Next15)", () => { |
7 | 7 | const code = ` |
@@ -96,3 +96,59 @@ describe("prepareImpl (Next14)", () => { |
96 | 96 | `); |
97 | 97 | }); |
98 | 98 | }); |
| 99 | + |
| 100 | +describe("getInstrumenationModule (Next154)", () => { |
| 101 | + const code = ` |
| 102 | + async function getInstrumentationModule(projectDir, distDir) { |
| 103 | + if (cachedInstrumentationModule) { |
| 104 | + return cachedInstrumentationModule; |
| 105 | + } |
| 106 | + try { |
| 107 | + cachedInstrumentationModule = (0, _interopdefault.interopDefault)(await require(_nodepath.default.join(projectDir, distDir, "server", \`\${_constants.INSTRUMENTATION_HOOK_FILENAME}.js\`\))); |
| 108 | + return cachedInstrumentationModule; |
| 109 | + } catch (err) { |
| 110 | + if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") { |
| 111 | + throw err; |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + `; |
| 116 | + |
| 117 | + test("patch when an instrumentation file is not present", async () => { |
| 118 | + expect(patchCode(code, getNext154Rule(null))).toMatchInlineSnapshot(` |
| 119 | + "async function getInstrumentationModule(projectDir, distDir) { |
| 120 | + if (cachedInstrumentationModule) { |
| 121 | + return cachedInstrumentationModule; |
| 122 | + } |
| 123 | + try { |
| 124 | + cachedInstrumentationModule = null; |
| 125 | + return cachedInstrumentationModule; |
| 126 | + } catch (err) { |
| 127 | + if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") { |
| 128 | + throw err; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + " |
| 133 | + `); |
| 134 | + }); |
| 135 | + |
| 136 | + test("patch when an instrumentation file is present", async () => { |
| 137 | + expect(patchCode(code, getNext154Rule("/_file_exists_/instrumentation.js"))).toMatchInlineSnapshot(` |
| 138 | + "async function getInstrumentationModule(projectDir, distDir) { |
| 139 | + if (cachedInstrumentationModule) { |
| 140 | + return cachedInstrumentationModule; |
| 141 | + } |
| 142 | + try { |
| 143 | + cachedInstrumentationModule = require('/_file_exists_/instrumentation.js'); |
| 144 | + return cachedInstrumentationModule; |
| 145 | + } catch (err) { |
| 146 | + if ((0, _iserror.default)(err) && err.code !== "ENOENT" && err.code !== "MODULE_NOT_FOUND" && err.code !== "ERR_MODULE_NOT_FOUND") { |
| 147 | + throw err; |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + " |
| 152 | + `); |
| 153 | + }); |
| 154 | +}); |
0 commit comments