-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathts-runner.spec.ts
142 lines (117 loc) · 3.73 KB
/
ts-runner.spec.ts
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
/* eslint-disable jest/valid-title */
import path from 'node:path'
import { jest } from '@jest/globals'
import { _dirname, nodeVersion, tsUseEsmSupported } from './helpers.js'
import type { AsyncWorkerFn } from './types.js'
import { MTS_SUPPORTED_NODE_VERSION, TsRunner } from 'synckit'
beforeEach(() => {
jest.resetModules()
})
const workerJsPath = path.resolve(_dirname, 'worker-js')
const workerMjsPath = path.resolve(_dirname, 'worker.mjs')
const workerMtsPath = path.resolve(_dirname, 'worker-mts.mjs')
test(TsRunner.EsbuildRegister, async () => {
const { createSyncFn } = await import('synckit')
let syncFn = createSyncFn<AsyncWorkerFn>(workerJsPath, {
tsRunner: TsRunner.EsbuildRegister,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
syncFn = createSyncFn<AsyncWorkerFn>(workerMjsPath, {
tsRunner: TsRunner.EsbuildRegister,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
if (tsUseEsmSupported) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() =>
createSyncFn<AsyncWorkerFn>(workerMtsPath, {
tsRunner: TsRunner.EsbuildRegister,
}),
).toThrow('esbuild-register is not supported for .mts files yet')
}
})
test(TsRunner.EsbuildRunner, async () => {
const { createSyncFn } = await import('synckit')
let syncFn = createSyncFn<AsyncWorkerFn>(workerJsPath, {
tsRunner: TsRunner.EsbuildRunner,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
syncFn = createSyncFn<AsyncWorkerFn>(workerMjsPath, {
tsRunner: TsRunner.EsbuildRunner,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
if (tsUseEsmSupported) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() =>
createSyncFn<AsyncWorkerFn>(workerMtsPath, {
tsRunner: TsRunner.EsbuildRunner,
}),
).toThrow('esbuild-runner is not supported for .mts files yet')
}
})
test(TsRunner.SWC, async () => {
const { createSyncFn } = await import('synckit')
let syncFn = createSyncFn<AsyncWorkerFn>(workerJsPath, {
tsRunner: TsRunner.SWC,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
syncFn = createSyncFn<AsyncWorkerFn>(workerMjsPath, {
tsRunner: TsRunner.SWC,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
if (tsUseEsmSupported) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() =>
createSyncFn<AsyncWorkerFn>(workerMtsPath, {
tsRunner: TsRunner.SWC,
}),
).toThrow('swc is not supported for .mts files yet')
}
})
test(TsRunner.TSX, async () => {
const { createSyncFn } = await import('synckit')
let syncFn = createSyncFn<AsyncWorkerFn>(workerJsPath, {
tsRunner: TsRunner.TSX,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
if (nodeVersion < MTS_SUPPORTED_NODE_VERSION) {
// eslint-disable-next-line jest/no-conditional-expect
expect(() =>
createSyncFn<AsyncWorkerFn>(workerMtsPath, {
tsRunner: TsRunner.TSX,
}),
).toThrow('tsx is not supported for .mts files yet')
return
}
if (!tsUseEsmSupported) {
return
}
syncFn = createSyncFn<AsyncWorkerFn>(workerMtsPath, {
tsRunner: TsRunner.TSX,
})
expect(syncFn(1)).toBe(1)
expect(syncFn(2)).toBe(2)
expect(syncFn(5)).toBe(5)
})
test('unknown ts runner', async () => {
const { createSyncFn } = await import('synckit')
expect(() =>
createSyncFn<AsyncWorkerFn>(path.resolve(_dirname, 'worker.js'), {
// @ts-expect-error -- intended
tsRunner: 'unknown',
}),
).toThrowErrorMatchingInlineSnapshot(`"Unknown ts runner: unknown"`)
})