Skip to content

Commit f2e32ab

Browse files
jasonLasterdarkwing
authored andcommitted
Add some more mochitest docs (firefox-devtools#5319)
* Add some more mochitest docs * A few word changes
1 parent b51612b commit f2e32ab

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

assets/dictionary.txt

+3
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,6 @@ isPaused
123123
QuickOpen
124124
named-evals
125125
Greenkeeper
126+
headlessly
127+
natively
128+
outputed

docs/mochitests.md

+50-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
We use [mochitests] to do integration testing. Mochitests are part of Firefox and allow us to test the debugger literally as you would use it (as a devtools panel).
1010

11+
Mochitests are different from Jest, Mocha, Selenium, because we have access to firefox internals and run inside the browser toolbox. This makes it possible to write tests as if we're a user interacting with the debugger natively :)
12+
1113
![](http://g.recordit.co/dp6qbK0Jnf.gif)
1214

1315
### Getting Started
@@ -17,7 +19,6 @@ We use [mochitests] to do integration testing. Mochitests are part of Firefox an
1719
* mercurial ( `brew install mercurial` )
1820
* autoconf213 ( `brew install [email protected] && brew unlink autoconf` )
1921

20-
2122
**Setup Firefox**
2223

2324
* Inside the main debugger.html folder run:
@@ -39,8 +40,9 @@ reflected in the new firefox directory.
3940

4041
`mochi` passes its params along to `mochitest`, so you can include `--jsdebugger` and test globs
4142

42-
* `yarn mochi -- --jsdebugger` opens a browser toolbox
43-
* `yarn mochi browser_dbg-editor-highlight` runs just one test
43+
* `yarn mochi dbg-editor-highlight` runs just one test
44+
* `yarn mochid dbg-editor-highlight` opens a browser toolbox
45+
* `yarn mochih dbg-editor-highlight` runs the test headlessly
4446

4547
## Writing Tests
4648

@@ -50,17 +52,60 @@ Here are a few tips for writing mochitests:
5052
* Try to write async user actions that involve a user action like clicking a button or typing a key press followed by a redux action to listen for. For example, the user step in action involves the user clicking the step in button followed by the "stepIn" action firing.
5153
* The `dbg` object has several helpful properties (actions, selectors, getState, store, toolbox, win)
5254

55+
### Logging
56+
57+
The mochitests run in a special environment, which make `console.log` a little different than usual.
58+
`console.log` inside the test will print to the terminal. `console.log` in the debugger source, will be redirected to the browser console and will not be outputed. This is why we recommend using the special firefox `dump` call which is available everywhere.
59+
60+
If you want a convenience method for logging in the test, `log` is a bit cleaner than `dump`.
61+
62+
```js
63+
console.log(">>> YO")
64+
log("FOO", { t: 3 })
65+
dump(">> FOOO\n");
66+
```
67+
68+
69+
### Pausing the test
70+
71+
There are two ways to pause the tests and see what is going on.
72+
73+
The first is to add a `debugger` statement to the test and run `yarn mochid {test_name}` (ex: `dbg-sources`). Here you'll have to click a modal when the test opens. When the test pauses, the browser toolbox will show your test with the `dbg` object you can interact with.
74+
75+
The other way is to add `await waitForever()` to your test. This stops the test and gives you a chance to interact with the debugger as the user would. Both ways of pausing are useful for different use cases!
76+
77+
### Waiting in a test
78+
79+
It's really common to want to wait for something to happen in a test. Generally we wait for one of two things to happen:
80+
81+
* waiting for the Redux state to change
82+
* waiting for an action to be dispatched
83+
84+
```js
85+
await waitForState(dbg, state => isPaused(state));
86+
await waitForDispatch(dbg, "STEP_OVER";)
87+
```
88+
5389
### Testing the DOM
5490

5591
You can find common elements in the debugger with the `findElement` function,
5692
which use shared selectors. You can also find any element with the
5793
`findElementWithSelector` function.
5894

95+
```js
96+
findElement(dbg, "sourceNode", 3);
97+
findElementWithSelector(dbg, ".sources-list .focused");
98+
```
99+
59100
### Evaluating in the debuggee
60101

61102
If you want to evaluate a function in the debuggee context you can use
62103
the `invokeInTab` function. Under the hood it is using `ContentTask.spawn`.
63104

105+
```js
106+
invokeInTab(dbg, "doSomething")
107+
```
108+
64109
```js
65110
ContentTask.spawn(gBrowser.selectedBrowser, null, function* () {
66111
content.wrappedJSObject.foo();
@@ -94,9 +139,8 @@ We recommend prefixing your logs and formatting them so they are easy to scan e.
94139
* `info(">> Current breakpoints ${breakpoints.map(bp => bp.location.line).join(", ")}\n")`
95140
* `info(">> Symbols for source ${source.url} ${JSON.stringify(symbols)}\n")`
96141

97-
At some point, it can be nice to pause the test and debug it. We are working on a debugger after all :)
98-
Mochitest, makes it easy to pause the test at `debugger` statements with the `--jsdebugger` flag.
99-
You can run the test like this `yarn mochid browser_dbg-editor-highlight`.
142+
At some point, it can be nice to pause the test and debug it. Mochitest makes it easy to pause the test at `debugger` statements with the `--jsdebugger` flag.
143+
You can run the test with `yarn mochid {test_name}` (ex: `browser_dbg-editor-highlight`).
100144

101145
![](https://shipusercontent.com/e8441c77ab9ff6e84e5561b05bc25da2/Screen%20Shot%202017-10-26%20at%205.45.05%20PM.png)
102146
![](https://shipusercontent.com/57e41ae7227a46b2b6ae8b66956729ea/Screen%20Shot%202017-10-26%20at%205.44.54%20PM.png)

0 commit comments

Comments
 (0)