Commit Graph

347 Commits

Author SHA1 Message Date
Anders Kaseorg 923d67ecba node_tests: Fix example jQuery mock in comment.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-23 11:16:42 -07:00
Anders Kaseorg d8a36d0702 i18n: Remove i18next.
Fixes #17890.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-13 17:41:10 -07:00
Anders Kaseorg 4b4cea90a3 i18n: Initialize FormatJS.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-04-13 17:41:10 -07:00
Steve Howell 902e0f7e95 node tests: Clear page_params at start of run_test. 2021-04-03 15:03:00 -04:00
Anders Kaseorg 38ffd47b90 js: Convert static/js/page_params.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-26 10:17:56 -07:00
Anders Kaseorg bb1b2048bd js: Convert static/js/i18n.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-26 10:17:56 -07:00
Anders Kaseorg 9553f11387 eslint: Forbid CommonJS variables in ES6 modules.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-23 01:42:43 -07:00
Anders Kaseorg 3ef6f6e2e2 js: Convert static/js/blueslip.js to ES6 module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-17 08:47:15 -04:00
Steve Howell 198c67a9bd node tests: Split up tutorial into smaller examples.
I didn't bother to keep the very last test from
tutorial, which has some complications related
to clearing stream_list data and was kind of
overkill.
2021-03-15 10:46:00 -04:00
Anders Kaseorg 9275489134 zjsunit: Truncate the stack trace only at run_one_module.
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>
2021-03-13 14:44:13 -05:00
Steve Howell fbd3669461 zjsunit: Do not run $(...) automatically.
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.
2021-03-13 11:48:50 -05:00
Steve Howell e5669f9087 zjsunit: Extract complain_about_unused_mocks.
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.
2021-03-13 08:59:06 -05:00
Anders Kaseorg 37bd04054f zjsunit: Try to document the difference between CJS and ES modules.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-12 11:18:13 -08:00
Anders Kaseorg ea9ca6b7d0 js: Use jQuery as a module.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-12 10:08:25 -08:00
Anders Kaseorg 5251fa81fb zjsunit: Lift restriction against mocking CJS modules.
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>
2021-03-12 10:06:34 -08:00
Anders Kaseorg 876806eb4d zjsunit: Lift restriction against mocking third party modules.
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>
2021-03-12 10:06:30 -08:00
Steve Howell d74266ac75 zjsunit: Simplify zjquery patching.
This makes it so that we can bypass the Proxy
object in zjquery with less code in namespace.js.
2021-03-11 13:00:30 -05:00
Anders Kaseorg 59e5e9ba35 zjsunit: Move mock clearing to namespace.finish.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg 1b58f5a700 zjsunit: Remove objs_installed flag.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg 1689c57cc4 zjsunit: Fix filename resolution in _load hook.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg f4d38bc39f zjsunit: Add unmock_module function.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg e18171e1c5 zjsunit: Rename mocked_paths to used_module_mocks.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg d40bbde51c zjsunit: Remove redundant mock_names set.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg 3551cc5679 zjsunit: Convert mock_paths object to a Map.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Anders Kaseorg 7218a06cab zjsunit: Install Module._load hook in namespace.start.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-10 07:07:34 -05:00
Steve Howell 8ab5475b37 zjsunit: Allow multiple overrides again.
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.
2021-03-07 11:19:33 -05:00
Steve Howell f54f7cfc33 node tests: Introduce mock_module helper. 2021-03-06 13:15:04 -05:00
Steve Howell 167fda142c node tests: Remove __esModule cruft.
We just set __esModule in our rewiremock helper.
2021-03-06 12:36:07 -05:00
Steve Howell 30c7108955 zjsunit: Remove rewiremock dependency.
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.
2021-03-06 11:10:57 -05:00
Anders Kaseorg cce3257f2c zjsunit: Remove babel-plugin-lazy-require.
There are no more require() calls in tested modules.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-03-02 17:06:35 -08:00
Anders Kaseorg 4df3fef88f zjsunit: Add babel-plugin-lazy-require.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg b93e00f213 zjsunit: Enable Babel lazy imports.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg aee94f141d zjsunit: Remove _patched_with_set_global check.
Most of our set_global usage will disappear as our globals do, anyway.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg db50451a5a zjsunit: Use __Rewire__ for ES modules in with_field.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 8985b5ffc7 zjsunit: Use __Rewire__ for ES modules in override.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-28 14:23:00 -08:00
Anders Kaseorg 54c97c4457 node_tests: Consistently move set_global mocks before zrequire calls.
This way, as we convert them to rewiremock, they will become available
before they are imported.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-26 07:55:53 -08:00
Steve Howell 5437a16602 zjquery: Remove bogus remove(). 2021-02-25 13:42:47 -05:00
Steve Howell ac69450681 zjsunit: Clear $ elements in run_test.
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
    });
2021-02-23 07:55:43 -05:00
Anders Kaseorg 3ccb4391d8 zjsunit: Restore missing fields correctly in with_field.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-22 20:21:41 -08:00
Anders Kaseorg 58b18fd499 zjsunit: Handle exceptions in with_field, with_overrides.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2021-02-22 20:21:41 -08:00
Anders Kaseorg 0ca543396a zjsunit: Use clear_zulip_refs as the main way to undo zrequire.
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>
2021-02-22 10:53:49 -08:00
Steve Howell ff3175f8a2 lightbox: Export method for testing purposes.
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.
2021-02-22 12:38:07 -05:00
Steve Howell 42c2c9fb2d zjquery: Avoid array-related hacks.
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.
2021-02-22 12:38:07 -05:00
Aman Agrawal 2b8921bf67 zjquery: Tell user why using tags as selector is invalid.
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.
2021-02-22 10:24:59 -05:00
Aman Agrawal 269457c58b zjquery: Extract func to verify selector. 2021-02-22 10:24:59 -05:00
Steve Howell 914533c8bc zjsunit: Prevent common mistakes with overrides.
We only allow you to replace functions with functions,
unless you have used set_global() or are dealing
with $ from zjquery.
2021-02-22 10:04:29 -05:00
Steve Howell 5caee967d3 zjquery: Remove bogus after/before functions. 2021-02-22 10:04:29 -05:00
Steve Howell 132e67cb28 zjquery: Prohibit extensions to $.fn.
We make an exception for the popovers code.

Luckily it's pretty rare that we extend $.fn
in our real code.
2021-02-22 10:04:29 -05:00
Steve Howell 89ab76920f zjquery: Prevent direct patches to $.
We prohibit code from making direct patches to
$ unless you use override().
2021-02-22 10:04:29 -05:00
Steve Howell b40d8d5790 node tests: Inline setup for settings_bots.
I also use override in more places, and I add
$.validator to zjquery.

In passing I clean up some IIFEs that aren't
really needed.
2021-02-22 10:04:29 -05:00