@@ -824,37 +824,70 @@ const emit = defineEmits(['a', 'b'])
824
824
} )
825
825
826
826
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
+ ) {
828
832
const { content } = compile ( `<script setup>${ code } </script>` )
829
833
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
+ }
830
839
}
831
840
832
841
test ( 'expression statement' , ( ) => {
833
- assertAwaitDetection ( `await foo` )
842
+ assertAwaitDetection ( `await foo` , `await _withAsyncContext(foo)` )
834
843
} )
835
844
836
845
test ( 'variable' , ( ) => {
837
- assertAwaitDetection ( `const a = 1 + (await foo)` )
846
+ assertAwaitDetection (
847
+ `const a = 1 + (await foo)` ,
848
+ `1 + (await _withAsyncContext(foo))`
849
+ )
838
850
} )
839
851
840
852
test ( 'ref' , ( ) => {
841
- assertAwaitDetection ( `ref: a = 1 + (await foo)` )
853
+ assertAwaitDetection (
854
+ `ref: a = 1 + (await foo)` ,
855
+ `1 + (await _withAsyncContext(foo))`
856
+ )
842
857
} )
843
858
844
859
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
+ } )
846
866
} )
847
867
848
868
test ( 'should ignore await inside functions' , ( ) => {
849
869
// function declaration
850
- assertAwaitDetection ( `async function foo() { await bar }` , false )
870
+ assertAwaitDetection (
871
+ `async function foo() { await bar }` ,
872
+ `await bar` ,
873
+ false
874
+ )
851
875
// function expression
852
- assertAwaitDetection ( `const foo = async () => { await bar }` , false )
876
+ assertAwaitDetection (
877
+ `const foo = async () => { await bar }` ,
878
+ `await bar` ,
879
+ false
880
+ )
853
881
// 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
+ )
855
887
// class method
856
888
assertAwaitDetection (
857
889
`const cls = class Foo { async method() { await bar }}` ,
890
+ `await bar` ,
858
891
false
859
892
)
860
893
} )
0 commit comments