-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaybe-map-sourcemaps.test.ts
More file actions
255 lines (212 loc) · 7.8 KB
/
maybe-map-sourcemaps.test.ts
File metadata and controls
255 lines (212 loc) · 7.8 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import fs from 'node:fs';
import { FileSystem } from '@effect/platform/FileSystem';
import { NodeFileSystem } from '@effect/platform-node';
import { Effect, Layer, Match, pipe } from 'effect';
import { describe, expect, it, vi } from 'vitest';
import { makeFsTestLayer } from '@tests/layers';
import {
fromPromiseStack,
fromPromiseTaskSources,
parallelErrorsStack,
parallelErrorsTaskSources,
} from '@tests/mock-data';
import { execShellCommand, getExampleSources } from '@tests/util';
import type { RawErrorLocation } from './get-sources-from-map-file.js';
describe('maybeMapSourcemaps function', () => {
it('should extract sources from a typescript file', async () => {
const fromPromiseSources = await getExampleSources('from-promise');
const { FsTestLayer } = makeFsTestLayer({
readFileString: Effect.succeed(fromPromiseSources),
});
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(
maybeMapSourcemaps('fetchTask', fromPromiseStack),
Effect.provide(FsTestLayer),
),
);
expect(result).toStrictEqual(fromPromiseTaskSources);
});
it('should extract sources from a parallel run', async () => {
const parallelSources = await getExampleSources('parallel-errors');
const { FsTestLayer } = makeFsTestLayer({
readFileString: Effect.succeed(parallelSources),
});
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(
maybeMapSourcemaps('readUser', parallelErrorsStack),
Effect.provide(FsTestLayer),
),
);
expect(result).toStrictEqual(parallelErrorsTaskSources);
});
it('should return no sources', async () => {
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(
maybeMapSourcemaps('readUser', [
'at /Users/jpb06/repos/perso/effect-errors/src/examples/parallel-errors.ts',
]),
Effect.provide(NodeFileSystem.layer),
),
);
expect(result).toStrictEqual([
{
_tag: 'stack-entry',
runPath:
'at /Users/jpb06/repos/perso/effect-errors/src/examples/parallel-errors.ts',
},
]);
});
it('should return locations when no map file is associated to a js file', async () => {
const jsFile =
'at /Users/jpb06/repos/perso/effect-errors/src/yolo.js:40:20';
const { FsTestLayer } = makeFsTestLayer({
exists: Effect.succeed(false),
});
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(maybeMapSourcemaps('yolo', [jsFile]), Effect.provide(FsTestLayer)),
);
expect(result).toHaveLength(1);
expect(result[0]._tag).toBe('location');
const location = result[0] as RawErrorLocation;
expect(location.column).toBe(20);
expect(location.line).toBe(40);
expect(location.filePath.endsWith('/src/yolo.js'));
});
it('should return no sources if map file is invalid', async () => {
const jsFile =
'at /Users/jpb06/repos/perso/effect-errors/src/yolo.js:40:20';
const { FsTestLayer } = makeFsTestLayer({
exists: Effect.succeed(true),
readFileString: Effect.succeed(`{ "version": 3 }`),
});
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(maybeMapSourcemaps('ono', [jsFile]), Effect.provide(FsTestLayer)),
);
expect(result).toStrictEqual([
{
_tag: 'stack-entry',
runPath: 'at /Users/jpb06/repos/perso/effect-errors/src/yolo.js:40:20',
},
]);
});
it('should return sources from the map file associated with a js file', async () => {
const jsFile =
'/Users/jpb06/repos/perso/effect-errors/src/tests/bundle/from-promise.js:168:388';
const mapFile = await fs.promises.readFile(
'./src/tests/bundle/from-promise.js.map',
{
encoding: 'utf-8',
},
);
const fromPromiseSources = await getExampleSources('from-promise');
const { FsTestLayer } = makeFsTestLayer({
exists: Effect.succeed(true),
readFileString: vi.fn().mockImplementation((path: string) => {
return Match.value(path).pipe(
Match.when(
(path) => path.endsWith('from-promise.js.map'),
() => Effect.succeed(mapFile),
),
Match.when(
(path) => path.endsWith('from-promise.ts'),
() => Effect.succeed(fromPromiseSources),
),
Match.orElseAbsurd,
);
}),
});
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(
maybeMapSourcemaps('fetchTask', [`at ${jsFile}`]),
Effect.provide(FsTestLayer),
),
);
expect(result).toStrictEqual([
{
...fromPromiseTaskSources[1],
runPath: jsFile,
sourcesPath: `/Users/jpb06/repos/perso/${fromPromiseTaskSources[1].runPath}`,
},
]);
});
it('should return sources from a js bundle', async () => {
const result = await execShellCommand(
'node ./src/tests/bundle/from-promise.js',
);
expect(result.error).toBeDefined();
expect(result.result).toBeUndefined();
const parsedError = JSON.parse(result.error as never);
expect(parsedError.interrupted).toBe(false);
expect(Array.isArray(parsedError.errors)).toBe(true);
expect(parsedError.errors.length).toBe(1);
const effectError = parsedError.errors.at(0);
const runPaths = [
'effect-errors/src/tests/bundle/from-promise.js:168:366',
'effect-errors/src/tests/bundle/from-promise.js:168:388',
'effect-errors/src/tests/bundle/from-promise.js:168:655',
];
expect(effectError.sources?.length).toBe(3);
for (let i = 0; i < effectError.sources.length; i++) {
const error = effectError.sources[i];
const expected = fromPromiseTaskSources[i];
expect(error.runPath.endsWith(runPaths[i])).toBe(true);
expect(error.sourcesPath.endsWith(expected.runPath)).toBe(true);
expect(error.source).not.toBeUndefined();
expect(error.source).toStrictEqual(
expected.source!.map((d) =>
d.column === undefined ? { code: d.code, line: d.line } : d,
),
);
}
});
it('should handle stacktraces with trailing spaces', async () => {
const jsFile =
'/Users/jpb06/repos/perso/effect-errors/src/tests/bundle/from-promise.js:168:388';
const mapFile = await fs.promises.readFile(
'./src/tests/bundle/from-promise.js.map',
{
encoding: 'utf-8',
},
);
const fromPromiseSources = await getExampleSources('from-promise');
const TestFileSystemlayer = Layer.succeed(
FileSystem,
FileSystem.of({
exists: () => Effect.succeed(true),
readFileString: (path: string) => {
return Match.value(path).pipe(
Match.when(
(path) => path.endsWith('from-promise.js.map'),
() => Effect.succeed(mapFile),
),
Match.when(
(path) => path.endsWith('from-promise.ts'),
() => Effect.succeed(fromPromiseSources),
),
Match.orElseAbsurd,
);
},
} as unknown as FileSystem),
);
const { maybeMapSourcemaps } = await import('./maybe-map-sourcemaps.js');
const result = await Effect.runPromise(
pipe(
maybeMapSourcemaps('fetchTask', [` at file://${jsFile}`]),
Effect.provide(TestFileSystemlayer),
),
);
expect(result).toStrictEqual([
{
...fromPromiseTaskSources[1],
runPath: jsFile,
sourcesPath: `/Users/jpb06/repos/perso/${fromPromiseTaskSources[1].runPath}`,
},
]);
});
});