There is no need for mock_template to reach into the internals of
mock_cjs. Make it a normal caller of mock_cjs.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
We use css() pretty rarely in our codebase, and
it can sometimes be used mistakenly, when a better
alternative is to toggle a class for external css.
It's hard to support the full API in zjquery, so
we just punt and tell folks to create their own
stubs.
Most of the existing tests that were "fixed" here
weren't actually verifying the behavior of the css()
calls, and for those I just create no-op stubs.
In a few places I verify that css() was called as
expected.
Commit 5bd73ce190 (#17367)
unintentionally truncated more of the traceback rather than less when
there’s more than one Module._compile frame.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
We have generally gone away from using $(...)
initialization in modules that we test with
zjsunit, but there are a few remaining special
cases related to our billing and portico
codebases.
This is prep toward allowing individual tests
to fail while continuing to run the test suite.
Most of the steps in namespace.finish() could
be put in a `finally` block, but once a test
fails, there's no reason to complain about
unused mocks, since there are bigger things
to address.
Factor out mock_cjs from mock_esm because adding __esModule prevents
mocks for CJS modules from being imported correctly.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
Use fully resolvable request paths because we need to be able to refer
to third party modules, and to increase uniformity and explicitness.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
I now let folks override the same target multiple
times inside of `with_overrides` (and then indirectly
inside of `run_test`.)
I may re-impose this restriction in the future, since
most violations are due to code smells, but there are
a few legitimate use cases for this, and the code
can handle it, plus I want to remove some other
ugliness first.
We now just use a module._load hook to inject
stubs into our code.
For conversion purposes I temporarily maintain
the API of rewiremock, apart from the enable/disable
pieces, but I will make a better wrapper in an
upcoming commit.
We can detect when rewiremock is called after
zrequire now, and I fix all the violations in
this commit, mostly by using override.
We can also detect when a mock is needlessly
created, and I fix all the violations in this
commit.
The one minor nuisance that this commit introduces
is that you can only stub out modules in the Zulip
source tree, which is now static/js. This should
not really be a problem--there are usually better
techniques to deal with third party depenencies.
In the prior commit I show a typical workaround,
which is to create a one-line wrapper in your
test code. It's often the case that you can simply
use override(), as well.
In passing I kill off `reset_modules`, and I
eliminated the second argument to zrequire,
which dates back to pre-es6 days.
We now call $.clear_all_elements at the top
of run_test.
We have to exempt two modules from the new regime:
compose
settings_user_groups
Also, if modules do set_global("$", ...) we don't
try to call the non-existent function.
It's possible we'll want to move to something like
this, but we might want to clean up the two
sloppy_$ modules first:
// AVOID THIS:
// const $ = require("zjquery")
run_test("test widget", ({override, $}) => {
override(foo, "bar", ...);
$.create(...);
// do stuff
});
Move clear_zulip_refs into restore, and rewrite it without lodash. We
no longer need the requires array, and zrequire is now nothing more
than a wrapper around require.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
We weren't exercising this method in any
meaningful way during the tests, and when
do add coverage, we probably want to just
test it directly.
We also kill off stub_selector(), which was
never well-documented.
Callers can either explicitly pass in children,
stub out $(...)[0] as needed, or just
circumvent jQuery complications with override.
Note the reactions test was broken before,
since $(...)[0] was always returning the same
stub.
Like using $('input') is too broad a selector and shouldn't
be used in the codebase. With this error messages, contributors
can easily understand that now.
We no longer export make_zjquery().
We now instead have a singleton zjquery instance
that we attach to global.$ in index.js.
We call $.clear_all_elements() before each module.
(We will soon get even more aggressive about doing
it in run_test.)
Test functions can still override $ with set_global.
A good example of this is copy_and_paste using the
real jquery module.
We no longer exempt $ as a global variable, so
test modules that use the zjquery $ need to do:
const $ = require("../zjsunit/zjquery");
The "silent" option was kind of evil, as it had
$(...).find(...) passing back "self" instead of a stub.
Now we just use $(...).set_find_results(...) or
override(...) to simulate/bypass drawing code.
(It turns out hash_util didn't even need this option.)
We can relax this restriction in the future, but
basically every time it came up for me, the test
code was just disorganized, or it had an easy
workaround.
We still need to write to these globals with set_global because the
code being tested reads from them, but the tests themselves should
never need to read from them.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
It's actually pretty rare in our codebase to
call methods like `$(...).map` or `$(...).each`,
but we now support them better in zjquery.
You can pass a list of child elements now to
`$.create(...)`.
Now when we want to measure how long a block
of code takes to execute, we just wrap it with
`blueslip.measure_time`, instead of the awkward
idiom from my original commit of getting a callback
function.
My rationale for the original scheme was that I
wanted to minimize diffs and avoid changing
`const` to `let` in a few cases, but I believe
now that the function wrapper is nicer.
In a few cases I just removed the blueslip timing
code, since I was able to confirm on czo that
the times were pretty minimal.
Instead of prohibiting ‘return undefined’ (#8669), we require that a
function must return an explicit value always or never. This prevents
you from forgetting to return a value in some cases. It will also be
important for TypeScript, which distinguishes between undefined and
void.
Signed-off-by: Anders Kaseorg <anders@zulip.com>