Skip to content

Commit 45559e2

Browse files
authored
test: add possibility to specify url on integration test invocation + return body as string (#35)
1 parent d1dd8d4 commit 45559e2

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

tests/utils/fixture.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { tmpdir } from 'node:os'
1010
import { join } from 'node:path'
1111
import { fileURLToPath } from 'node:url'
1212
import { vi } from 'vitest'
13-
import { EnhancedNetlifyPluginOptions } from '../../src/helpers/types.js'
13+
import { streamToString } from './stream-to-string.js'
14+
import type { NetlifyPluginConstants, NetlifyPluginOptions } from '@netlify/build'
1415

1516
export interface FixtureTestContext extends TestContext {
1617
cwd: string
@@ -71,15 +72,20 @@ export async function runPluginAndExecute(
7172
* @default 'GET'
7273
*/
7374
httpMethod?: string
75+
/**
76+
* The relative path that should be requested
77+
* @default '/'
78+
*/
79+
url?: string
7480
/** The headers used for the invocation*/
7581
headers?: Record<string, string>
7682
/** The body that is used for the invocation */
7783
body?: unknown
7884
/** Additional constants or overrides that are used for the plugin execution */
79-
constants?: Partial<EnhancedNetlifyPluginOptions>
85+
constants?: Partial<NetlifyPluginConstants>
8086
} = {},
81-
): Promise<LambdaResponse> {
82-
const { constants, httpMethod, headers, body } = options
87+
) {
88+
const { constants, httpMethod, headers, body, url } = options
8389
const { onBuild } = await import('../../src/index.js')
8490
await onBuild({
8591
constants: {
@@ -98,7 +104,7 @@ export async function runPluginAndExecute(
98104
// INTERNAL_FUNCTIONS_SRC: '.netlify/functions-internal',
99105
// INTERNAL_EDGE_FUNCTIONS_SRC: '.netlify/edge-functions',
100106
},
101-
} as EnhancedNetlifyPluginOptions)
107+
} as unknown as NetlifyPluginOptions)
102108

103109
// We need to do a dynamic import as we mock the `process.cwd()` inside the createFixture function
104110
// If we import it before calling that it will resolve to the actual process working directory instead of the mocked one
@@ -135,15 +141,20 @@ export async function runPluginAndExecute(
135141
).toString('base64'),
136142
}
137143

138-
const response = await execute({
144+
const response = (await execute({
139145
event: {
140146
headers: headers || {},
141147
httpMethod: httpMethod || 'GET',
142-
rawUrl: 'https://example.netlify',
148+
rawUrl: new URL(url || '/', 'https://example.netlify').href,
143149
},
144150
environment,
145151
lambdaFunc: { handler },
146-
})
152+
})) as LambdaResponse
147153

148-
return response as LambdaResponse
154+
return {
155+
statusCode: response.statusCode,
156+
body: await streamToString(response.body),
157+
headers: response.headers,
158+
isBase64Encoded: response.isBase64Encoded,
159+
}
149160
}

tests/utils/stream-to-string.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* Converts a readable stream to a string
3+
* @param stream Node.js Readable stream
4+
* @returns
5+
*/
6+
export function streamToString(stream: NodeJS.ReadableStream) {
7+
const chunks: Buffer[] = []
8+
9+
return new Promise<string>((resolve, reject) => {
10+
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
11+
stream.on('error', (err) => reject(err))
12+
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
13+
})
14+
}

vite.config.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

vitest.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { defineConfig } from 'vitest/config'
33
export default defineConfig({
44
root: '.',
55
test: {
6+
globals: true,
67
restoreMocks: true,
78
clearMocks: true,
89
mockReset: true,

0 commit comments

Comments
 (0)