File tree Expand file tree Collapse file tree 3 files changed +54
-63
lines changed
Expand file tree Collapse file tree 3 files changed +54
-63
lines changed Original file line number Diff line number Diff line change 11import { describe , expect , test } from '@jest/globals' ;
2+ import { makeBlock , setupCodeMatcher } from '../shared.js' ;
23import injector from '../../injectors/modifyScriptSetup.js' ;
34
4- function makeBlock ( code ) {
5- return {
6- content : code ,
7- loc : { start : { offset : 0 } , end : { offset : code . length } }
8- }
9- }
10-
11- expect . extend ( {
12- toMatchCode ( received , expected ) {
13- const normalize = str => str . replace ( / \s + / g, ' ' ) . trim ( ) ;
14- const normalizedReceived = normalize ( received ) ;
15- const normalizedExpected = normalize ( expected ) ;
16-
17- const pass = normalizedReceived === normalizedExpected ;
18-
19- if ( pass ) {
20- return {
21- message : ( ) =>
22- `Expected code not to match:\n` +
23- `Expected: ${ this . utils . printExpected ( normalizedExpected ) } \n` +
24- `Received: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
25- pass : true
26- } ;
27- } else {
28- return {
29- message : ( ) =>
30- `Expected code to match:\n` +
31- `Expected: ${ this . utils . printExpected ( normalizedExpected ) } \n` +
32- `Received: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
33- pass : false
34- } ;
35- }
36- }
37- } ) ;
5+
6+ // Setup the custom matcher
7+ setupCodeMatcher ( ) ;
388
399describe ( 'injector' , ( ) => {
4010 test ( 'handles code with no useFusion import' , ( ) => {
Original file line number Diff line number Diff line change 11import { describe , expect , test } from '@jest/globals' ;
2+ import { makeBlock , setupCodeMatcher } from '../shared.js' ;
23import optionsWithSetup from '../../injectors/optionsWithSetup.js' ;
34
4- function makeBlock ( code ) {
5- return {
6- content : code ,
7- loc : { start : { offset : 0 } , end : { offset : code . length } }
8- } ;
9- }
10-
11- expect . extend ( {
12- toMatchCode ( received , expected ) {
13- const normalize = str => str . replace ( / \s + / g, ' ' ) . trim ( ) ;
14- const normalizedReceived = normalize ( received ) ;
15- const normalizedExpected = normalize ( expected ) ;
16- const pass = normalizedReceived === normalizedExpected ;
17-
18- if ( pass ) {
19- return {
20- message : ( ) =>
21- `Expected code not to match:\nExpected: ${ this . utils . printExpected ( normalizedExpected ) } \nReceived: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
22- pass : true
23- } ;
24- } else {
25- return {
26- message : ( ) =>
27- `Expected code to match:\nExpected: ${ this . utils . printExpected ( normalizedExpected ) } \nReceived: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
28- pass : false
29- } ;
30- }
31- }
32- } ) ;
5+
6+ // Setup the custom matcher
7+ setupCodeMatcher ( ) ;
338
349describe ( 'optionsWithSetup' , ( ) => {
3510 test ( 'handles code with no useFusion call' , ( ) => {
Original file line number Diff line number Diff line change 1+ // test-helpers.js
2+ import { expect } from '@jest/globals' ;
3+
4+ /**
5+ * Creates a code block object with location information
6+ * @param {string } code - The source code
7+ * @returns {Object } Block object with content and location info
8+ */
9+ export function makeBlock ( code ) {
10+ return {
11+ content : code ,
12+ loc : { start : { offset : 0 } , end : { offset : code . length } }
13+ } ;
14+ }
15+
16+ /**
17+ * Custom Jest matcher for comparing code strings while ignoring whitespace differences
18+ */
19+ export function setupCodeMatcher ( ) {
20+ expect . extend ( {
21+ toMatchCode ( received , expected ) {
22+ const normalize = str => str . replace ( / \s + / g, ' ' ) . trim ( ) ;
23+ const normalizedReceived = normalize ( received ) ;
24+ const normalizedExpected = normalize ( expected ) ;
25+ const pass = normalizedReceived === normalizedExpected ;
26+
27+ if ( pass ) {
28+ return {
29+ message : ( ) =>
30+ `Expected code not to match:\n` +
31+ `Expected: ${ this . utils . printExpected ( normalizedExpected ) } \n` +
32+ `Received: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
33+ pass : true
34+ } ;
35+ } else {
36+ return {
37+ message : ( ) =>
38+ `Expected code to match:\n` +
39+ `Expected: ${ this . utils . printExpected ( normalizedExpected ) } \n` +
40+ `Received: ${ this . utils . printReceived ( normalizedReceived ) } ` ,
41+ pass : false
42+ } ;
43+ }
44+ }
45+ } ) ;
46+ }
You can’t perform that action at this time.
0 commit comments