-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathnftPack.test.js
80 lines (66 loc) · 2.25 KB
/
nftPack.test.js
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
import { vi, test, expect } from 'vitest'
import { findApiDistFunctions } from '@redwoodjs/internal/dist/files'
import * as nftPacker from '../packing/nft.js'
vi.mock('@vercel/nft', () => {
return {
nodeFileTrace: vi.fn(),
}
})
vi.mock('@redwoodjs/internal/dist/files', () => {
return {
findApiDistFunctions: () => {
return [
'/Users/carmack/dev/redwood/__fixtures__/example-todo-main/api/dist/functions/graphql.js',
'/Users/carmack/dev/redwood/__fixtures__/example-todo-main/api/dist/functions/healthz/healthz.js',
'/Users/carmack/dev/redwood/__fixtures__/example-todo-main/api/dist/functions/invalid/x.js',
'/Users/carmack/dev/redwood/__fixtures__/example-todo-main/api/dist/functions/nested/nested.js',
'/Users/carmack/dev/redwood/__fixtures__/example-todo-main/api/dist/functions/x/index.js',
]
},
}
})
vi.mock('@redwoodjs/project-config', () => {
return {
getPaths: () => {
return {
base: '/Users/carmack/dev/redwood/__fixtures__/example-todo-main/',
}
},
ensurePosixPath: (path) => {
return path.replace(/\\/g, '/')
},
}
})
test('Check packager detects all functions', () => {
const packageFileMock = vi
.spyOn(nftPacker, 'packageSingleFunction')
.mockResolvedValue(true)
nftPacker.nftPack()
expect(packageFileMock).toHaveBeenCalledTimes(5)
})
test('Creates entry file for nested functions correctly', () => {
const nestedFunction = findApiDistFunctions().find((fPath) =>
fPath.includes('nested'),
)
const [outputPath, content] = nftPacker.generateEntryFile(
nestedFunction,
'nested',
)
expect(outputPath).toBe('./api/dist/zipball/nested/nested.js')
expect(content).toMatchInlineSnapshot(
`"module.exports = require('./api/dist/functions/nested/nested.js')"`,
)
})
test('Creates entry file for top level functions correctly', () => {
const graphqlFunction = findApiDistFunctions().find((fPath) =>
fPath.includes('graphql'),
)
const [outputPath, content] = nftPacker.generateEntryFile(
graphqlFunction,
'graphql',
)
expect(outputPath).toBe('./api/dist/zipball/graphql/graphql.js')
expect(content).toMatchInlineSnapshot(
`"module.exports = require('./api/dist/functions/graphql.js')"`,
)
})