Fork me on GitHub
#re-frame
<
2021-07-22
>
Dan Boitnott14:07:59

I'm having a bit of trouble understanding unit tests in re-frame. I've found docs on how to write tests for certain things but not on the basic process of running those tests. I get the impression that there's functionality in the shadow-cljs/re-frame/karma stack to run unit tests automatically on save. Is this correct or am I just looking for something that's not there?

p-himik14:07:10

Shadow-cljs definitely does have that functionality.

Dan Boitnott14:07:50

Is that delivered by the :browser-test build target?

Dan Boitnott15:07:59

Ah, digging in some source on github I'm noticing that build wasn't in the shadow-cljs.edn set up by the template. So I'm guessing if I get that put in (as in https://github.com/day8/re-frame-test/blob/master/project.clj) I'll be able to deftest, save, and see a unit test report appear in the browser?

p-himik15:07:23

Right, but note that it will be a separate shadow-cljs process. Better read shadow-cljs documentation on the topic.

Dan Boitnott15:07:11

Ah, ok. I'll take a look. Thanks!

👍 2
Dan Boitnott15:07:27

Success! Thanks! If you'll permit one more dumb question, where does Karma fit in?

p-himik15:07:57

Sorry, no clue - never used it.

Dan Boitnott15:07:09

Fair enough. Thanks again!

dntn16:07:45

It has been a while since I've done this but I think the browser-test target will provide a nice browser window showing tests success and re-running them as you make changes to code -- providing a nice TDD workflow. If I remember right, the karma stuff is how you would want to run tests for CI/CD and is configured a little differently. I could have done things in a non-standard way and this entire conversation might be more appropriate in #shadow-cljs but here is a relevant bit from one of my project's shadow-cljs.edn

:browser-test {:target :browser-test
                         :test-dir "out"
                         :ns-regexp "-test$"
                         :devtools {:http-port 8606
                                    :http-root "out"}}
          :test {:target :karma
                 :output-to  "out/karma-test.js"
                 :ns-regexp  "-test$"}
and from my package.json within the scripts section
"test": "shadow-cljs compile test && karma start --single-run",
    "browser-test": "shadow-cljs watch browser-test" 
and lastly my karma.conf.js
module.exports = function(config) {
  config.set({
    // The directory where the output file lives
    basePath: 'out',
    browsers: ['ChromeHeadlessNoSandbox'],
    client: {
      args: ['shadow.test.karma.init'],
      singleRun: true
    },
    colors: true,
    customLaunchers: {
      ChromeHeadlessNoSandbox: {
        base: 'ChromeHeadless',
        flags: ['--no-sandbox']
      }
    },
    // The file itself
    files: ['karma-test.js'],
    frameworks: ['cljs-test'],
    logLevel: config.LOG_INFO,
    plugins: ['karma-chrome-launcher','karma-cljs-test', 'karma-spec-reporter'],
    reporters: ['spec'],
    specReporter: {
      maxLogLines: 5,
      showSpecTiming: true
    }
  });
};