AngelMunoz / Perla

A cross-platform tool for unbundled front-end development that doesn't depend on Node or requires you to install a complex toolchain
https://perla-docs.web.app/
MIT License
132 stars 11 forks source link

Add Test command #75

Open AngelMunoz opened 2 years ago

AngelMunoz commented 2 years ago

Is your feature request related to a problem? Please describe. One thing perla doesn't have right now is a way to create/run tests, normally this would be complicated and messy but with the help of web test runner we can re-use those libraries to enable in-browser tests, which are the best kind since our code and tests would be running in the same environment it is going to run.

Describe the solution you'd like As usual, the Perla CLI has to be quite simple and should be convention based

perla test
perla test --files ./tests/file-a.test.js ./tests/feature/file-b.test.js
perla test --groups featureA featureB
perla test --browsers edge chrome firefox

Arguments I have considered right now are:

Describe alternatives you've considered I have not seen any alternatives for web test runner servers in F#/.NET

Additional context I already have a PoC in a project called Nacre as a standalone project I think it could also take off, not sure if it's worth getting this inside perla (as we did with the scaffolding features: Clam -> Perla)

Technical quirks Inside Nacre we had to use import maps to mock the file that calls into the test runner's script that handles socket connections to the server, we can use/re-use the Http/SSE mechanisms we're using inside Perla instead of sockets.

Inside Nacre we had to use import maps to mock the file that...

Following this point, we could also use import maps to let the user mock any other files and dependencies, this requires some way to specify in the mocks (either with code or a configuration property)

Considerations

Disadvantages

Non Goals

AngelMunoz commented 2 years ago

I wouldn't mind some thoughts if you're able to check this out @nojaf, @kaeedo 🙏🏼

nojaf commented 2 years ago

Hello, it has been a while since I've done anything with unit tests in JavaScript. I've never worked with @web/test-runner, so I can't really say anything clever. If you use @web/test-runner can you combine this with any test framework of the day? Is Jest still cool these days?

I have been thinking about checking out PlayWright and just staying in .NET land to run some UI tests. But that doesn't really give an answer to run plain JS unit tests I guess.

AngelMunoz commented 2 years ago

The gist of @web/test-runner is basically setting up some basic infrastructure to run tests in an environment browser the npm package brings more to the table as how to run playwright and other few set of settings you can configure for it.

In our case, using @web/test-runner + playwright just makes it easier to add tests since we don't have to write any code for setting up tests, teardown, run them and such We can still use things like mocha/jasmine assuming they provide ways to integrate with it. The advantage of this approach is that your tests either UI or Unit ones run in the browser in the environment they are going to run on production as well

Is Jest still cool these days?

No Idea haha, I think Jest is node only and runs tests in node rather than in the browser

I have been thinking about checking out PlayWright and just staying in .NET land to run some UI tests. But that doesn't really give an answer to run plain JS unit tests I guess.

That's actually what the PoC does https://github.com/AngelMunoz/Nacre it simply runs in headless mode, For fable users tests could look like this https://github.com/fable-compiler/Fable.Lit/blob/main/test/LitElementTest.fs#L91 and at that point one would be able to also code unit tests besides UI tests

kaeedo commented 2 years ago

Is Jest still cool these days

I believe Jest can run tests in either node or the browser, but the bigger problem is that it doesn't support ES modules at all. So another tool is required to transpile es modules down to commonJS modules.

Other than that, Fable.Lit uses @web/test-runner for its testing. https://fable.io/Fable.Lit/docs/testing.html. This could be a good place to get a feel for "modern" fable testing

@web/test-runner also relies on the same import-map feature that Perla does, so I think it would be nice since both runtime and testing (test-time?) use the same way of importing dependencies. Additionally, it runs tests inside a headless browser instead of node, which fits with perla's usage without needing node installed at all

DejanMilicic commented 1 year ago

I am attempting to customize the existing Fable.Mocha tests to run with Perla. What I have is a Fake script with "test" target. The part I am missing here is ability to point Perla to serve files from a specific folder and for this to be available as a command line parameter.

So, something like perla serve -source ... would do the job. Unfortunately, if I am not wrong, the only way to specify source folder for file is via devServer section of perla.jsonc

Is there any way to circumvent this? Ability to pass source folder via command line would do.

AngelMunoz commented 1 year ago

I am currently working on this in the #89 branch the current perla implementation isn't flexible enough to implement a test runner

The PoC is done there but there are a few missing pieces

AngelMunoz commented 1 year ago

I tried to get preview builds a couple of weeks ago, but I never got to it.

No promises but maybe in a couple of weeks I can finally get it in a well enough state to start making a few preview releases for vnext