@@ -9,7 +9,7 @@ import AttrChanged from 'x/attrChanged';
99import ReflectCamel from 'x/reflectCamel' ;
1010import WithChildElmsHasSlot from 'x/withChildElmsHasSlot' ;
1111import WithChildElmsHasSlotLight from 'x/withChildElmsHasSlotLight' ;
12- import { spyConsole } from '../../../helpers/console.js ' ;
12+ import { spyOn } from '@vitest/spy ' ;
1313import { USE_COMMENTS_FOR_FRAGMENT_BOOKENDS } from '../../../helpers/constants.js' ;
1414
1515const vFragBookend = USE_COMMENTS_FOR_FRAGMENT_BOOKENDS ? '<!---->' : '' ;
@@ -69,19 +69,9 @@ it('should create custom element if it exists before customElements.define', ()
6969describe ( 'non-empty custom element' , ( ) => {
7070 let consoleSpy ;
7171 beforeEach ( ( ) => {
72- consoleSpy = spyConsole ( ) ;
72+ consoleSpy = spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
7373 } ) ;
74- afterEach ( ( ) => {
75- consoleSpy . reset ( ) ;
76- } ) ;
77-
78- function expectWarnings ( expectedWarnings ) {
79- const observedWarnings = consoleSpy . calls . warn
80- . flat ( )
81- . map ( ( err ) => ( err instanceof Error ? err . message : err ) ) ;
82-
83- expect ( observedWarnings ) . toEqual ( expectedWarnings ) ;
84- }
74+ afterEach ( ( ) => consoleSpy . mockRestore ( ) ) ;
8575
8676 it ( 'should log error if non-native-shadow custom element has children' , ( ) => {
8777 const elm = document . createElement ( 'test-custom-element-preexisting2' ) ;
@@ -93,11 +83,11 @@ describe('non-empty custom element', () => {
9383 class extends WithChildElms . CustomElementConstructor { }
9484 ) ;
9585 if ( process . env . NODE_ENV !== 'production' && ! process . env . NATIVE_SHADOW ) {
96- expectWarnings ( [
97- 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.' ,
98- ] ) ;
86+ expect ( consoleSpy ) . toHaveBeenCalledExactlyOnceWith (
87+ 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
88+ ) ;
9989 } else {
100- expectWarnings ( [ ] ) ;
90+ expect ( consoleSpy ) . not . toHaveBeenCalled ( ) ;
10191 }
10292
10393 expect ( elm . shadowRoot . childNodes . length ) . toBe ( 1 ) ;
@@ -123,11 +113,11 @@ describe('non-empty custom element', () => {
123113 ) ;
124114
125115 if ( process . env . NODE_ENV !== 'production' ) {
126- expectWarnings ( [
127- 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.' ,
128- ] ) ;
116+ expect ( consoleSpy ) . toHaveBeenCalledExactlyOnceWith (
117+ 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
118+ ) ;
129119 } else {
130- expectWarnings ( [ ] ) ;
120+ expect ( consoleSpy ) . not . toHaveBeenCalled ( ) ;
131121 }
132122
133123 expect ( elm . innerHTML ) . toBe ( `${ vFragBookend } ${ vFragBookend } ` ) ;
@@ -143,11 +133,11 @@ describe('non-empty custom element', () => {
143133 class extends WithChildElmsHasSlot . CustomElementConstructor { }
144134 ) ;
145135 if ( process . env . NODE_ENV !== 'production' && ! process . env . NATIVE_SHADOW ) {
146- expectWarnings ( [
147- 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.' ,
148- ] ) ;
136+ expect ( consoleSpy ) . toHaveBeenCalledExactlyOnceWith (
137+ 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
138+ ) ;
149139 } else {
150- expectWarnings ( [ ] ) ;
140+ expect ( consoleSpy ) . not . toHaveBeenCalled ( ) ;
151141 }
152142
153143 expect ( elm . shadowRoot . childNodes . length ) . toBe ( 1 ) ;
@@ -174,11 +164,11 @@ describe('non-empty custom element', () => {
174164 ) ;
175165
176166 if ( process . env . NODE_ENV === 'production' ) {
177- expectWarnings ( [ ] ) ;
167+ expect ( consoleSpy ) . not . toHaveBeenCalled ( ) ;
178168 } else {
179- expectWarnings ( [
180- 'Found an existing shadow root for the custom element "Child". Call `hydrateComponent` instead.' ,
181- ] ) ;
169+ expect ( consoleSpy ) . toHaveBeenCalledExactlyOnceWith (
170+ 'Found an existing shadow root for the custom element "Child". Call `hydrateComponent` instead.'
171+ ) ;
182172 }
183173 expect ( elm . shadowRoot . innerHTML ) . toBe ( '<div></div>' ) ;
184174 } ) ;
@@ -194,11 +184,11 @@ describe('non-empty custom element', () => {
194184 document . body . appendChild ( elm ) ;
195185
196186 if ( process . env . NODE_ENV !== 'production' && ! process . env . NATIVE_SHADOW ) {
197- expectWarnings ( [
198- 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.' ,
199- ] ) ;
187+ expect ( consoleSpy ) . toHaveBeenCalledExactlyOnceWith (
188+ 'Light DOM and synthetic shadow custom elements cannot have child nodes. Ensure the element is empty, including whitespace.'
189+ ) ;
200190 } else {
201- expectWarnings ( [ ] ) ;
191+ expect ( consoleSpy ) . not . toHaveBeenCalled ( ) ;
202192 }
203193
204194 expect ( elm . childNodes . length ) . toBe ( 1 ) ;
0 commit comments