/
main.test.ts
293 lines (242 loc) · 8.62 KB
/
main.test.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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* eslint-disable @typescript-eslint/no-var-requires,@typescript-eslint/ban-ts-comment */
import { describe, it, expect } from 'vitest';
import { isseven as isseven_prop } from '@/main.js';
import isseven from '@/main.js';
describe('imports to work', () => {
it('should work as an ESM import', () => {
expect(isseven).toBeTypeOf('function');
});
it('should work as an ESM named import', () => {
expect(isseven_prop).toBeTypeOf('function');
});
it('should work as a CJS default import', () => {
const isseven = require('../../dist/main.js').default;
expect(isseven).toBeTypeOf('function');
});
it('should work as a CJS property import', () => {
const isseven = require('../../dist/main.js').isseven;
expect(isseven).toBeTypeOf('function');
});
it('should work as a CJS destructuring import', () => {
const { isseven } = require('../../dist/main.js');
expect(isseven).toBeTypeOf('function');
});
it('should work as a CJS iterator destructure import', () => {
const [isseven] = require('../../dist/main.js').isseven;
expect(isseven).toBeTypeOf('function');
});
it('should work as a CJS async iterator import', async () => {
for await (const isseven of require('../../dist/main.js').isseven) {
expect(isseven).toBeTypeOf('function');
}
});
it('should work as an ESM import() default import', async () => {
const isseven = (await import('../../dist/main.js')).default;
expect(isseven).toBeTypeOf('function');
});
it('should work as an ESM import() property import', async () => {
const isseven = (await import('../../dist/main.js')).isseven;
expect(isseven).toBeTypeOf('function');
});
it('should work as an ESM import() destructuring import', async () => {
const { isseven } = await import('../../dist/main.js');
expect(isseven).toBeTypeOf('function');
});
it('should work as an ESM import() iterator destructure import', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
const [isseven] = (await import('../../dist/main.js')).isseven;
expect(isseven).toBeTypeOf('function');
});
it('should work as an ESM import() async iterator import', async () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
for await (const isseven of (await import('../../dist/main.js')).isseven) {
expect(isseven).toBeTypeOf('function');
}
});
// extra test for coverage, no clue why v8 doesn't cover this
it('should let both iterators run exactly once', async () => {
for await (const item of isseven) {
expect(item).toBeTypeOf('function');
}
for (const item of isseven) {
expect(item).toBeTypeOf('function');
}
});
});
describe('isseven to behave properly', () => {
it('should work with positive integers', () => {
expect(isseven(1)).toBe(false);
expect(isseven(7)).toBe(true);
});
it('should work with positive floats', () => {
expect(isseven(1.0)).toBe(false);
expect(isseven(7.0)).toBe(true);
expect(isseven(3.141592653589793)).toBe(false);
});
it('should work with positive infinity', () => {
expect(isseven(Infinity)).toBe(false);
});
it('should work with negative integers', () => {
expect(isseven(-1)).toBe(false);
expect(isseven(-7)).toBe(false);
});
it('should work with negative floats', () => {
expect(isseven(-1.0)).toBe(false);
expect(isseven(-7.0)).toBe(false);
expect(isseven(-3.141592653589793)).toBe(false);
});
it('should work with negative infinity', () => {
expect(isseven(-Infinity)).toBe(false);
});
it('should work with NaN', () => {
expect(isseven(NaN)).toBe(false);
});
it('should work with 0', () => {
expect(isseven(0)).toBe(false);
});
it('should work with negative 0', () => {
expect(isseven(-0)).toBe(false);
});
it('should work with 0 as a float', () => {
expect(isseven(0.0)).toBe(false);
});
it('should work with negative 0 as a float', () => {
expect(isseven(-0.0)).toBe(false);
});
it('should work with positive integers as a string', () => {
expect(isseven('1')).toBe(false);
expect(isseven('7')).toBe(true);
});
it('should work with positive floats as a string', () => {
expect(isseven('1.0')).toBe(false);
expect(isseven('7.0')).toBe(true);
expect(isseven('3.141592653589793')).toBe(false);
});
it('should work with positive infinity as a string', () => {
expect(isseven('Infinity')).toBe(false);
});
it('should work with negative integers as a string', () => {
expect(isseven('-1')).toBe(false);
expect(isseven('-7')).toBe(false);
});
it('should work with negative floats as a string', () => {
expect(isseven('-1.0')).toBe(false);
expect(isseven('-7.0')).toBe(false);
expect(isseven('-3.141592653589793')).toBe(false);
});
it('should work with negative infinity as a string', () => {
expect(isseven('-Infinity')).toBe(false);
});
it('should work with NaN as a string', () => {
expect(isseven('NaN')).toBe(false);
});
it('should work with 0 as a string', () => {
expect(isseven('0')).toBe(false);
});
it('should work with negative 0 as a string', () => {
expect(isseven('-0')).toBe(false);
});
it('should work with 0 as a float as a string', () => {
expect(isseven('0.0')).toBe(false);
});
it('should work with negative 0 as a float as a string', () => {
expect(isseven('-0.0')).toBe(false);
});
it('should work with positive integers as a BigInt', () => {
expect(isseven(1n)).toBe(false);
expect(isseven(7n)).toBe(true);
});
it('should work with positive infinity as a BigInt', () => {
expect(isseven(Infinity)).toBe(false);
});
it('should work with negative integers as a BigInt', () => {
expect(isseven(-1n)).toBe(false);
expect(isseven(-7n)).toBe(false);
});
it('should work with negative infinity as a BigInt', () => {
expect(isseven(-Infinity)).toBe(false);
});
it('should work with 0 as a BigInt', () => {
expect(isseven(0n)).toBe(false);
});
it('should work with negative 0 as a BigInt', () => {
expect(isseven(-0n)).toBe(false);
});
it('should work with null', () => {
expect(isseven(null)).toBe(false);
});
it('should work with undefined', () => {
expect(isseven(undefined)).toBe(false);
});
});
describe('throwing on invalid input', () => {
it('should not throw', () => {
expect(() => isseven(7)).not.toThrow();
expect(() => isseven(1)).not.toThrow();
});
});
describe('casting types', () => {
it('should work with strings', () => {
expect(isseven('7')).toBe(true);
expect(isseven('7.0')).toBe(true);
expect(isseven('1')).toBe(false);
});
it('should work with arrays', () => {
// @ts-ignore
expect(isseven([7])).toBe(true);
// @ts-ignore
expect(isseven([1])).toBe(false);
});
it('should work with objects', () => {
// @ts-ignore
expect(
isseven({
valueOf() {
return 7;
}
})
).toBe(true);
// @ts-ignore
expect(
isseven({
valueOf() {
return 1;
}
})
).toBe(false);
});
it('should work with booleans', () => {
// @ts-ignore
expect(isseven(true)).toBe(false);
// @ts-ignore
expect(isseven(false)).toBe(false);
});
it('should work with booleans as strings', () => {
expect(isseven('true')).toBe(false);
expect(isseven('false')).toBe(false);
});
it('should work with functions', () => {
function fn7() {}
fn7.valueOf = () => 7;
function fn1() {}
fn1.valueOf = () => 1;
// @ts-ignore
expect(isseven(fn7)).toBe(true);
// @ts-ignore
expect(isseven(fn1)).toBe(false);
});
});
describe('side effects', () => {
it('should do nothing when called', () => {
// @ts-ignore
isseven(7);
// @ts-ignore
expect().toBe();
});
it('should do nothing when not called', () => {
// @ts-ignore
expect().toBe();
});
});