(t *testing.T)
| 1957 | } |
| 1958 | |
| 1959 | func TestContextAsyncFunction(t *testing.T) { |
| 1960 | useStableOwnerHooksForLegacySubtests(t) |
| 1961 | |
| 1962 | rt := NewRuntime() |
| 1963 | defer rt.Close() |
| 1964 | ctx := rt.NewContext() |
| 1965 | defer ctx.Close() |
| 1966 | |
| 1967 | t.Run("AsyncFunctionResolveNoArgs", func(t *testing.T) { |
| 1968 | // Test the resolve(ctx.NewUndefined()) branch when no arguments are passed |
| 1969 | asyncFn := ctx.NewAsyncFunction(func(ctx *Context, this *Value, promise *Value, args []*Value) *Value { |
| 1970 | resolve := promise.Get("resolve") |
| 1971 | defer resolve.Free() |
| 1972 | |
| 1973 | // Call resolve without passing any arguments to cover resolve(ctx.NewUndefined()) branch |
| 1974 | resolve.Execute(ctx.NewUndefined()) // No arguments passed |
| 1975 | return ctx.NewUndefined() |
| 1976 | }) |
| 1977 | |
| 1978 | ctx.Globals().Set("testAsyncResolveNoArgs", asyncFn) |
| 1979 | result := ctx.Eval(`testAsyncResolveNoArgs()`, EvalAwait(true)) |
| 1980 | defer result.Free() |
| 1981 | require.False(t, result.IsException()) |
| 1982 | require.True(t, result.IsUndefined()) // Should resolve to undefined |
| 1983 | }) |
| 1984 | |
| 1985 | t.Run("AsyncFunctionRejectWithArgs", func(t *testing.T) { |
| 1986 | // Test the reject(args[0]) branch when arguments are passed to reject |
| 1987 | asyncFn := ctx.NewAsyncFunction(func(ctx *Context, this *Value, promise *Value, args []*Value) *Value { |
| 1988 | reject := promise.Get("reject") |
| 1989 | defer reject.Free() |
| 1990 | |
| 1991 | // Call reject with an error argument to cover reject(args[0]) branch |
| 1992 | errorVal := ctx.NewError(errors.New("specific error message")) |
| 1993 | defer errorVal.Free() |
| 1994 | reject.Execute(ctx.NewUndefined(), errorVal) // Pass argument |
| 1995 | return ctx.NewUndefined() |
| 1996 | }) |
| 1997 | |
| 1998 | ctx.Globals().Set("testAsyncRejectWithArgs", asyncFn) |
| 1999 | result := ctx.Eval(`testAsyncRejectWithArgs()`, EvalAwait(true)) |
| 2000 | defer result.Free() |
| 2001 | require.True(t, result.IsException()) |
| 2002 | |
| 2003 | err := ctx.Exception() |
| 2004 | require.Error(t, err) |
| 2005 | require.Contains(t, err.Error(), "specific error message") |
| 2006 | }) |
| 2007 | |
| 2008 | t.Run("AsyncFunctionRejectNoArgs", func(t *testing.T) { |
| 2009 | // Test the reject without arguments branch (else clause in reject function) |
| 2010 | asyncFn := ctx.NewAsyncFunction(func(ctx *Context, this *Value, promise *Value, args []*Value) *Value { |
| 2011 | reject := promise.Get("reject") |
| 2012 | defer reject.Free() |
| 2013 | |
| 2014 | // Call reject without passing any arguments to cover the else branch |
| 2015 | // This will trigger: errObj := ctx.NewError(fmt.Errorf("Promise rejected without reason")) |
| 2016 | reject.Execute(ctx.NewUndefined()) // No arguments passed |
nothing calls this directly
no test coverage detected