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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nod_ created an issue. See original summary.

dawehner’s picture

Should we mark #2702747: Add javascript unit testing as a duplicate?

nod_’s picture

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.

nod_’s picture

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.

nod_’s picture

The last submitted patch, 4: core-es6-testing-2815199-4.patch, failed testing.

Status: Needs review » Needs work

The last submitted patch, 5: core-es6-testing-2815199-4.patch, failed testing.

nod_’s picture

Status: Needs work » Needs review
m1r1k’s picture

Have added couple packages to work with $, ajax, drupal anonymous functions, etc.
Also another example of test.

m1r1k’s picture

I 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.

nod_’s picture

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).

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

dawehner’s picture

One 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:

  • Do we want the functional JS tests in JS or in PHP?
  • Would be attract more JS people by not force them to write PHP?
  • Would it be a disadvantage to have two systems to write tests in when you want to contribute to Drupal on both PHP and JS?
GrandmaGlassesRopeMan’s picture

#13

One part we should take into account in this issue is whether / how to deal with javascript functional testing.

Yes.

At least for now a lot of javascript functional test coverage is missing, and I'm wondering whether moving that to JS would help.

It would possibly make it easier for people who know minor bits about PHP to write functional tests for JavaScript components.

Do we want the functional JS tests in JS or in PHP?

In JavaScript.

Would be attract more JS people by not force them to write PHP?

Yes.

Would it be a disadvantage to have two systems to write tests in when you want to contribute to Drupal on both PHP and JS?

Possibly?, but probably only briefly.

michielnugter’s picture

I'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.

dawehner’s picture

I opened up an issue about leveraging nightwatch for functional test coverage on #2869825: Leverage JS for JS testing (using nightwatch)

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

droplet’s picture

What blocking this move forward?

I thought #13, #14, #15, #16 are unrelated to JS unit tests.

dawehner’s picture

@droplet
Well, at least nightwatch would add a unit test runner as well.

droplet’s picture

I'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?

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Lendude’s picture

So, 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.

dawehner’s picture

+++ b/core/tests/Drupal/Nightwatch/Scripts/unitTestScript.js
index 0000000000..6dff4a4f13
--- /dev/null

--- /dev/null
+++ b/core/tests/Drupal/Nightwatch/Tests/testUnitTest.js

+++ b/core/tests/Drupal/Nightwatch/Tests/testUnitTest.js
+++ b/core/tests/Drupal/Nightwatch/Tests/testUnitTest.js
@@ -0,0 +1,21 @@

@@ -0,0 +1,21 @@
+const assert = require('assert');
+const testScript = require('../Scripts/unitTestScript');
+

The 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.

Lendude’s picture

Yeah, 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.

dawehner’s picture

We 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?

Lendude’s picture

Updated 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 :)

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

larowlan’s picture

Status: Needs review » Reviewed & tested by the community
Related issues: +#2760937: contextual.js produces an undesireable URL when a contextual link has a fragment in it

I 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.

yogeshmpawar’s picture

Updated patch will resolve CS issues

lauriii’s picture

Status: Reviewed & tested by the community » Needs review
Issue tags: +Needs issue summary update
Related issues: +#3247647: Document how to run Nightwatch unit tests with code using ESM

I'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.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs Review Queue Initiative

For IS update requested in #36

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.