@@ -6,14 +6,15 @@ import { instanceOf } from '../instanceOf.js';
66describe ( 'instanceOf' , ( ) => {
77 it ( 'do not throw on values without prototype' , ( ) => {
88 class Foo {
9+ readonly __isFoo = true as const ;
910 get [ Symbol . toStringTag ] ( ) {
1011 return 'Foo' ;
1112 }
1213 }
1314
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 ) ;
1718 } ) ;
1819
1920 it ( 'detect name clashes with older versions of this lib' , ( ) => {
@@ -24,6 +25,7 @@ describe('instanceOf', () => {
2425
2526 function newVersion ( ) {
2627 class Foo {
28+ readonly __isFoo = true as const ;
2729 get [ Symbol . toStringTag ] ( ) {
2830 return 'Foo' ;
2931 }
@@ -33,13 +35,17 @@ describe('instanceOf', () => {
3335
3436 const NewClass = newVersion ( ) ;
3537 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 ( ) ;
3843 } ) ;
3944
4045 it ( 'allows instances to have share the same constructor name' , ( ) => {
4146 function getMinifiedClass ( tag : string ) {
4247 class SomeNameAfterMinification {
48+ readonly [ tag ] = true as const ;
4349 get [ Symbol . toStringTag ] ( ) {
4450 return tag ;
4551 }
@@ -49,17 +55,25 @@ describe('instanceOf', () => {
4955
5056 const Foo = getMinifiedClass ( 'Foo' ) ;
5157 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 ) ;
5462
5563 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 ( ) ;
5871 } ) ;
5972
6073 it ( 'fails with descriptive error message' , ( ) => {
6174 function getFoo ( ) {
6275 class Foo {
76+ readonly __isFoo = true as const ;
6377 get [ Symbol . toStringTag ] ( ) {
6478 return 'Foo' ;
6579 }
@@ -69,11 +83,14 @@ describe('instanceOf', () => {
6983 const Foo1 = getFoo ( ) ;
7084 const Foo2 = getFoo ( ) ;
7185
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,
7491 ) ;
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,
7794 ) ;
7895 } ) ;
7996} ) ;
0 commit comments