Skip to content

Commit

Permalink
Revert to jest.mock
Browse files Browse the repository at this point in the history
  • Loading branch information
Janelle Law committed Apr 25, 2022
1 parent de6ba63 commit 34a3303
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
2 changes: 1 addition & 1 deletion TESTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ The decision to mock out a component during testing should adhere to RTL's guidi

* Make sure to import the component under test last. In Jest, any `jest.mock` calls are automatically hoisted to the top of the file, above the imports. This ensures that when modules are imported, Jest knows to replace the real implementations with the mocked versions. However, the actual mock implementation code isn’t processed until the component under test is imported, which is why it’s important to do this import last so that any imported modules used inside the implementations will not end up undefined.

* If you want to return mock constants that you defined in the file scope while writing `jest.mock` calls, you will need to replace `jest.mock` with [`jest.doMock`](https://jestjs.io/docs/jest-object#jestdomockmodulename-factory-options) to prevent Jest from hoisting the `jest.mock` call before your constant is defined. However, you will need to import your component(s) under test after all of your `jest.doMock` calls to ensure that Jest uses your mocked function calls. For example, if you are mocking the `Api.Service` with `jest.doMock` for your component under test called `MyComponent`, you will need to move `import { MyComponent } from '@app/Path/To/MyComponent';` as well as `import { ServiceContext, defaultServices } from '@app/Shared/Services/Services';` after all of your `jest.doMock` calls.
* If you want to return mock constants that you defined in the file scope while writing `jest.mock` calls, you will need to import your components under test after the `jest.mock` call to prevent Jest from invoking the `jest.mock` calls before your constant is defined. For example, if you are mocking the `Api.Service` with `jest.mock` for your component under test called `MyComponent`, you will need to move `import { MyComponent } from '@app/Path/To/MyComponent';` as well as `import { ServiceContext, defaultServices } from '@app/Shared/Services/Services';` after all of your `jest.mock` calls.

* Use [`jest.requireActual`](https://jestjs.io/docs/jest-object#jestrequireactualmodulename) when you need the actual implementation of a mocked module. It can also be used to partially mock modules, allowing you to pick and choose which functions you want to mock or leave untouched.

Expand Down
4 changes: 2 additions & 2 deletions src/test/SecurityPanel/StoreJmxCredentials.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jest.mock('@app/Shared/Services/Target.service', () => {
};
});

jest.doMock('@app/Shared/Services/Api.service', () => {
jest.mock('@app/Shared/Services/Api.service', () => {
return {
ApiService: jest.fn(() => {
return {
Expand All @@ -130,7 +130,7 @@ jest.doMock('@app/Shared/Services/Api.service', () => {
};
});

jest.doMock('@app/Shared/Services/Targets.service', () => {
jest.mock('@app/Shared/Services/Targets.service', () => {
return {
TargetsService: jest.fn(() => {
return {
Expand Down

0 comments on commit 34a3303

Please sign in to comment.