Skip to content

Commit 9c02f90

Browse files
committed
refactor typedef tests
1 parent 9b4c1c9 commit 9c02f90

File tree

4 files changed

+81
-74
lines changed

4 files changed

+81
-74
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"cjs": "tsc -p tsconfig.cjs.json",
2626
"run-example": "cd example && yarn && yarn dev",
2727
"clean": "rm -rf dist",
28-
"lint": "eslint '**/*.{js,jsx,ts,tsx}'",
28+
"lint": "tsc --noEmit && eslint '**/*.{js,jsx,ts,tsx}'",
2929
"prepare": "yarn pkg && husky install",
3030
"test": "jest"
3131
},

src/__tests__/analytics-pre-init.test.ts

Lines changed: 0 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -78,79 +78,6 @@ describe('buffered class', () => {
7878
})
7979
})
8080

81-
{
82-
/* Type definintion tests */
83-
;async () => {
84-
{
85-
/* TEST: AnalyticsBuffered should return the correct type if awaited on */
86-
87-
// @ts-expect-error
88-
await new AnalyticsBuffered(() => null)
89-
90-
const [analytics, context] = await new AnalyticsBuffered(
91-
() => undefined as unknown as Promise<[Analytics, Context]>
92-
)
93-
94-
const f: Analytics = analytics
95-
// @ts-expect-error
96-
analytics._SHOULD_ERR // check for any
97-
98-
const c: Context = context
99-
// @ts-expect-error
100-
c.SHOULD_ERR // check for any
101-
102-
console.log(f, c)
103-
}
104-
{
105-
void new AnalyticsBuffered(
106-
() => undefined as unknown as Promise<[Analytics, Context]>
107-
)
108-
.then(([analytics, context]) => {
109-
// @ts-expect-error
110-
analytics._SHOULD_ERR
111-
// @ts-expect-error
112-
context._SHOULD_ERR
113-
114-
const f: Analytics = analytics
115-
// @ts-expect-error
116-
analytics._SHOULD_ERR // check for any
117-
118-
const c: Context = context
119-
// @ts-expect-error
120-
c.SHOULD_ERR // check for any
121-
122-
console.log(f, c)
123-
})
124-
.then(() => {
125-
return 'a String!'
126-
})
127-
.then((str) => {
128-
/* TEST: chaining multiple .thens should preserve type info */
129-
// @ts-expect-error
130-
str.SHOULD_ERR // check for any
131-
132-
const aString: string = str
133-
134-
console.log(aString)
135-
})
136-
}
137-
{
138-
/* TEST: if catch is before "then" in the middleware chain, should preserve type info */
139-
void new AnalyticsBuffered(
140-
() => undefined as unknown as Promise<[Analytics, Context]>
141-
)
142-
.catch((reason) => {
143-
console.log(reason.SHOULD_NOT_ERR) // should be "any"
144-
return 'a String'
145-
})
146-
.then((response) => {
147-
const f: string | [Analytics, Context] = response // should return a union of either the "catch response" or "Analytics response"
148-
console.log(f)
149-
})
150-
}
151-
}
152-
}
153-
15481
describe('callAnalyticsMethod', () => {
15582
let ajs!: Analytics
15683
let resolveSpy!: jest.Mock<any, any>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
type IsAny<T> = unknown extends T ? (T extends {} ? T : never) : never
2+
type NotAny<T> = T extends IsAny<T> ? never : T
3+
type NotUnknown<T> = unknown extends T ? never : T
4+
5+
type NotTopType<T> = NotAny<T> & NotUnknown<T>
6+
7+
// this is not meant to be run, just for type tests
8+
export function assertNotAny<T>(val: NotTopType<T>) {
9+
console.log(val)
10+
}
11+
12+
// this is not meant to be run, just for type tests
13+
export function assertIs<T extends SomeType, SomeType = any>(val: T) {
14+
console.log(val)
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { Analytics } from '@/analytics'
2+
import { AnalyticsBuffered } from '@/analytics-pre-init'
3+
import { Context } from '@/core/context'
4+
import { AnalyticsBrowser } from '../../browser'
5+
import { assertNotAny, assertIs } from '../test-helpers/type-assertions'
6+
7+
/**
8+
* These are general typescript definition tests;
9+
* They aren't meant to be run by anything but the typescript compiler.
10+
*/
11+
export default {
12+
'Analytics should return AnalyticsBuffered': () => {
13+
const result = AnalyticsBrowser.load({ writeKey: 'abc' })
14+
assertNotAny(result)
15+
assertIs<AnalyticsBuffered>(result)
16+
},
17+
'AnalyticsBuffered should return Promise<[Analytics, Context]> if awaited on.':
18+
async () => {
19+
// @ts-expect-error
20+
await new AnalyticsBuffered(() => null)
21+
22+
const [analytics, context] = await new AnalyticsBuffered(
23+
() => undefined as unknown as Promise<[Analytics, Context]>
24+
)
25+
26+
assertNotAny(analytics)
27+
assertIs<Analytics>(analytics)
28+
29+
assertNotAny(context)
30+
assertIs<Context>(context)
31+
},
32+
'Promise API should work': () => {
33+
void new AnalyticsBuffered(
34+
() => undefined as unknown as Promise<[Analytics, Context]>
35+
)
36+
.then(([analytics, context]) => {
37+
assertNotAny(analytics)
38+
assertIs<Analytics>(analytics)
39+
40+
assertNotAny(context)
41+
assertIs<Context>(context)
42+
})
43+
.then(() => {
44+
return 'a String!' as const
45+
})
46+
.then((str) => {
47+
assertNotAny(str)
48+
assertIs<'a String!'>(str)
49+
})
50+
},
51+
'If catch is before "then" in the middleware chain, .then should take into account the catch clause':
52+
() => {
53+
void new AnalyticsBuffered(
54+
() => undefined as unknown as Promise<[Analytics, Context]>
55+
)
56+
.catch((err: string) => {
57+
assertIs<string>(err)
58+
return 123
59+
})
60+
.then((response) => {
61+
assertNotAny(response)
62+
assertIs<number | [Analytics, Context]>(response)
63+
})
64+
},
65+
}

0 commit comments

Comments
 (0)