Skip to content

Commit 64121c2

Browse files
authored
test: require(json) should return the content (vitejs#20234)
1 parent 43ac73d commit 64121c2

File tree

7 files changed

+32
-0
lines changed

7 files changed

+32
-0
lines changed

playground/json/__tests__/csr/json-csr.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ test('?raw', async () => {
4949
)
5050
})
5151

52+
test('require(json) returns object without default export', async () => {
53+
expect(await page.textContent('.require')).toBe(JSON.stringify({ 200: 'ok' }))
54+
})
55+
5256
test.runIf(isServe)('should full reload', async () => {
5357
expect(await page.textContent('.hmr')).toBe(hmrStringified)
5458

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"200": "ok"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const content = require('./content.json')
2+
3+
module.exports = { content }
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "@vitejs/require",
3+
"version": "0.0.0",
4+
"main": "index.js"
5+
}

playground/json/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ <h2>JSON Module</h2>
2525
<h2>Has BOM Tag</h2>
2626
<pre class="bom"></pre>
2727

28+
<h2>require(json) returns object without default export</h2>
29+
<pre class="require"></pre>
30+
2831
<h2>HMR</h2>
2932
<pre class="hmr"></pre>
3033

@@ -61,6 +64,9 @@ <h2>HMR</h2>
6164
import hasBomJson from './json-bom/has-bom.json'
6265
text('.bom', JSON.stringify(hasBomJson))
6366

67+
import requireJson from '@vitejs/test-json-require'
68+
text('.require', JSON.stringify(requireJson.content))
69+
6470
import hmrJSON from './hmr.json'
6571
text('.hmr', JSON.stringify(hmrJSON))
6672

playground/json/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"preview": "vite preview"
1111
},
1212
"devDependencies": {
13+
"@vitejs/test-json-require": "file:./dep-json-require",
1314
"@vitejs/test-json-module": "file:./json-module",
1415
"vue": "^3.5.17"
1516
}

pnpm-lock.yaml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)