-
Notifications
You must be signed in to change notification settings - Fork 654
Expand file tree
/
Copy pathimplementation.test.ts
More file actions
212 lines (187 loc) · 6.3 KB
/
Copy pathimplementation.test.ts
File metadata and controls
212 lines (187 loc) · 6.3 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import { getPlatformVersion } from '@metamask/snaps-utils';
import {
DEFAULT_SNAP_BUNDLE,
DEFAULT_SNAP_ICON,
getMockSnapFilesWithUpdatedChecksum,
getPackageJson,
getSnapManifest,
} from '@metamask/snaps-utils/test-utils';
import normalFs from 'fs';
import { dirname } from 'path';
import type { Configuration } from 'webpack';
import { build } from './implementation';
import { getMockConfig } from '../../test-utils';
import { getCompiler } from '../../webpack';
import type * as webpack from '../../webpack';
import type * as utils from '../../webpack/utils';
import { BROWSERSLIST_FILE } from '../../webpack/utils';
const { promises: fs } = normalFs;
jest.mock('fs');
jest.mock('../../webpack', () => ({
...jest.requireActual('../../webpack'),
getCompiler: jest.fn<
ReturnType<typeof getCompiler>,
Parameters<typeof getCompiler>
>(async (...args) => {
const compiler = await jest
.requireActual<typeof webpack>('../../webpack')
.getCompiler(...args);
// @ts-expect-error: Type mismatch.
compiler.inputFileSystem = normalFs;
// @ts-expect-error: Type mismatch.
compiler.outputFileSystem = normalFs;
return compiler;
}),
}));
jest.mock('../../webpack/utils', () => ({
...jest.requireActual('../../webpack/utils'),
getDefaultLoader: jest.fn<
ReturnType<typeof utils.getDefaultLoader>,
Parameters<typeof utils.getDefaultLoader>
>(async (config) => {
return jest
.requireActual<typeof utils>('../../webpack/utils')
.getDefaultLoader(config);
}),
}));
describe('build', () => {
beforeEach(async () => {
const { manifest } = await getMockSnapFilesWithUpdatedChecksum({
manifest: getSnapManifest({
platformVersion: getPlatformVersion(),
}),
});
await fs.mkdir('/snap');
await fs.writeFile('/snap/input.js', DEFAULT_SNAP_BUNDLE);
await fs.writeFile(
'/snap/snap.manifest.json',
JSON.stringify(manifest.result),
);
await fs.writeFile('/snap/package.json', JSON.stringify(getPackageJson()));
await fs.mkdir('/snap/images');
await fs.writeFile('/snap/images/icon.svg', DEFAULT_SNAP_ICON);
await fs.mkdir(dirname(BROWSERSLIST_FILE), { recursive: true });
await fs.writeFile(
BROWSERSLIST_FILE,
await jest
.requireActual<typeof normalFs>('fs')
.promises.readFile(BROWSERSLIST_FILE),
);
});
afterEach(async () => {
await fs.rm('/snap', { force: true, recursive: true });
});
it('builds the snap bundle using Webpack', async () => {
jest.spyOn(process, 'cwd').mockReturnValue('/snap');
const warn = jest.spyOn(console, 'warn').mockImplementation();
const config = getMockConfig({
input: '/snap/input.js',
output: {
path: './',
filename: 'output.js',
},
evaluate: false,
manifest: {
path: '/snap/snap.manifest.json',
},
customizeWebpackConfig: (webpackConfig: Configuration) => {
delete webpackConfig.module?.rules;
return webpackConfig;
},
});
await build(config);
expect(warn).toHaveBeenCalledWith(
expect.stringMatching(
/Compiled \d+ files? in \d+ms with \d+ warnings?\./u,
),
);
const output = await fs.readFile('/snap/output.js', 'utf8');
expect(output).toMatchInlineSnapshot(
`"(()=>{var r={157:r=>{r.exports.onRpcRequest=({request:r})=>{console.log("Hello, world!");const{method:e,id:o}=r;return e+o}}},e={};var o=function o(t){var s=e[t];if(void 0!==s)return s.exports;var n=e[t]={exports:{}};return r[t](n,n.exports,o),n.exports}(157);module.exports=o})();"`,
);
});
it('builds an unminimized snap bundle using Webpack', async () => {
jest.spyOn(process, 'cwd').mockReturnValue('/snap');
const warn = jest.spyOn(console, 'warn').mockImplementation();
const config = getMockConfig({
input: '/snap/input.js',
output: {
path: './',
filename: 'output.js',
minimize: false,
},
evaluate: false,
manifest: {
path: '/snap/snap.manifest.json',
},
customizeWebpackConfig: (webpackConfig: Configuration) => {
delete webpackConfig.module?.rules;
return webpackConfig;
},
});
await build(config);
expect(warn).toHaveBeenCalledWith(
expect.stringMatching(
/Compiled \d+ files? in \d+ms with \d+ warnings?\./u,
),
);
const output = await fs.readFile('/snap/output.js', 'utf8');
expect(output).toMatchInlineSnapshot(`
"(() => {
var __webpack_modules__ = {
157: module => {
module.exports.onRpcRequest = ({
request
}) => {
console.log("Hello, world!");
const {
method,
id
} = request;
return method + id;
};
}
};
var __webpack_module_cache__ = {};
function __webpack_require__(moduleId) {
var cachedModule = __webpack_module_cache__[moduleId];
if (cachedModule !== undefined) {
return cachedModule.exports;
}
var module = __webpack_module_cache__[moduleId] = {
exports: {}
};
__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
return module.exports;
}
var __webpack_exports__ = __webpack_require__(157);
module.exports = __webpack_exports__;
})();"
`);
});
it('rejects if the compiler has an error', async () => {
const error = new Error('Compiler error.');
const mock = getCompiler as jest.MockedFunction<typeof getCompiler>;
// @ts-expect-error - Partial mock.
mock.mockImplementationOnce(() => ({
run: jest.fn((callback) => {
callback(error);
}),
}));
await expect(build(getMockConfig())).rejects.toThrow(error);
});
it('rejects if closing the compiler has an error', async () => {
const error = new Error('Close compiler error.');
const mock = getCompiler as jest.MockedFunction<typeof getCompiler>;
// @ts-expect-error - Partial mock.
mock.mockImplementationOnce(() => ({
run: jest.fn((callback) => {
callback(null);
}),
close: jest.fn((callback) => {
callback(error);
}),
}));
await expect(build(getMockConfig())).rejects.toThrow(error);
});
});