-
Notifications
You must be signed in to change notification settings - Fork 544
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): avoid serialization warning when primitives have prototy…
…pe changed (#2902)
- Loading branch information
Showing
2 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { describe, expect, it, vi } from "vitest"; | ||
import { normalizeRuntimeConfig } from "../../src/core/config/resolvers/runtime-config"; | ||
import type { NitroConfig } from "nitropack/types"; | ||
|
||
const defaultRuntimeConfig = { | ||
textProperty: "value", | ||
numberProperty: 42, | ||
booleanProperty: true, | ||
arrayProperty: ["A", "B", "C"], | ||
objectProperty: { | ||
innerProperty: "value", | ||
}, | ||
mixedArrayProperty: [ | ||
"A", | ||
"B", | ||
{ | ||
inner: { | ||
innerProperty: "value", | ||
}, | ||
}, | ||
], | ||
}; | ||
|
||
const nitroConfig: NitroConfig = { | ||
runtimeConfig: defaultRuntimeConfig, | ||
baseURL: "https://example.com", | ||
experimental: { | ||
envExpansion: false, | ||
}, | ||
}; | ||
|
||
describe("normalizeRuntimeConfig", () => { | ||
it("should not warn on a serializable runtime config", () => { | ||
const warnSpy = vi.spyOn(console, "warn"); | ||
normalizeRuntimeConfig(nitroConfig); | ||
expect(warnSpy).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("should not warn when primitive prototype is changed", () => { | ||
const warnSpy = vi.spyOn(console, "warn"); | ||
|
||
// https://github.com/nitrojs/nitro/pull/2902 | ||
(String.prototype as any).brokenFunction = () => undefined; | ||
|
||
normalizeRuntimeConfig(nitroConfig); | ||
expect(warnSpy).not.toHaveBeenCalled(); | ||
|
||
delete (String.prototype as any).brokenFunction; | ||
}); | ||
|
||
it("should throw a warning when runtimeConfig is not serializable", () => { | ||
const warnSpy = vi.spyOn(console, "warn"); | ||
normalizeRuntimeConfig({ | ||
...nitroConfig, | ||
runtimeConfig: { | ||
...defaultRuntimeConfig, | ||
brokenProperty: new Map(), | ||
}, | ||
}); | ||
expect(warnSpy).toHaveBeenCalled(); | ||
}); | ||
}); |