janestreet / bonsai

A library for building dynamic webapps, using Js_of_ocaml
MIT License
367 stars 39 forks source link

inline_tests work on v0.15.1 but not on v0.16.0 #37

Open mooreryan opened 1 year ago

mooreryan commented 1 year ago

I think I must be doing something obviously wrong here...but I can't get the inline tests to work with v0.16.0.

open! Core

let component = Bonsai.const Bonsai_web.Vdom.Node.(p [text "hello world"])

module Test = struct
  module Handle = Bonsai_web_test.Handle
  module Result_spec = Bonsai_web_test.Result_spec

  let%expect_test "it shows hello world" =
    let handle = Handle.create (Result_spec.vdom Fn.id) component in
    Handle.show handle ; [%expect {|  |}]
end

dune file

(library
 (name hi)
 (inline_tests
  (modes js))
 (libraries core bonsai.web bonsai.web_test)
 (preprocess
  (pps ppx_jane)))

Error when running tests

File "src/lib/test/dune", line 3, characters 1-28:
3 |  (inline_tests
4 |   (modes js))
node:internal/modules/cjs/loader:998
  throw err;
  ^

Error: Cannot find module 'deasync'
Require stack:
- /home/ryan/projects/x/_build/default/src/lib/test/.lib_test.inline-tests/inline_test_runner_lib_test.bc.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:995:15)
    at Module._load (node:internal/modules/cjs/loader:841:27)
    at Module.require (node:internal/modules/cjs/loader:1061:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at /home/ryan/projects/x/_build/default/src/lib/test/.lib_test.inline-tests/inline_test_runner_lib_test.bc.js:4158:18
    at Object.<anonymous> (/home/ryan/projects/x/_build/default/src/lib/test/.lib_test.inline-tests/inline_test_runner_lib_test.bc.js:11581:3)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/ryan/projects/x/_build/default/src/lib/test/.lib_test.inline-tests/inline_test_runner_lib_test.bc.js'
  ]
}

Node.js v18.11.0

Switching back to the v0.15.1 release of bonsai, the above works fine.

mooreryan commented 1 year ago

I also tried to set up the dune file as in the blog post on testing (https://github.com/janestreet/bonsai/blob/master/docs/blogs/testing.mdx):

(library (
  (name my_ui_test)
  (js_of_ocaml ())                   ; Test library must be marked with js_of_ocaml
  (libraries (core my_ui))
  (inline_tests (                    ; Native tests must be disabled
    (native     dont_build_dont_run)
    (javascript build_and_run)))))

But that gave other dune errors, so I'm assuming that isn't part of the solution.

mooreryan commented 1 year ago

Here is a "minimum" example that triggers the error when running dune runtest:

dune file

(library
 (name hi)
 (inline_tests
  (modes js))
 (libraries bonsai.web_test)
 (preprocess
  (pps ppx_inline_test)))

OCaml file

let%test _ = 1 = 2
mooreryan commented 1 year ago

So, I found a fix. I noticed that the bonsai.web_test pulls in async_js.async_test (here). Went to check on the runtime there and saw it required some JS package deasync (this).

So in the root of my project I ran npm install deasync, then reran the tests and it worked.

mooreryan commented 1 year ago

So, I guess my question now is, is the npm install deasync the "correct" thing to do? (Well, it works so I will use it for now.)

TyOverby commented 1 year ago

Ah, yes that is the right package, but I'm a bit surprised that deasync is required if you aren't using our bonsai-test + async integration. I'll look into this