Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Did not spy fs.createWriteStream, and it will create the file in the real file system #404

Open
axetroy opened this issue Nov 14, 2024 · 0 comments

Comments

@axetroy
Copy link

axetroy commented Nov 14, 2024

import fs from 'node:fs'
import path from 'node:path'
import test from 'node:test'

import mock from 'mock-fs'

test.beforeEach(() => {
    mock({
        [import.meta.dirname]: {}
    })
})

test.afterEach(() => {
    mock.restore()
})

test('Test', async (t) => {
    // ❌ This will create the file in the real file system
    fs.createWriteStream(path.join(import.meta.dirname, 'testing-2024-11-13.001.log'))
    // ✅ This will not
    // fs.writeFileSync(path.join(import.meta.dirname, 'testing-2024-11-13.001.log'), 'hello world!')

    for (const file of fs.readdirSync(import.meta.dirname)) {
        console.log(file)
    }
})

Just create test file mock-fs.test.mjs

And then run the test with the following command

node --test --experimental-default-type=module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant