Skip to content

Commit 4072745

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

File tree

4 files changed

+76
-74
lines changed

4 files changed

+76
-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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
export const __test = {
8+
'Analytics should return AnalyticsBuffered': () => {
9+
const result = AnalyticsBrowser.load({ writeKey: 'abc' })
10+
assertNotAny(result)
11+
},
12+
'AnalyticsBuffered should return Promise<[Analytics, Context]> if awaited on.':
13+
async () => {
14+
// @ts-expect-error
15+
await new AnalyticsBuffered(() => null)
16+
17+
const [analytics, context] = await new AnalyticsBuffered(
18+
() => undefined as unknown as Promise<[Analytics, Context]>
19+
)
20+
21+
assertNotAny(analytics)
22+
assertIs<Analytics>(analytics)
23+
24+
assertNotAny(context)
25+
assertIs<Context>(context)
26+
},
27+
'general type def test': () => {
28+
/* Type definintion tests */
29+
void new AnalyticsBuffered(
30+
() => undefined as unknown as Promise<[Analytics, Context]>
31+
)
32+
.then(([analytics, context]) => {
33+
assertNotAny(analytics)
34+
assertIs<Analytics>(analytics)
35+
36+
assertNotAny(context)
37+
assertIs<Context>(context)
38+
})
39+
.then(() => {
40+
return 'a String!' as const
41+
})
42+
.then((str) => {
43+
assertNotAny(str)
44+
assertIs<'a String!'>(str)
45+
})
46+
},
47+
'if catch is before "then" in the middleware chain, should preserve type info */':
48+
() => {
49+
void new AnalyticsBuffered(
50+
() => undefined as unknown as Promise<[Analytics, Context]>
51+
)
52+
.catch(() => {
53+
return 'a String'
54+
})
55+
.then((response) => {
56+
assertNotAny(response)
57+
assertIs<string | [Analytics, Context]>(response)
58+
})
59+
},
60+
}

0 commit comments

Comments
 (0)