You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit d8f8ba5
Browse filesBrowse the repository at this point in the historyBrowse files
Mocking of class and module dependencies of your unit under test is strongly encouraged. In some cases it is mandatory.
113
+
114
+
Some functionality, such as the ability to mock strings and AMD modules, is provided as part of the Moodle core and these mocks are reset between tests.
115
+
116
+
You should not need to clean mocks up manually. Each test starts with a fresh state.
117
+
118
+
You can clear up any additional test state within your test file using:
119
+
120
+
-`beforeEach()`
121
+
-`afterEach()`
122
+
123
+
See the [Jest Setup and Teardown](https://jestjs.io/docs/setup-teardown) documentation for further information.
124
+
125
+
### Mocking strings
126
+
127
+
Because of the way in which Moodle's string module fetches strings using a web service, all strings are mocked to a standard value of:
128
+
129
+
```
130
+
[identifier, component]
131
+
```
132
+
133
+
For cases where your tests expect a specific string value, you can mock values using the global `mockString` method:
AMD modules (anything loaded via `requirejs`) **cannot** run inside Jest. The Jest module system and the AMD loader are completely separate environments, so `requirejs`, `M`, jQuery, and other Moodle globals are not available.
If code under test calls `requireAsync` or `requireManyAsync` with a module that has not been registered via`mockAmdModule`, the test will throw:
175
+
If code under test calls `requireAsync` or `requireManyAsync` with a module that has not been registered using`mockAmdModule` then the test will throw an error:
136
176
137
177
```
138
178
Error: Unexpected call to requireAsync with module name: core/notification
@@ -142,37 +182,40 @@ This is intentional: missing mocks produce a hard failure rather than silent wro
142
182
143
183
:::
144
184
145
-
### Registrations reset between tests
185
+
##Handling redirects
146
186
147
-
Mocks and test fixtures, such as the AMD module map, and the string map, are cleared between each test using the `afterEach` notation.
148
-
149
-
You do not need to clean up manually. Each test starts with a fresh state.
150
-
151
-
You can clear up any additional test state within your test file using:
152
-
153
-
-`beforeEach()`
154
-
-`afterEach()`
187
+
If your code causes the page to redirect, then it must use the `@moodle/lms/core/location` module's `redirect` method, for example:
155
188
156
-
See the [Jest Setup and Teardown](https://jestjs.io/docs/setup-teardown) documentation for further information.
`mockString(identifier, component, resolved)` registers a resolved value for a specific `(identifier, component)` pair. This delegates to the default `core/str` mock that is already registered in `.jest/globalSetup.ts`.
197
+
Moodle automatically mocks the `redirect` function and allows you to specify the expected value before calling your method using the `expectRedirect()` helper:
0 commit comments