At this point we assume core is using ES6, following Airbnb coding standards.
There are hundred of testing libraries. One of the most used testing tool is mocha (also used by airbnb). But it doesn't really matter, most testing libs are the same. This one has integration written for (almost?) everything we can think of using in core js.
The patch is big since it's a diff from my es6 branch. I split the test code in it's own patch for showing how it looks like. For now apply the huge patch and run the following
npm install
npm run test:core
This is the output (with code coverage):
$ npm run test:core
> Drupal@8.3.0 test:core …/drupal
> nyc mocha -r jsdom-global/register $(find core/ -name '*.test.js') || exit 0
Drupal
#checkPlain()
✓ should escape html strings safe for printing
1 passing (6ms)
---------------|----------|----------|----------|----------|----------------|
File | % Stmts | % Branch | % Funcs | % Lines |Uncovered Lines |
---------------|----------|----------|----------|----------|----------------|
All files | 19.44 | 1.54 | 11.11 | 20 | |
drupal.es6.js | 19.44 | 1.54 | 11.11 | 20 |... 573,574,588 |
---------------|----------|----------|----------|----------|----------------|
There are many things to fix, like mocking the browser env, fixing scripts so that mocking isn't needed, excluding vendor code from code coverage, a way to compile es6 files on the fly for testing, etc.
But it's a start.
Comment | File | Size | Author |
---|---|---|---|
#35 | interdiff-2815199-27-35.txt | 1.7 KB | yogeshmpawar |
#35 | 2815199-35.patch | 1.46 KB | yogeshmpawar |
| |||
#27 | 2815199-27.patch | 1.46 KB | Lendude |
#27 | interdiff-2815199-25-27.txt | 1.36 KB | Lendude |
#25 | 2815199-25.patch | 1.44 KB | Lendude |
Comments
Comment #2
dawehnerShould we mark #2702747: Add javascript unit testing as a duplicate?
Comment #3
nod_Oh right, forgot that one. Na this one has less people watching, I'll update the other one and merge the two, you already fixed some todos I have.
Comment #4
nod_reroll -all is the diff with my es6 branch, the stuff needed to do the testing is in the small patch.
Still need to get around to merging the two patches.
Comment #5
nod_Comment #8
nod_Comment #9
m1r1k CreditAttribution: m1r1k commentedHave added couple packages to work with $, ajax, drupal anonymous functions, etc.
Also another example of test.
Comment #10
m1r1k CreditAttribution: m1r1k commentedI am not sure how to properly use jsdom-global for now, may be we could erase most of the code in boostrap.js file :)
Also what do you think about splitting js files on API files and actual files with behaviors?
Drupal object itself, Drupal.ProgressBar, Drupal.debounce, Drupal.Ajax*, etc. We could something like npm package with API js files.
Comment #11
nod_woot, thanks a lot.
I don't remember if there is a dedicated issue yet but it is certainly planned to separate library code from behaviors. And more than that, make sure all the behavior object have only a attach and detach property, nothing else (otherwise it should be a dedicated library).
Comment #13
dawehnerOne part we should take into account in this issue is whether / how to deal with javascript functional testing. At least for now a lot of javascript functional test coverage is missing, and I'm wondering whether moving that to JS would help. In case we decide that we should take into account and probably use a testing framework which can handle both.
There are probably many systems out there, of which one is Nightwatch.
This raises a couple of questions:
Comment #14
GrandmaGlassesRopeMan#13
Yes.
It would possibly make it easier for people who know minor bits about PHP to write functional tests for JavaScript components.
In JavaScript.
Yes.
Possibly?, but probably only briefly.
Comment #15
michielnugter CreditAttribution: michielnugter as a volunteer and at Synetic commentedI've been thinking a bit about this and I'll just share my view on this. Do note that I'm relatively new to Drupal and testing such a system but have more experience in projects for work with a QA specialist. I have been quite active in the PHPUnit initiative in the last couple of months so I do feel that it's useful to share my view :)
So what are we actually testing?
We say and assume we're testing JavaScript with the JavaScriptTestBase tests, I'm not 100% sure on that though. Why? We're still doing functional tests, but this time in a real browser instead of a simulated one. I think that we should therefore look at this differently, don't focus on JavaScript but on implementing real-world tests, that's what we're trying to do.
The reason why we need it is still the same, a simulated browser is not enough for testing rich user interfaces that utilize browser functions like JavaScript or CSS.
The question then is how to test it
Currently we’re running tests in PHP, which to me is fine because the other functional testing (BrowserTestBase) is PHP as well. This way we have a similar environment for both types of test and it’s easy to move a BrowserTestBase to JavascriptTestBase.
To me functional testing in JavaScript doesn’t feel right (yet?), to me we'll only move the responsibility to an area where we have even less people interested in functional testing. I'm not convinced either that it's better to functional test in JavaScript and that deep JavaScript knowledge is required to do it.
To clarify and prevent any misunderstandings: I do agree on needing unit tests in Javascript and I like the direction for this in this issue :)
Another option would be to use Selenium for these kinds of test (I think this was discussed in #825436: Create selenium-RC PIFR plugin for full functional testing but rejected at the time). I'm not very familiar with Selenium but it is a much used tool for functional testing by the QA specialist. It would actually open up a whole new way of contributing to Drupal for QA specialist. Writing PHP is difficult for them but Selenium tests are well within their comfort zone. It would still cause a split in how functional tests are written though with BrowserTests.
Comment #16
dawehnerI opened up an issue about leveraging nightwatch for functional test coverage on #2869825: Leverage JS for JS testing (using nightwatch)
Comment #18
droplet CreditAttribution: droplet commentedWhat blocking this move forward?
I thought #13, #14, #15, #16 are unrelated to JS unit tests.
Comment #19
dawehner@droplet
Well, at least nightwatch would add a unit test runner as well.
Comment #20
droplet CreditAttribution: droplet commentedI'm thinking if we could make a shortcut as a temp workaround.
Most of these engines (Jest / Mocha / Nightwatch) are shared a similar syntax. There's codemods to migrate them ( https://www.npmjs.com/package/jest-codemods ). Usually, with my own or others experiences on the net, migration is pretty easy and fast.
We actually could start to use Mocha and load a HTML page in the current test env:
https://mochajs.org/#running-mocha-in-the-browser
A pure JS unit test really help for these issues: (More productive also)
#2880168: Refactor drupal.js to take advantage of new language features.
#736066: ajax.js insert command sometimes wraps content in a div, potentially producing invalid HTML and other bugs
#1988968: Drupal.ajax does not guarantee that "add new JS file to page" commands have finished before calling said JS
@dawehner,
Do you mean a parallel runner for cross-platform?
Comment #25
LendudeSo, we can now do this, maybe not yet with all sorts of fancy stuff, but the basics seem to work fine. Would it be an idea to add an example for this to core? So something like this.
Comment #26
dawehnerThe current tests in nightwatch are in the Tests folder. Given that I'm wondering whether moving unit tests to its own folder would be sensible.
Comment #27
LendudeYeah, let's move it, moved it to a subfolder, might be an idea to move the others to a 'functional' folder. Also renamed the test to be more in line with the existing ExampleTest.
Comment #28
dawehnerWe probably want to document unit tests at least in https://www.drupal.org/docs/8/testing/javascript-testing-using-nightwatch. Do you think we need a change record?
Comment #29
LendudeUpdated the docs with the example.
I don't think we want to do a release note for this, since we are not actually adding any new functionality here, but happy to be overruled on that :)
Comment #34
larowlanI think we should move forward with this
Marking as RTBC, but it likely will need a re-roll, but let's see what the bot says
I'm kind of biased, but I like the ergonomics of the testing-library suite, and specifically twig-testing-library, because we can load twig for the markup, but yeah, that's a whole load of extra dependencies.
Comment #35
yogeshmpawarUpdated patch will resolve CS issues
Comment #36
lauriiiI'm not sure if it's something we should do here or not but we already tried to use Nightwatch for unit testing CKEditor 5 code. However, that turned out quite difficult because of the lack of support for ESM: #3247647: Document how to run Nightwatch unit tests with code using ESM. I'm wondering if we should figure out how to do that before we commit to using Nightwatch for this.
Would be also great if we could update the issue summary with the proposed solution.
Comment #39
smustgrave CreditAttribution: smustgrave at Mobomo commentedFor IS update requested in #36