closure-library icon indicating copy to clipboard operation
closure-library copied to clipboard

jsunit test report not showing tests

Open myphysicslab opened this issue 7 years ago • 4 comments

After upgrading to latest Closure Library and Closure Compiler, my unit tests are no longer printing the test results when using SIMPLE compile. When using ADVANCED compile, I still see the results.

Here is a fragment of the page showing results which I get with ADVANCED compile:

/Users/erikn/Documents/Programming/myphysicslab/adv-build/test/UnitTest-en.html
105 of 105 tests run in 1797.5000000000002ms.
105 passed, 0 failed.
17 ms/test. 1 files loaded.
.
13:43:20.231  Start
13:43:20.232  testAbstractSubject1 : PASSED (run individually)
13:43:20.232  testAffineTransform : PASSED (run individually)
13:43:20.233  testCircularEdge1 : PASSED (run individually)
13:43:20.233  testClock1 : PASSED (run individually)
13:43:20.234  testClock2 : PASSED (run individually)
13:43:20.235  testClock3 : PASSED (run individually)
13:43:20.235  testConcreteLine : PASSED (run individually)
13:43:20.235  testConcreteLineSimilar : PASSED (run individually)
13:43:20.235  testConcreteMemoList1 : PASSED (run individually)
13:43:20.236  testCoordMap : PASSED (run individually)
... many more tests ...
13:43:22.028  testVectorMath : PASSED (run individually)
13:43:22.028  testVectorSimilar : PASSED (run individually)
13:43:22.028  testVertex1 : PASSED (run individually)
13:43:22.028  Done

Run again without reloading

But with SIMPLE compile all I get is this:

/Users/erikn/Documents/Programming/myphysicslab/build/test/UnitTest-en.html
107 of 107 tests run in 1948.5ms.
107 passed, 0 failed.
18 ms/test. 1 files loaded.
.
13:46:08.846  Start

I no longer see each individual test being reported. And if there is an error I don't see anything except that a test failed -- no idea which one or why.

I'm using goog.testing.jsunit. I define functions that start with the name "test" and they get run when I do goog.require().

Here is a typical test:

goog.provide('myphysicslab.lab.util.test.Util_test');

goog.require('myphysicslab.lab.util.Util');
goog.require('goog.testing.jsunit');
goog.require('goog.asserts');

var testUtil = function() {
  var Util = myphysicslab.lab.util.Util;
  var NF5 = myphysicslab.lab.util.Util.NF5;
  var nf5 = myphysicslab.lab.util.Util.nf5;
  var nf7 = myphysicslab.lab.util.Util.nf7;

  // the default tolerance is 1E-14, so large and small numbers should differ
  // at about the 14th decimal place
  assertTrue(Util.veryDifferent(2E-14, 3.1E-14));
  assertFalse(Util.veryDifferent(2E-14, 2.9E-14));
  assertTrue( Util.veryDifferent(1.12345678901234E5, 1.12345678901236E5));
  assertFalse(Util.veryDifferent(1.12345678901234E5, 1.123456789012349E5));
  // more....
};
goog.exportProperty(window, 'testUtil', testUtil);

Here is the bash script that does the compiling: compile_test.sh. It produces a file typically named UnitTest-en.js (for the English version).

The tests are run by opening the file UnitTest.html. (Note that file uses some "macro" commands that are replaced during the build process so it's not standard HTML). For the compiled version, that basically just executes the script produced by compile_test.sh like this:

<script src="UnitTest-en.js"></script>

myphysicslab avatar Jan 11 '18 19:01 myphysicslab

I've figured out the problem, it's on this line of goog.html.SafeUrl.sanitizeAssertUnchanged

if (!goog.asserts.assert(goog.html.SAFE_URL_PATTERN_.test(url))) {
  url = goog.html.SafeUrl.INNOCUOUS_STRING;
}

If I change it to the following, then I am able to see the test results.

if (!goog.html.SAFE_URL_PATTERN_.test(url)) {
  url = goog.html.SafeUrl.INNOCUOUS_STRING;
}

This assertion is failing (under SIMPLE compile) when I run by opening the file on my local machine. If I upload to my website, then this doesn't fail. This is because goog.html.SAFE_URL_PATTERN_ doesn't allow a file:// prefix on the url. I suspect that with ADVANCED compile I have goog.DEBUG turned off which disables the assertion.

The code doesn't make sense. If the assertion fails, then we can never make it to the next line with goog.html.SafeUrl.INNOCUOUS_STRING. Why not just have the plain assertion? Why have an if statement that depends on an assertion failing?

As written, I can't see the test results when running the test on my local machine.

The goog.html.SafeUrl.sanitizeAssertUnchanged changes happened in commit 3689198649041f33b340460d39d19a00eb270409

myphysicslab avatar Jan 11 '18 23:01 myphysicslab

This makes sense to me. The idea behind assertions is that they're enabled in debug, disabled in production. So the reason this fails in a test is because you're debugging. The assert should be doing its job, throwing an exception, and letting you know in the console.

@shicks @concavelenz are tests meant to be run locally with file://? Or are you required to start up an HTTP server locally and serve the tests?

jplaisted avatar Feb 03 '18 00:02 jplaisted

I had to set goog.DEBUG=true because in some unit tests I was using goog.testing.MockClock uses goog.async.run which requires goog.DEBUG to be true. Perhaps that has changed, but there is still one at least one place that requires goog.DEBUG.

So, I would have to stop using MockClock in order to get this to work.

(I still think that code doesn't make sense for the reasons I outlined above... an assertion should just cause a failure, why would you ever test the result of an assert?)

I've stopped using goog.testing.jsunit because of this and other issues, so it's no longer a problem in my code.

myphysicslab avatar Feb 05 '18 19:02 myphysicslab

Hello, is there.any update on this issue? I'm maintaining a local patch to the Closure Library to work around this issue since 3 years, but would be happy if this can be fixed upstream here.

Is the assertion really useful for the test code? (as mentioned in #872, it's failing in goog.testing.TestRunner.prototype.writeLog() - at the place where it calls into goog.dom.safe.setAnchorHref().)

emaxx-google avatar Jan 25 '22 06:01 emaxx-google