···11+// we can also use `import`, but then
22+// every export should be explicitly defined
33+44+const { fs } = require('memfs')
55+66+module.exports = fs
+6
test/core/src/read-hello-world.ts
···11+// hello-world.js
22+import { readFileSync } from 'node:fs'
33+44+export function readHelloWorld(path: string) {
55+ return readFileSync(path, 'utf-8')
66+}
+5-1
test/core/test/file-path.test.ts
···22import { describe, expect, it, vi } from 'vitest'
33import { isWindows, slash, toFilePath } from '../../../packages/vite-node/src/utils'
4455-vi.mock('fs')
55+vi.mock('fs', () => {
66+ return {
77+ existsSync: vi.fn(),
88+ }
99+})
610711describe('current url', () => {
812 it('__filename is equal to import.meta.url', () => {
+37
test/core/test/mock-fs.test.ts
···11+// hello-world.test.js
22+import { beforeEach, expect, it, vi } from 'vitest'
33+import { fs, vol } from 'memfs'
44+import { readHelloWorld } from '../src/read-hello-world'
55+66+// tell vitest to use fs mock from __mocks__ folder
77+// this can be done in a setup file if fs should always be mocked
88+vi.mock('node:fs')
99+vi.mock('node:fs/promises')
1010+1111+beforeEach(() => {
1212+ // reset the state of in-memory fs
1313+ vol.reset()
1414+})
1515+1616+it('should return correct text', () => {
1717+ const path = '/hello-world.txt'
1818+ fs.writeFileSync(path, 'hello world')
1919+2020+ const text = readHelloWorld(path)
2121+ expect(text).toBe('hello world')
2222+})
2323+2424+it('can return a value multiple times', () => {
2525+ // you can use vol.fromJSON to define several files
2626+ vol.fromJSON(
2727+ {
2828+ './dir1/hw.txt': 'hello dir1',
2929+ './dir2/hw.txt': 'hello dir2',
3030+ },
3131+ // default cwd
3232+ '/tmp',
3333+ )
3434+3535+ expect(readHelloWorld('/tmp/dir1/hw.txt')).toBe('hello dir1')
3636+ expect(readHelloWorld('/tmp/dir2/hw.txt')).toBe('hello dir2')
3737+})
···11+// we can also use `import`, but then
22+// every export should be explicitly defined
33+44+const { fs } = require('memfs')
55+66+module.exports = fs.promises