-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #2811
- Loading branch information
Showing
8 changed files
with
7,276 additions
and
8,136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
id: mock-es6-class | ||
title: Mock ES6 class | ||
--- | ||
|
||
TypeScript is transpiling your ts file and your module is likely being imported using ES2015s import. | ||
`const soundPlayer = require('./sound-player')`. Therefore creating an instance of the class that was exported as | ||
a default will look like this: `new soundPlayer.default()`. However if you are mocking the class as suggested by the documentation. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { playSoundFile: mockPlaySoundFile } | ||
}) | ||
}) | ||
``` | ||
|
||
You will get the error | ||
|
||
``` | ||
TypeError: sound_player_1.default is not a constructor | ||
``` | ||
|
||
because `soundPlayer.default` does not point to a function. Your mock has to return an object which has a property default | ||
that points to a function. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return { | ||
default: jest.fn().mockImplementation(() => { | ||
return { | ||
playSoundFile: mockPlaySoundFile, | ||
} | ||
}), | ||
} | ||
}) | ||
``` | ||
|
||
For named imports, like `import { OAuth2 } from './oauth'`, replace `default` with imported module name, `OAuth2` in this example: | ||
|
||
```js | ||
jest.mock('./oauth', () => { | ||
return { | ||
OAuth2: ... // mock here | ||
} | ||
}) | ||
``` |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
website/versioned_docs/version-26.5/guides/mock-es6-class.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
id: mock-es6-class | ||
title: Mock ES6 class | ||
--- | ||
|
||
TypeScript is transpiling your ts file and your module is likely being imported using ES2015s import. | ||
`const soundPlayer = require('./sound-player')`. Therefore creating an instance of the class that was exported as | ||
a default will look like this: `new soundPlayer.default()`. However if you are mocking the class as suggested by the documentation. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { playSoundFile: mockPlaySoundFile } | ||
}) | ||
}) | ||
``` | ||
|
||
You will get the error | ||
|
||
``` | ||
TypeError: sound_player_1.default is not a constructor | ||
``` | ||
|
||
because `soundPlayer.default` does not point to a function. Your mock has to return an object which has a property default | ||
that points to a function. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return { | ||
default: jest.fn().mockImplementation(() => { | ||
return { | ||
playSoundFile: mockPlaySoundFile, | ||
} | ||
}), | ||
} | ||
}) | ||
``` | ||
|
||
For named imports, like `import { OAuth2 } from './oauth'`, replace `default` with imported module name, `OAuth2` in this example: | ||
|
||
```js | ||
jest.mock('./oauth', () => { | ||
return { | ||
OAuth2: ... // mock here | ||
} | ||
}) | ||
``` |
47 changes: 47 additions & 0 deletions
47
website/versioned_docs/version-27.0/guides/mock-es6-class.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
--- | ||
id: mock-es6-class | ||
title: Mock ES6 class | ||
--- | ||
|
||
TypeScript is transpiling your ts file and your module is likely being imported using ES2015s import. | ||
`const soundPlayer = require('./sound-player')`. Therefore creating an instance of the class that was exported as | ||
a default will look like this: `new soundPlayer.default()`. However if you are mocking the class as suggested by the documentation. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return jest.fn().mockImplementation(() => { | ||
return { playSoundFile: mockPlaySoundFile } | ||
}) | ||
}) | ||
``` | ||
|
||
You will get the error | ||
|
||
``` | ||
TypeError: sound_player_1.default is not a constructor | ||
``` | ||
|
||
because `soundPlayer.default` does not point to a function. Your mock has to return an object which has a property default | ||
that points to a function. | ||
|
||
```js | ||
jest.mock('./sound-player', () => { | ||
return { | ||
default: jest.fn().mockImplementation(() => { | ||
return { | ||
playSoundFile: mockPlaySoundFile, | ||
} | ||
}), | ||
} | ||
}) | ||
``` | ||
|
||
For named imports, like `import { OAuth2 } from './oauth'`, replace `default` with imported module name, `OAuth2` in this example: | ||
|
||
```js | ||
jest.mock('./oauth', () => { | ||
return { | ||
OAuth2: ... // mock here | ||
} | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters