squint

rafaeldelboni 2024-09-25T16:50:30.791499Z

Hi folks I hope you all good, does anybody have an example of project using squint with a test suite?

rafaeldelboni 2024-10-18T17:47:59.367819Z

Hey sorry to necro bump this thread back, but I was playing around with tests in the react sample project and my squint.edn file looks like this:

{:paths ["src" "test"]
 :output-dir "public/js"
 :extension "jsx"}
the problem is that all my compiled files are now .jsx and my tests are being compiled public/js folder, is there any config I could do to split up tests vs src ?

rafaeldelboni 2024-10-18T18:37:15.464459Z

maybe an alternative for this would be a way to set different squint.edn files for different tasks

rafaeldelboni 2024-10-18T18:37:52.843169Z

something like: npx squint watch --config squint.test.edn

rafaeldelboni 2024-10-18T18:38:13.358439Z

or with alias, like in tools deps

borkdude 2024-10-18T18:39:41.907499Z

if you don't want jsx why do you specify it as an extension?

borkdude 2024-10-18T18:40:15.276459Z

most things are overridable via the command line I think, but let me double check

borkdude 2024-10-18T18:41:49.103349Z

--paths isn't yet overridable but we could make it so

borkdude 2024-10-18T18:41:53.445299Z

--extension is

borkdude 2024-10-18T18:42:09.358479Z

but you could also just not specify it at all in squint.edn

borkdude 2024-10-18T18:42:50.079579Z

@rafaeldelboni let's start a fresh thread where you introduce the problem at hand

rafaeldelboni 2024-10-18T18:46:04.414109Z

sure

borkdude 2024-09-25T17:33:45.553409Z

I do have a test suite but it's not a typical test suite...

borkdude 2024-09-25T17:33:55.833939Z

What platform do you use, browser or node?

rafaeldelboni 2024-09-25T17:34:33.517129Z

mainly browser

borkdude 2024-09-25T17:35:07.807149Z

I think @martinklepsch has used vitest successfully in his project (right?)

borkdude 2024-09-25T17:35:57.961839Z

else I would investigate what are the other typical things JS developers use for testing

borkdude 2024-09-25T17:37:01.249229Z

I think there's even some CLJS people who've used jest

martinklepsch 2024-09-25T17:44:21.682949Z

I explored vitest but don't think I went all the way because I wanted to use playwright and support for it was experimental still. But I think you should have pretty good success with the regular vite plugin

martinklepsch 2024-09-25T17:44:31.474259Z

which can be as simple as calling compileString

martinklepsch 2024-09-25T17:44:45.323759Z

https://github.com/squint-cljs/squint/discussions/464

borkdude 2024-09-25T17:45:28.632619Z

this is specifically a question about testing

borkdude 2024-09-25T17:45:55.764259Z

or does the regular vite plugin support testing somehow?

martinklepsch 2024-09-25T17:52:17.741809Z

I know, since it's vite it's just the same I believe

martinklepsch 2024-09-25T17:53:56.249439Z

vitest is basically an assertion lib that integrates with vite, you'll just need to do sth like this

import { expect, test } from 'vitest'
import { sum } from './sum.js'

test('adds 1 + 2 to equal 3', () => {
  expect(sum(1, 2)).toBe(3)
})

👍 1
martinklepsch 2024-09-25T17:54:40.298889Z

@rafaeldelboni are you looking for unit testing or integration testing?

rafaeldelboni 2024-09-25T17:55:45.359849Z

I do usually both, but honestly a base example with unit tests would be good enough I usually use react-testing-library for more complex integration stuff

borkdude 2024-09-25T18:06:17.747369Z

trying vitest locally, but I don't understand why it doesn't find my file:

borkdude@m1-3 /tmp/test $ ls test
foo_test.cljs foo_test.mjs
borkdude@m1-3 /tmp/test $ npx vitest

 DEV  v2.1.1 /private/tmp/test

