-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathindex.test.ts
146 lines (130 loc) · 4.37 KB
/
index.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { createNext, FileRef } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { fetchViaHTTP } from 'next-test-utils'
import path from 'path'
import fs from 'fs-extra'
function extractJSON(response) {
return JSON.parse(response.headers.get('data') ?? '{}')
}
function baseNextConfig(): Parameters<typeof createNext>[0] {
return {
files: {
'src/add.wasm': new FileRef(path.join(__dirname, './add.wasm')),
'src/add.js': `
import wasm from './add.wasm?module'
const instance$ = WebAssembly.instantiate(wasm);
export async function increment(a) {
const { instance } = await instance$;
return instance.exports.add_one(a);
}
`,
'pages/index.js': `
export default function () { return <div>Hello, world!</div> }
`,
'middleware.js': `
import { increment } from './src/add.js'
export default async function middleware(request) {
const input = Number(request.nextUrl.searchParams.get('input')) || 1;
const value = await increment(input);
return new Response(null, { headers: { data: JSON.stringify({ input, value }) } });
}
`,
},
}
}
describe('edge api endpoints can use wasm files', () => {
let next: NextInstance
beforeAll(async () => {
next = await createNext({
files: {
'pages/api/add.js': `
import { increment } from '../../src/add.js'
export default async (request) => {
const input = Number(request.nextUrl.searchParams.get('input')) || 1;
const value = await increment(input);
return new Response(null, { headers: { data: JSON.stringify({ input, value }) } });
}
export const config = { runtime: 'edge' };
`,
'src/add.wasm': new FileRef(path.join(__dirname, './add.wasm')),
'src/add.js': `
import wasm from './add.wasm?module'
const instance$ = WebAssembly.instantiate(wasm);
export async function increment(a) {
const { instance } = await instance$;
return instance.exports.add_one(a);
}
`,
},
})
})
afterAll(() => next.destroy())
it('uses the wasm file', async () => {
const response = await fetchViaHTTP(next.url, '/api/add', { input: 10 })
expect(extractJSON(response)).toEqual({
input: 10,
value: 11,
})
})
})
describe('middleware can use wasm files', () => {
let next: NextInstance
beforeAll(async () => {
const config = baseNextConfig()
next = await createNext(config)
})
afterAll(() => next.destroy())
it('uses the wasm file', async () => {
const response = await fetchViaHTTP(next.url, '/')
expect(extractJSON(response)).toEqual({
input: 1,
value: 2,
})
})
it('can be called twice', async () => {
const response = await fetchViaHTTP(next.url, '/', { input: 2 })
expect(extractJSON(response)).toEqual({
input: 2,
value: 3,
})
})
if (!(global as any).isNextDeploy) {
it('lists the necessary wasm bindings in the manifest', async () => {
const manifestPath = path.join(next.testDir, '.next/server/middleware-manifest.json')
const manifest = await fs.readJSON(manifestPath)
expect(manifest.middleware['/']).toMatchObject({
wasm: [
{
filePath: 'server/edge-chunks/wasm_58ccff8b2b94b5dac6ef8957082ecd8f6d34186d.wasm',
name: 'wasm_58ccff8b2b94b5dac6ef8957082ecd8f6d34186d',
},
],
})
})
}
})
describe('middleware can use wasm files with the experimental modes on', () => {
let next: NextInstance
beforeAll(async () => {
const config = baseNextConfig()
config.files['next.config.js'] = `
module.exports = {
webpack(config) {
config.output.webassemblyModuleFilename = 'static/wasm/[modulehash].wasm'
// Since Webpack 5 doesn't enable WebAssembly by default, we should do it manually
config.experiments = { ...config.experiments, asyncWebAssembly: true }
return config
},
}
`
next = await createNext(config)
})
afterAll(() => next.destroy())
it('uses the wasm file', async () => {
const response = await fetchViaHTTP(next.url, '/')
expect(extractJSON(response)).toEqual({
input: 1,
value: 2,
})
})
})