-
Notifications
You must be signed in to change notification settings - Fork 600
/
js-test.js
165 lines (151 loc) Β· 4.28 KB
/
js-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
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
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {Dependency} from '../../../types.flow';
import CountingSet from '../../../../lib/CountingSet';
import {wrap as raw} from 'jest-snapshot-serializer-raw';
import createModuleIdFactory from '../../../../lib/createModuleIdFactory';
import {wrapModule} from '../js';
import nullthrows from 'nullthrows';
let myModule;
expect.addSnapshotSerializer(require('jest-snapshot-serializer-raw'));
beforeEach(() => {
myModule = {
path: '/root/foo.js',
dependencies: new Map<string, Dependency>([
[
'bar',
{
absolutePath: '/bar.js',
data: {data: {asyncType: null, locs: [], key: 'bar'}, name: 'bar'},
},
],
[
'baz',
{
absolutePath: '/baz.js',
data: {data: {asyncType: null, locs: [], key: 'baz'}, name: 'baz'},
},
],
]),
getSource: () => Buffer.from(''),
inverseDependencies: new CountingSet(),
output: [
{
data: {
code: '__d(function() { console.log("foo") });',
lineCount: 1,
map: [],
},
type: 'js/module',
},
],
};
});
describe('wrapModule()', () => {
it('Should wrap a module in nondev mode', () => {
expect(
raw(
wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: false,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
}),
),
).toMatchInlineSnapshot(`__d(function() { console.log("foo") },0,[1,2]);`);
});
it('Should wrap a module in dev mode', () => {
expect(
raw(
wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: true,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
}),
),
).toMatchInlineSnapshot(
`__d(function() { console.log("foo") },0,[1,2],"foo.js");`,
);
});
it('should not wrap a script', () => {
myModule.output[0].type = 'js/script';
expect(
raw(
wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: true,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
}),
),
).toMatchInlineSnapshot(`__d(function() { console.log("foo") });`);
});
it('should use custom createModuleId param', () => {
// Just use a createModuleId that returns the same path.
expect(
raw(
wrapModule(myModule, {
createModuleId: (path: string) => path,
dev: false,
includeAsyncPaths: false,
projectRoot: '/root',
serverRoot: '/root',
}),
),
).toMatchInlineSnapshot(
`__d(function() { console.log("foo") },"/root/foo.js",["/bar.js","/baz.js"]);`,
);
});
it('includes the paths of async dependencies when requested', () => {
const dep = nullthrows(myModule.dependencies.get('bar'));
myModule.dependencies.set('bar', {
...dep,
data: {...dep.data, data: {...dep.data.data, asyncType: 'async'}},
});
expect(
raw(
wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: false,
includeAsyncPaths: true,
projectRoot: '/root',
serverRoot: '/root',
}),
),
).toMatchInlineSnapshot(
`__d(function() { console.log("foo") },0,{"0":1,"1":2,"paths":{"1":"../bar"}});`,
);
});
it('async dependency paths respect serverRoot', () => {
const dep = nullthrows(myModule.dependencies.get('bar'));
myModule.dependencies.set('bar', {
...dep,
data: {...dep.data, data: {...dep.data.data, asyncType: 'async'}},
});
expect(
raw(
wrapModule(myModule, {
createModuleId: createModuleIdFactory(),
dev: false,
includeAsyncPaths: true,
projectRoot: '/root',
serverRoot: '/',
}),
),
).toMatchInlineSnapshot(
`__d(function() { console.log("foo") },0,{"0":1,"1":2,"paths":{"1":"bar"}});`,
);
});
});