include: **/*.{test,spec}.?(c|m)[jt]s?(x)

borkdude 2024-09-25T18:07:07.525209Z

No test files found, exiting with code 1

martinklepsch 2024-09-25T18:13:13.583029Z

i think the include needs to have .cljs - vite will handle transpilation

borkdude 2024-09-25T18:13:58.658589Z

I didn't use vite for compilation, I compiled using squint compile to .mjs.

borkdude 2024-09-25T18:14:14.165149Z

somehow the .mjs file isn't picked up

martinklepsch 2024-09-25T18:14:33.032579Z

it doesn’t have the .test.mjs suffix

borkdude 2024-09-25T18:14:42.141689Z

aah it needs a dot in front of it?

borkdude 2024-09-25T18:14:48.338469Z

right

borkdude 2024-09-25T18:14:59.851309Z

that's a pity since that's not typically what you output from CLJS

borkdude 2024-09-25T18:15:11.779599Z

so this needs configuring, ah well

martinklepsch 2024-09-25T18:15:33.106219Z

that said, you can just compile using vite as well and supply your own include regex

borkdude 2024-09-25T18:17:20.799189Z

Ah this makes it work with vitest.config.js:

import { defineConfig } from 'vite'

export default defineConfig({
  test: {
    include: ["test/*.mjs"],
  },
})

borkdude 2024-09-25T18:17:37.617959Z

(ns foo-test
  (:require ["vitest" :refer [expect test]]
            [foo :refer [adder]]))

(test "adder works"
      (fn []
        (-> (expect (adder 1 2 3))
            (.toBe 6))))
Test Files  1 passed (1)
      Tests  1 passed (1)

🆒 1
martinklepsch 2024-09-25T18:18:37.270899Z

this misses a key benefit of vitest - it doesn’t compile your cljs

borkdude 2024-09-25T18:19:55.043229Z

I know, but using squint watch and running npx vitest stuff works good enough for this little test project

borkdude 2024-09-25T18:20:02.050089Z

watching works

borkdude 2024-09-25T18:20:33.196019Z

changed the implementation to make it fail and BOOM:

martinklepsch 2024-09-25T18:25:17.928599Z

this reminds me of source maps ^^

martinklepsch 2024-09-25T18:27:51.021659Z

npx vitest will watch your cljs config if you add the plugin

export default defineConfig({
  plugins: [
    {
      name: "squint_compile",
      transform: function (src, id) {
        if (/\.cljs$/.test(id)) {
          var js = compileString(src);
          return { code: js, map: null };
        }
      },
    }
  ],
});

borkdude 2024-09-25T18:29:33.257679Z

yes, except that it won't have support for loading namespaces by their namespace name (rather than their file name) and macros. something that still needs to be addressed https://github.com/squint-cljs/vite-plugin-squint/issues/19#issuecomment-2075071784 meanwhile I'm happy to just run npx squint watch in a different window ;)

borkdude 2024-09-25T18:30:13.812459Z

but yeah, I need to take a look at this once again

martinklepsch 2024-09-25T18:32:59.969179Z

Ah yeah, I remember that now!

borkdude 2024-09-25T18:33:40.637519Z

(also pinged you in the corresponding slack thread, information got decentralized a bit.., but I think we were almost there)

2024-09-25T19:42:43.635179Z

I have a vector of hiccup coming from my server side and would like to render it as #jsx. Something like this, where zz comes from the server as json. I would like to see "hi" but am seeing "phi". Can I render the dynamic vector as react elements somehow?

(defn ViewWork []
  (let [zz [:p "hi"]]
    #jsx [:div zz ]))

borkdude 2024-09-25T19:44:41.408039Z

No you can’t do this in JSX. The hi element can be dynamic but not the p tag

borkdude 2024-09-25T19:44:55.138649Z

Unless you make it a another component

borkdude 2024-09-25T19:45:39.705069Z

Perhaps it works when you prefix the other tag with jsx as well

borkdude 2024-09-25T19:49:51.200049Z

I tested that in a JSX playground and that seems to work

2024-09-25T19:49:54.973399Z

If I try

(defn ViewWork []
  (let [j [:p "hi"]
        zz #jsx j]
    #jsx [:div [:<> zz] ]))
I get
Uncaught Error: Objects are not valid as a React child (found: object with keys {j1}). If you meant to render a collection of children, use an array instead.

borkdude 2024-09-25T19:50:18.070629Z

no, (let [j #jsx [:p "hi"] ..)

borkdude 2024-09-25T19:50:46.771469Z

(defn ViewWork []
  (let [zz #jsx [:p "hi"]]
    #jsx [:div zz ]))

2024-09-25T19:51:49.735779Z

Does that help me with rendering something dynamic from the server?

borkdude 2024-09-25T19:53:24.035759Z

not unless you dynamically compile this squint code

2024-09-25T19:54:08.522899Z

Maybe I will render on the server and do dangerouslySetInnerHTML

borkdude 2024-09-25T19:54:23.299249Z

sure you can do that

borkdude 2024-09-25T19:54:54.801419Z

since you're generating JSX, did you mean to just generate HTML perhaps? or really JSX?

borkdude 2024-09-25T19:55:03.826039Z

brb

2024-09-25T19:57:20.482539Z

I am just looking to render in my react app a document I have stored as edn on the server side. Generating HTML on the server side using the rum functionality should work for me. Thanks, @borkdude

borkdude 2024-09-25T20:17:41.441669Z

ok

borkdude 2024-09-25T20:18:10.350289Z

so the react app is already compiled to JS I assume and you inject some global value in the JS environment which you then want to render?

2024-09-25T21:04:28.595749Z

I call an api which returns a document as json which I want to render. Yes it is an already compiled app.

borkdude 2024-09-25T21:05:59.479469Z

right. JSX is a compile time feature. This is both true in squint and in React

borkdude 2024-09-25T21:06:45.421009Z

so creating the HTML on the server and using the "dangerous" feature is probably what you want

2024-09-25T21:07:08.526539Z

Gotcha. I have this working now.