Skip to content

Commit

Permalink
Add runtime information to async calls ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
Freymaurer committed Nov 30, 2023
1 parent 10f5793 commit af33c7f
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 18 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
/dist/py
/tests/Fable.Pytest.Tests/js
/tests/Fable.Pytest.Tests/py
/tests/js
/tests/py
/tests/**/js
/tests/**/py
/tests/**/ts
15 changes: 10 additions & 5 deletions Fable.Pyxpecto.sln
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Build", "build\Build.fsproj", "{F7027BE5-755C-4857-9AD5-2B58953A7FFE}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Fable.Pyxpecto.Tests", "tests\Fable.Pyxpecto.Tests.fsproj", "{78D65800-18D8-489F-99F9-9157D52F3749}"
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{F6DC93A8-274F-4BC7-8CBD-BEFAE46C9941}"
EndProject
Project("{6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705}") = "Mocha.Tests", "tests\Mocha.Tests\Mocha.Tests.fsproj", "{0C60BD36-F361-4C85-B86B-1BA1A7B05388}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -32,14 +34,17 @@ Global
{F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F7027BE5-755C-4857-9AD5-2B58953A7FFE}.Release|Any CPU.Build.0 = Release|Any CPU
{78D65800-18D8-489F-99F9-9157D52F3749}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78D65800-18D8-489F-99F9-9157D52F3749}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78D65800-18D8-489F-99F9-9157D52F3749}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78D65800-18D8-489F-99F9-9157D52F3749}.Release|Any CPU.Build.0 = Release|Any CPU
{0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0C60BD36-F361-4C85-B86B-1BA1A7B05388}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0C60BD36-F361-4C85-B86B-1BA1A7B05388} = {F6DC93A8-274F-4BC7-8CBD-BEFAE46C9941}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8760AA51-9BCC-433F-A279-C4273FC26858}
EndGlobalSection
Expand Down
2 changes: 1 addition & 1 deletion build/ProjectInfo.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ let project = "Fable.Pyxpecto"
let testProjects =
[
// add relative paths (from project root) to your testprojects here
"tests"
"tests/Mocha.Tests"
]

let solutionFile = $"{project}.sln"
Expand Down
26 changes: 22 additions & 4 deletions src/Pyxpecto.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@ open System
open Fable.Core.Testing
open Fable.Core

[<AttachMembers>]
type Stopwatch() =
member val StartTime: DateTime option = None with get, set
member val StopTime: DateTime option = None with get, set
member this.Start() = this.StartTime <- Some DateTime.Now
member this.Stop() =
match this.StartTime with
| Some _ -> this.StopTime <- Some DateTime.Now
| None -> failwith "Error. Unable to call `Stop` before `Start`."
member this.Elapsed : TimeSpan =
match this.StartTime, this.StopTime with
| Some start, Some stop -> stop - start
| _, _ -> failwith "Error. Unable to call `Elapsed` without calling `Start` and `Stop` before."

type FocusState =
| Normal
| Pending
Expand Down Expand Up @@ -350,10 +364,11 @@ module Pyxpecto =
member this.SumTests
with get() = this.SuccessfulTests + this.FailedTests + this.ErrorTests

member private this.printSuccessMsg (name: string) =
member private this.printSuccessMsg (name: string) (runtime: TimeSpan option) =
let focused = if this.HasFocused then "💎 | " else ""
this.SuccessfulTests <- this.SuccessfulTests + 1
printfn $"{focused}✔️ {name}"
let timespan = if runtime.IsSome then $" ({runtime.Value.ToString()})" else ""
printfn $"{focused}✔️ {name}{timespan}"
member private this.printErrorMsg (name: string) (msg: string) (isTrueError: bool)=
this.ErrorMessages.Add(name, msg)
let focused = if this.HasFocused then "💎 | " else ""
Expand All @@ -370,18 +385,21 @@ module Pyxpecto =
member this.RunSyncTest(name: string, body: unit -> unit) =
try
body()
this.printSuccessMsg name
this.printSuccessMsg name None
with
| :? AssertException as exn ->
this.printErrorMsg name exn.Message false
| e ->
this.printErrorMsg name e.Message true

member this.RunAsyncTest(name, body: Async<unit>) =
let stopwatch = new Stopwatch()
stopwatch.Start()
try
async {
do! body
this.printSuccessMsg name
stopwatch.Stop()
this.printSuccessMsg name (Some stopwatch.Elapsed)
}
|> Async.RunSynchronously
with
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,7 @@
<PackageReference Condition="'$(FABLE_COMPILER_JAVASCRIPT)' == 'true'" Include="Fable.Mocha" Version="2.17.0" />
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.13.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.4.0" />
<ProjectReference Include="..\..\src\Fable.Pyxpecto.fsproj" />
<PackageReference Update="FSharp.Core" Version="7.0.*" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Fable.Pyxpecto.fsproj" />
</ItemGroup>
</Project>
3 changes: 3 additions & 0 deletions tests/Mocha.Tests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Mocha.Tests

These tests are used to check if switching Pyxpecto with Mocha works in an .NET Expecto environment.

0 comments on commit af33c7f

Please sign in to comment.