@@ -824,37 +824,70 @@ const emit = defineEmits(['a', 'b'])
824824 } )
825825
826826 describe ( 'async/await detection' , ( ) => {
827- function assertAwaitDetection ( code : string , shouldAsync = true ) {
827+ function assertAwaitDetection (
828+ code : string ,
829+ expected : string | ( ( content : string ) => boolean ) ,
830+ shouldAsync = true
831+ ) {
828832 const { content } = compile ( `<script setup>${ code } </script>` )
829833 expect ( content ) . toMatch ( `${ shouldAsync ? `async ` : `` } setup(` )
834+ if ( typeof expected === 'string' ) {
835+ expect ( content ) . toMatch ( expected )
836+ } else {
837+ expect ( expected ( content ) ) . toBe ( true )
838+ }
830839 }
831840
832841 test ( 'expression statement' , ( ) => {
833- assertAwaitDetection ( `await foo` )
842+ assertAwaitDetection ( `await foo` , `await _withAsyncContext(foo)` )
834843 } )
835844
836845 test ( 'variable' , ( ) => {
837- assertAwaitDetection ( `const a = 1 + (await foo)` )
846+ assertAwaitDetection (
847+ `const a = 1 + (await foo)` ,
848+ `1 + (await _withAsyncContext(foo))`
849+ )
838850 } )
839851
840852 test ( 'ref' , ( ) => {
841- assertAwaitDetection ( `ref: a = 1 + (await foo)` )
853+ assertAwaitDetection (
854+ `ref: a = 1 + (await foo)` ,
855+ `1 + (await _withAsyncContext(foo))`
856+ )
842857 } )
843858
844859 test ( 'nested statements' , ( ) => {
845- assertAwaitDetection ( `if (ok) { await foo } else { await bar }` )
860+ assertAwaitDetection ( `if (ok) { await foo } else { await bar }` , code => {
861+ return (
862+ code . includes ( `await _withAsyncContext(foo)` ) &&
863+ code . includes ( `await _withAsyncContext(bar)` )
864+ )
865+ } )
846866 } )
847867
848868 test ( 'should ignore await inside functions' , ( ) => {
849869 // function declaration
850- assertAwaitDetection ( `async function foo() { await bar }` , false )
870+ assertAwaitDetection (
871+ `async function foo() { await bar }` ,
872+ `await bar` ,
873+ false
874+ )
851875 // function expression
852- assertAwaitDetection ( `const foo = async () => { await bar }` , false )
876+ assertAwaitDetection (
877+ `const foo = async () => { await bar }` ,
878+ `await bar` ,
879+ false
880+ )
853881 // object method
854- assertAwaitDetection ( `const obj = { async method() { await bar }}` , false )
882+ assertAwaitDetection (
883+ `const obj = { async method() { await bar }}` ,
884+ `await bar` ,
885+ false
886+ )
855887 // class method
856888 assertAwaitDetection (
857889 `const cls = class Foo { async method() { await bar }}` ,
890+ `await bar` ,
858891 false
859892 )
860893 } )
0 commit comments