Skip to content

Commit

Permalink
Module mocking examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sgravrock committed Sep 2, 2023
1 parent fc34f2e commit 4ef412d
Show file tree
Hide file tree
Showing 47 changed files with 8,406 additions and 0 deletions.
38 changes: 38 additions & 0 deletions examples/module-mocking/browser-import-maps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
This example shows how to use import maps to mock modules in a web browser with
jasmine-browser-runner.

## Requirements
* The web browser must support ES module import maps.
* You are loading ES modules directly, not bundling them or transpiling to
something else.

## Building and running

```shell
npm install
npm test
```

`npm test` assumes that Firefox and a matching version of GeckoDriver are
installed. Alternately, run `npm run serve` and visit http://localhost:8888
in your browser of choice.

## Caveats

Module mocking with this approach is global: you can't use the real module in
some places and a mock in others. Unlike Node.js, browsers don't provide the
kind of configurable loaders that are needed to implement conditional module
mocking.

It's very important to replace all of the spies after each spec. Otherwise,
their state will leak from one spec to the next, causing specs to affect each
other's behavior. In this example, cleanup is done by the `afterEach(reset)`
call at the end of `mockModules/anOtherMod.mjs`.

## Tested with

* Firefox 114
* Safari 16.5 (will not work with versions before 16.3)
* Chrome 114
* jasmine-browser-runner 2.1.0
* jasmine-core 5.1.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export let theString = jasmine.createSpy('theString');

// IMPORTANT:
// Reset after each spec to prevent spy state from leaking to the next spec
afterEach(function() {
theString = jasmine.createSpy('theString');
});
Loading

0 comments on commit 4ef412d

Please sign in to comment.