-
-
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.
fix(vitest): correctly resolve mocked node:* imports in __mocks__ fol…
…der (#6204)
- Loading branch information
1 parent
3aab8a1
commit a48be6f
Showing
9 changed files
with
109 additions
and
16 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// we can also use `import`, but then | ||
// every export should be explicitly defined | ||
|
||
const { fs } = require('memfs') | ||
|
||
module.exports = fs |
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,6 @@ | ||
// we can also use `import`, but then | ||
// every export should be explicitly defined | ||
|
||
const { fs } = require('memfs') | ||
|
||
module.exports = fs.promises |
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,6 @@ | ||
// hello-world.js | ||
import { readFileSync } from 'node:fs' | ||
|
||
export function readHelloWorld(path: string) { | ||
return readFileSync(path, 'utf-8') | ||
} |
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,37 @@ | ||
// hello-world.test.js | ||
import { beforeEach, expect, it, vi } from 'vitest' | ||
import { fs, vol } from 'memfs' | ||
import { readHelloWorld } from '../src/read-hello-world' | ||
|
||
// tell vitest to use fs mock from __mocks__ folder | ||
// this can be done in a setup file if fs should always be mocked | ||
vi.mock('node:fs') | ||
vi.mock('node:fs/promises') | ||
|
||
beforeEach(() => { | ||
// reset the state of in-memory fs | ||
vol.reset() | ||
}) | ||
|
||
it('should return correct text', () => { | ||
const path = '/hello-world.txt' | ||
fs.writeFileSync(path, 'hello world') | ||
|
||
const text = readHelloWorld(path) | ||
expect(text).toBe('hello world') | ||
}) | ||
|
||
it('can return a value multiple times', () => { | ||
// you can use vol.fromJSON to define several files | ||
vol.fromJSON( | ||
{ | ||
'./dir1/hw.txt': 'hello dir1', | ||
'./dir2/hw.txt': 'hello dir2', | ||
}, | ||
// default cwd | ||
'/tmp', | ||
) | ||
|
||
expect(readHelloWorld('/tmp/dir1/hw.txt')).toBe('hello dir1') | ||
expect(readHelloWorld('/tmp/dir2/hw.txt')).toBe('hello dir2') | ||
}) |