-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Fixing type issue with spyOn method * Adding tests
- Loading branch information
Showing
3 changed files
with
27 additions
and
2 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
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,5 @@ | ||
export class HelloWorld { | ||
hello() { | ||
return 'Hello World!' | ||
} | ||
} |
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 |
---|---|---|
@@ -1,12 +1,32 @@ | ||
import { describe, expect, test, vi } from 'vitest' | ||
import * as mock from './fixtures/hello-mock' | ||
|
||
/** | ||
* @vitest-environment happy-dom | ||
*/ | ||
|
||
describe('spyOn', () => { | ||
const hw = new mock.HelloWorld() | ||
|
||
test('correctly infers method types', async () => { | ||
vi.spyOn(localStorage, 'getItem').mockReturnValue('world') | ||
expect(window.localStorage.getItem('hello')).toEqual('world') | ||
}) | ||
|
||
test('infers a class correctly', () => { | ||
vi.spyOn(mock, 'HelloWorld').mockImplementationOnce(() => { | ||
const Mock = vi.fn() | ||
Mock.prototype.hello = vi.fn(() => 'hello world') | ||
return new Mock() | ||
}) | ||
|
||
const mockedHelloWorld = new mock.HelloWorld() | ||
expect(mockedHelloWorld.hello()).toEqual('hello world') | ||
}) | ||
|
||
test('infers a method correctly', () => { | ||
vi.spyOn(hw, 'hello').mockImplementationOnce(() => 'hello world') | ||
|
||
expect(hw.hello()).toEqual('hello world') | ||
}) | ||
}) |