@@ -726,6 +726,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
726
726
setup,
727
727
teardown,
728
728
formatResult,
729
+ outputRaw,
729
730
fixtureOutputName,
730
731
fixtureOutputExt
731
732
} = localOptions ;
@@ -799,6 +800,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
799
800
code,
800
801
codeFixture : codePath ,
801
802
output,
803
+ outputRaw,
802
804
outputFixture : outputPath ,
803
805
exec,
804
806
execFixture : execPath
@@ -868,13 +870,14 @@ function pluginTester(options: PluginTesterOptions = {}) {
868
870
teardown,
869
871
formatResult,
870
872
snapshot,
871
- code : rawCode ,
872
- output : rawOutput ,
873
- exec : rawExec ,
873
+ code : originalCode ,
874
+ output : originalOutput ,
875
+ outputRaw,
876
+ exec : originalExec ,
874
877
fixture,
875
- codeFixture : rawCodeFixture ,
878
+ codeFixture : originalCodeFixture ,
876
879
outputFixture,
877
- execFixture : rawExecFixture
880
+ execFixture : originalExecFixture
878
881
} = mergeWith (
879
882
{
880
883
setup : noop ,
@@ -886,19 +889,23 @@ function pluginTester(options: PluginTesterOptions = {}) {
886
889
887
890
const codeFixture = getAbsolutePathUsingFilepathDirname (
888
891
filepath ,
889
- rawCodeFixture ?? fixture
892
+ originalCodeFixture ?? fixture
890
893
) ;
891
894
892
- const execFixture = getAbsolutePathUsingFilepathDirname ( filepath , rawExecFixture ) ;
895
+ const execFixture = getAbsolutePathUsingFilepathDirname (
896
+ filepath ,
897
+ originalExecFixture
898
+ ) ;
893
899
894
- const code = rawCode !== undefined ? stripIndent ( rawCode ) : readCode ( codeFixture ) ;
900
+ const code =
901
+ originalCode !== undefined ? stripIndent ( originalCode ) : readCode ( codeFixture ) ;
895
902
896
903
const output =
897
- rawOutput !== undefined
898
- ? stripIndent ( rawOutput )
904
+ originalOutput !== undefined
905
+ ? stripIndent ( originalOutput )
899
906
: readCode ( filepath , outputFixture ) ;
900
907
901
- const exec = rawExec ?? readCode ( execFixture ) ;
908
+ const exec = originalExec ?? readCode ( execFixture ) ;
902
909
903
910
const testConfig : MaybePluginTesterTestObjectConfig = mergeWith (
904
911
{ [ $type ] : 'test-object' } as const ,
@@ -945,6 +952,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
945
952
output !== undefined
946
953
? trimAndFixLineEndings ( output , endOfLine , code )
947
954
: undefined ,
955
+ outputRaw,
948
956
outputFixture,
949
957
exec,
950
958
execFixture :
@@ -973,9 +981,9 @@ function pluginTester(options: PluginTesterOptions = {}) {
973
981
verbose3 ( 'finalized test object: %O' , testConfig ) ;
974
982
975
983
validateTestConfig ( testConfig , {
976
- hasCodeAndCodeFixture : ! ! ( rawCode && codeFixture ) ,
977
- hasOutputAndOutputFixture : ! ! ( rawOutput && outputFixture ) ,
978
- hasExecAndExecFixture : ! ! ( rawExec && execFixture )
984
+ hasCodeAndCodeFixture : ! ! ( originalCode && codeFixture ) ,
985
+ hasOutputAndOutputFixture : ! ! ( originalOutput && outputFixture ) ,
986
+ hasExecAndExecFixture : ! ! ( originalExec && execFixture )
979
987
} ) ;
980
988
981
989
hasTests = true ;
@@ -1124,6 +1132,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
1124
1132
code,
1125
1133
codeFixture,
1126
1134
output,
1135
+ outputRaw,
1127
1136
outputFixture,
1128
1137
exec,
1129
1138
execFixture
@@ -1141,7 +1150,7 @@ function pluginTester(options: PluginTesterOptions = {}) {
1141
1150
`calling babel transform function (${ transformer . name } ) with parameters: %O` ,
1142
1151
parameters
1143
1152
) ;
1144
- return ( await transformer ( ...parameters ) ) ?. code ;
1153
+ return await transformer ( ...parameters ) ;
1145
1154
} catch ( error ) {
1146
1155
verbose2 ( `babel transformation failed with error: ${ error } ` ) ;
1147
1156
if ( expectedError ) {
@@ -1188,18 +1197,37 @@ function pluginTester(options: PluginTesterOptions = {}) {
1188
1197
) ;
1189
1198
} // ? Else condition is handled by the typeof === 'function' branch
1190
1199
}
1191
- } else if ( typeof rawBabelOutput !== 'string' ) {
1192
- throw new TypeError ( ErrorMessage . BabelOutputTypeIsNotString ( rawBabelOutput ) ) ;
1193
1200
} else {
1194
1201
debug2 ( 'expecting babel transform function to succeed' ) ;
1202
+
1203
+ // ? Since expectedError is not truthy, we know that rawBabelOutput must
1204
+ // ? be the return type of the babel transformer function
1205
+ const isRawBabelOutput = ! ! rawBabelOutput && typeof rawBabelOutput === 'object' ;
1206
+ const hasRawBabelOutputCode = isRawBabelOutput && 'code' in rawBabelOutput ;
1207
+
1208
+ if ( isRawBabelOutput && outputRaw ) {
1209
+ debug2 ( 'executing provided outputRaw function before final comparison' ) ;
1210
+ await outputRaw ( rawBabelOutput ) ;
1211
+ }
1212
+
1213
+ if ( ! hasRawBabelOutputCode || typeof rawBabelOutput . code !== 'string' ) {
1214
+ throw new TypeError (
1215
+ ErrorMessage . BabelOutputTypeIsNotString (
1216
+ hasRawBabelOutputCode ? rawBabelOutput . code : rawBabelOutput
1217
+ )
1218
+ ) ;
1219
+ }
1220
+
1221
+ debug2 ( 'performing final comparison' ) ;
1222
+
1195
1223
const formatResultFilepath = codeFixture || execFixture || filepath ;
1196
1224
1197
1225
// ? We split rawBabelOutput and result into two steps to ensure
1198
1226
// ? exceptions thrown by trimAndFixLineEndings and formatResult are not
1199
1227
// ? erroneously captured when we only really care about errors thrown by
1200
1228
// ? babel
1201
1229
const result = trimAndFixLineEndings (
1202
- await formatResult ( rawBabelOutput || '' , {
1230
+ await formatResult ( rawBabelOutput . code || '' , {
1203
1231
cwd : formatResultFilepath ? path . dirname ( formatResultFilepath ) : undefined ,
1204
1232
filepath : formatResultFilepath ,
1205
1233
filename : formatResultFilepath
@@ -1294,7 +1322,8 @@ function pluginTester(options: PluginTesterOptions = {}) {
1294
1322
exec,
1295
1323
output,
1296
1324
babelOptions,
1297
- expectedError
1325
+ expectedError,
1326
+ outputRaw
1298
1327
} = testConfig ;
1299
1328
1300
1329
if ( knownViolations ) {
@@ -1348,6 +1377,10 @@ function pluginTester(options: PluginTesterOptions = {}) {
1348
1377
throwTypeErrorWithDebugOutput ( ErrorMessage . InvalidHasThrowsAndOutput ( testConfig ) ) ;
1349
1378
}
1350
1379
1380
+ if ( outputRaw !== undefined && expectedError !== undefined ) {
1381
+ throwTypeErrorWithDebugOutput ( ErrorMessage . InvalidHasThrowsAndOutputRaw ( ) ) ;
1382
+ }
1383
+
1351
1384
if ( exec !== undefined && expectedError !== undefined ) {
1352
1385
throwTypeErrorWithDebugOutput ( ErrorMessage . InvalidHasThrowsAndExec ( testConfig ) ) ;
1353
1386
}
0 commit comments