Fork me on GitHub
#nbb
<
2022-06-01
>
genRaiy15:06:24

I'm trying to run some test code following the example

genRaiy15:06:51

and it gives me Message: No namespace: cljs.test

genRaiy15:06:19

I changed the orginal ns and put it into a test folder

(ns modulr-test
  (:require
    [cljs.test :as t :refer [async deftest is testing]]
    [promesa.core :as p]))

(deftest awesome-test
         (testing "one equals one"
                  (is (= 1 1)))
         (testing "this test will fail"
                  (is (= 1 2))))

;; run this function with: nbb -m example/run-tests
(defn run-tests []
      (t/run-tests 'modulr-test))

genRaiy15:06:50

nbb -m modulr-test/run-tests

borkdude15:06:33

@raymcdermott Which version of nbb are you using?

borkdude15:06:09

You might have to upgrade

genRaiy15:06:40

I have 0.3.2 from the CLI but 0.5.102 in package.json ... how do I sync the two??

genRaiy15:06:06

I tried npm install nbb -g but no to effect

borkdude15:06:16

Can you try npx nbb ...?

borkdude15:06:40

npx will always pick the local one if it can

genRaiy15:06:31

npx nbb -m modulr-test/run-tests does work!

genRaiy15:06:05

now to figure out the CLASSPATH for testing 🙂

borkdude15:06:05

@raymcdermott nbb.classpath/add-classpath works, or nbb --classpath ...

1
genRaiy17:06:50

:thinking_face: it doesn't seem able to pick up .js files in the src folder.

genRaiy17:06:37

Message: Cannot find module '/home/ray/xxx/repos/ops-modulr/signature' imported from /home/ray/xxx/repos/ops-modulr/script.cljs

genRaiy17:06:10

in the :require I have

["./signature$default" :as modulr-hmac]

genRaiy17:06:01

[ btw there is no script.cljs so I assume that this an artefact of nbb ? ]

genRaiy17:06:47

Ran using npx nbb --classpath src:test -m modulr-test/run-tests

genRaiy17:06:55

In other news I can report that It does work for other nses

borkdude17:06:38

@raymcdermott CLJS files aren't imported through string

borkdude17:06:04

(:require [signature :as modulr-hmac])

borkdude17:06:26

If you can post your project layout, maybe that helps

genRaiy19:06:26

$ ls -R src test *js *.json *.edn
auth.mjs  aws.mjs  borrowers.mjs  deps.edn  package.json  package-lock.json  pending.mjs

src:
auth.cljs  aws_secrets.cljs  borrowers.cljs  config.cljs  modulr.cljs  pending.cljs  signature.js

test:
auth_test.cljs  modulr_test.cljs

genRaiy19:06:27

signature is a js file and I think you advised the "./" but I'll try without

borkdude19:06:31

ah yes, but the classpath only applies to .cljs files

borkdude19:06:43

So ./src/signature.js possibly works

borkdude19:06:02

or just place signature in the root? Not sure why it's in src

genRaiy19:06:29

when it's src the tests work but the then the nodejs doesn't work

genRaiy19:06:03

it's in src cos it's src 😉 what do I know

genRaiy19:06:23

in the root, nothing works

borkdude19:06:32

@raymcdermott I recommend doing it like this. A test_runner.cljs in the root which sets the classpath to src:test and then requires your test namespaces and runs them

borkdude19:06:00

Don't use src/foo.cljs or test/foo.cljs as the main invoked script

genRaiy19:06:54

how does such a test_runner set the classpath?

borkdude19:06:05

Using nbb.classpath/add-classpath

genRaiy19:06:24

I'll give it a shot

borkdude19:06:54

The JS files are loaded relative to the parent directory of the invoked script

genRaiy19:06:24

(nbb.classpath/add-classpath "src:test")

borkdude20:06:10

or call it once for each dir, works too

genRaiy20:06:25

I can't seem to get it to work

genRaiy20:06:45

I'm going to have to pick it up next week mate

genRaiy20:06:55

thanks for trying to help

borkdude20:06:07

if the project is public, I can better help :P

borkdude20:06:14

I think I could solve this in 5 minutes

borkdude20:06:04

Anyway, have a good weekend...?

genRaiy20:06:36

I'll try to make a public version over the weekend cos the signature code is public and so it wouldn't break any confidences

borkdude20:06:14

yeah, or just minimal repro repository with some silly functions and one JS file that you want to load

1
borkdude20:06:17

that would also work

genRaiy20:06:23

it's the queen's jubilee weekend so they've shut down .... so that's what I'm doing 👑

borkdude20:06:32

aaaaah 🍻

genRaiy20:06:08

🍾 🥂 👑

genRaiy20:06:47

^^^ her current state of health

borkdude21:06:21

@raymcdermott I figured it out. So:

$ find .
.
./test
./test/runner.cljs
./init.cljs
./package.json
./src
./src/dude.mjs
./src/foo.cljs

$ cat src/foo.cljs
(ns foo
  (:require ["./src/dude.mjs$default" :as dude]))

(defn doit [_]
  (prn :done))

(defn -main [& _]
  (js/console.log dude))

$ cat src/dude.mjs
export default {"hello": true};

$ cat test/runner.cljs
(ns runner
  (:require
   ["./src/dude.mjs$default" :as dude]
   [foo]))

(defn -main [_]
  (println "Testing")
  (foo/doit {})
  (prn dude))

$ nbb -cp src:test -m runner
Testing
:done
#js {:hello true}

$ nbb -cp src -m foo
{ hello: true }

borkdude21:06:59

The crux is to, when using the classpath option, do not invoke nbb -cp .. src/foo.cljs but use nbb -cp .. -m foo

borkdude21:06:32

and JS files have to be loaded via the full path relative to the working directory, since the classpath only affects loading of CLJS namespaces

borkdude21:06:25

Hope that cleared it up

borkdude07:06:16

Now that I think about it, it would perhaps be more intuitive if, when requiring js files like ./foo.js it would be relative to the script

1
borkdude07:06:21

so maybe this has to be changed

borkdude07:06:29

Made an issue for that

genRaiy08:06:21

Good hammock work !

genRaiy09:06:16

btw nbb is just for testing, the code has to be invoked via node foo.mjs

genRaiy09:06:27

so the problem is that nbb and node are trying to access the file in different way

genRaiy09:06:55

it can make it work for both cases if I cd test && npx nbb --classpath ../src -m auth-test/run-tests

genRaiy09:06:25

and leave the require as ./signature$default

genRaiy09:06:52

@U04V15CAJ I'll try to produce a smaller though representative version of what I have for your experimentation

borkdude10:06:37

Afk for a few hours, national holiday etc here

1
genRaiy10:06:05

No problem ... I have something that I can work with

genRaiy12:06:32

actually this is how I'm doing it now cd src && npx nbb --classpath ../test -m auth-test/run-tests

genRaiy12:06:31

this way works for nbb, which as you know, always includes the current directory

genRaiy12:06:18

it also works for node without any changes cos the .mjs file adds src to the classpath

borkdude14:06:02

@raymcdermott nbb 0.5.104 is now building with the fix

borkdude14:06:44

It's live now!

borkdude14:06:44

Btw, the import should be:

["./signature.js$default" :as modulr-hmac]

😬 1
borkdude14:06:52

and then your project works

borkdude14:06:36

Maybe this should be changed as well? :thinking_face:

borkdude14:06:38

In Node I can only get this working with:

import './signature.mjs';
and not without

borkdude14:06:35

Well, the only fix I applied was:

(path/resolve (path/dirname @sci/file) libname)
So I don't think I'm adding or removing any .js or .mjs extensions, just modifying the relative dir to an absolute dir

genRaiy15:06:52

so you think that me not having a .js at the end was the reason it didn't work before?

genRaiy15:06:31

I can report that the new nbb is working with this npx nbb --classpath src:test -m modulr-test/run-tests

genRaiy15:06:40

from the project directory

borkdude15:06:14

it might have been one of the causes ;)

😇 1
genRaiy18:06:05

now that I have working tests, we can get ready for production 🤞:skin-tone-3:

🎉 1