@@ -6,14 +6,15 @@ import { instanceOf } from '../instanceOf.js';
6
6
describe ( 'instanceOf' , ( ) => {
7
7
it ( 'do not throw on values without prototype' , ( ) => {
8
8
class Foo {
9
+ readonly __isFoo = true as const ;
9
10
get [ Symbol . toStringTag ] ( ) {
10
11
return 'Foo' ;
11
12
}
12
13
}
13
14
14
- expect ( instanceOf ( true , Foo ) ) . to . equal ( false ) ;
15
- expect ( instanceOf ( null , Foo ) ) . to . equal ( false ) ;
16
- expect ( instanceOf ( Object . create ( null ) , Foo ) ) . to . equal ( false ) ;
15
+ expect ( instanceOf ( undefined , true , Foo ) ) . to . equal ( false ) ;
16
+ expect ( instanceOf ( undefined , null , Foo ) ) . to . equal ( false ) ;
17
+ expect ( instanceOf ( undefined , Object . create ( null ) , Foo ) ) . to . equal ( false ) ;
17
18
} ) ;
18
19
19
20
it ( 'detect name clashes with older versions of this lib' , ( ) => {
@@ -24,6 +25,7 @@ describe('instanceOf', () => {
24
25
25
26
function newVersion ( ) {
26
27
class Foo {
28
+ readonly __isFoo = true as const ;
27
29
get [ Symbol . toStringTag ] ( ) {
28
30
return 'Foo' ;
29
31
}
@@ -33,13 +35,17 @@ describe('instanceOf', () => {
33
35
34
36
const NewClass = newVersion ( ) ;
35
37
const OldClass = oldVersion ( ) ;
36
- expect ( instanceOf ( new NewClass ( ) , NewClass ) ) . to . equal ( true ) ;
37
- expect ( ( ) => instanceOf ( new OldClass ( ) , NewClass ) ) . to . throw ( ) ;
38
+ const newInstance = new NewClass ( ) ;
39
+ expect ( instanceOf ( newInstance . __isFoo , newInstance , NewClass ) ) . to . equal (
40
+ true ,
41
+ ) ;
42
+ expect ( ( ) => instanceOf ( undefined , new OldClass ( ) , NewClass ) ) . to . throw ( ) ;
38
43
} ) ;
39
44
40
45
it ( 'allows instances to have share the same constructor name' , ( ) => {
41
46
function getMinifiedClass ( tag : string ) {
42
47
class SomeNameAfterMinification {
48
+ readonly [ tag ] = true as const ;
43
49
get [ Symbol . toStringTag ] ( ) {
44
50
return tag ;
45
51
}
@@ -49,17 +55,25 @@ describe('instanceOf', () => {
49
55
50
56
const Foo = getMinifiedClass ( 'Foo' ) ;
51
57
const Bar = getMinifiedClass ( 'Bar' ) ;
52
- expect ( instanceOf ( new Foo ( ) , Bar ) ) . to . equal ( false ) ;
53
- expect ( instanceOf ( new Bar ( ) , Foo ) ) . to . equal ( false ) ;
58
+ const fooInstance = new Foo ( ) ;
59
+ const barInstance = new Bar ( ) ;
60
+ expect ( instanceOf ( fooInstance . foo , fooInstance , Bar ) ) . to . equal ( false ) ;
61
+ expect ( instanceOf ( barInstance . bar , barInstance , Foo ) ) . to . equal ( false ) ;
54
62
55
63
const DuplicateOfFoo = getMinifiedClass ( 'Foo' ) ;
56
- expect ( ( ) => instanceOf ( new DuplicateOfFoo ( ) , Foo ) ) . to . throw ( ) ;
57
- expect ( ( ) => instanceOf ( new Foo ( ) , DuplicateOfFoo ) ) . to . throw ( ) ;
64
+ const duplicateOfFooInstance = new DuplicateOfFoo ( ) ;
65
+ expect ( ( ) =>
66
+ instanceOf ( duplicateOfFooInstance . foo , new DuplicateOfFoo ( ) , Foo ) ,
67
+ ) . to . throw ( ) ;
68
+ expect ( ( ) =>
69
+ instanceOf ( fooInstance . foo , fooInstance , DuplicateOfFoo ) ,
70
+ ) . to . throw ( ) ;
58
71
} ) ;
59
72
60
73
it ( 'fails with descriptive error message' , ( ) => {
61
74
function getFoo ( ) {
62
75
class Foo {
76
+ readonly __isFoo = true as const ;
63
77
get [ Symbol . toStringTag ] ( ) {
64
78
return 'Foo' ;
65
79
}
@@ -69,11 +83,14 @@ describe('instanceOf', () => {
69
83
const Foo1 = getFoo ( ) ;
70
84
const Foo2 = getFoo ( ) ;
71
85
72
- expect ( ( ) => instanceOf ( new Foo1 ( ) , Foo2 ) ) . to . throw (
73
- / ^ C a n n o t u s e F o o " { } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
86
+ const foo1Instance = new Foo1 ( ) ;
87
+ const foo2Instance = new Foo2 ( ) ;
88
+
89
+ expect ( ( ) => instanceOf ( foo1Instance . __isFoo , foo1Instance , Foo2 ) ) . to . throw (
90
+ / ^ C a n n o t u s e F o o " { _ _ i s F o o : t r u e } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
74
91
) ;
75
- expect ( ( ) => instanceOf ( new Foo2 ( ) , Foo1 ) ) . to . throw (
76
- / ^ C a n n o t u s e F o o " { } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
92
+ expect ( ( ) => instanceOf ( foo2Instance . __isFoo , foo2Instance , Foo1 ) ) . to . throw (
93
+ / ^ C a n n o t u s e F o o " { _ _ i s F o o : t r u e } " f r o m a n o t h e r m o d u l e o r r e a l m ./ m,
77
94
) ;
78
95
} ) ;
79
96
} ) ;
0 commit comments