Skip to content

Commit

Permalink
Node: register exit hook automatically
Browse files Browse the repository at this point in the history
Part of #314.
  • Loading branch information
aantron committed Apr 21, 2020
1 parent 728100f commit 3471d2a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 20 deletions.
12 changes: 3 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,17 +216,11 @@ and install it:
]
```

3. If your tests will be running on Node,
[call this function](https://github.com/aantron/bisect-starter-bsb/blob/master/hello.re#L2)
3. If you are using Jest, [call this
function](https://github.com/aantron/bisect-starter-bsb/blob/master/hello.re#L2)
somewhere in your tester, which will have Node write a file like
`bisect0123456789.coverage` when the tester exits:

```reason
Bisect.Runtime.write_coverage_data_on_exit();
```

If you are using Jest, try

```reason
afterAll(Bisect.Runtime.write_coverage_data);
```
Expand All @@ -238,7 +232,7 @@ somewhere in your tester, which will have Node write a file like
```

This returns binary coverage data in a `string option`, which you should
upload or otherwise get out of the browser, and write into an `.coverage`
upload or otherwise get out of the browser, and write into a `.coverage`
file yourself.

4. Build in development with `BISECT_ENABLE=yes`, run tests, and generate the
Expand Down
23 changes: 15 additions & 8 deletions src/runtime/bucklescript/runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@



let register_file
~bisect_file:_ ~bisect_silent:_ file ~point_count ~point_definitions =
Bisect_common.register_file file ~point_count ~point_definitions

let get_coverage_data =
Bisect_common.runtime_data_to_string

Expand All @@ -29,14 +25,25 @@ let write_coverage_data () =
in
create_file 100

let reset_coverage_data =
Bisect_common.reset_counters

let node_at_exit = [%bs.raw {|
function (callback) {
process.on("exit", callback);
if (typeof process !== 'undefined' && typeof process.on !== 'undefined')
process.on("exit", callback);
}
|}]

let exit_hook_added = ref false

let write_coverage_data_on_exit () =
node_at_exit write_coverage_data
if not !exit_hook_added then begin
node_at_exit (fun () -> write_coverage_data (); reset_coverage_data ());
exit_hook_added := true
end

let reset_coverage_data =
Bisect_common.reset_counters
let register_file
~bisect_file:_ ~bisect_silent:_ file ~point_count ~point_definitions =
write_coverage_data_on_exit ();
Bisect_common.register_file file ~point_count ~point_definitions
2 changes: 1 addition & 1 deletion test/bucklescript/expected
Original file line number Diff line number Diff line change
@@ -1 +1 @@
Coverage: 2/2 (100.00%)
Coverage: 1/1 (100.00%)
3 changes: 1 addition & 2 deletions test/bucklescript/hello.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
let () = {
Bisect.Runtime.write_coverage_data_on_exit();
print_endline("Hello, world!");
}
};

0 comments on commit 3471d2a

Please sign in to comment.