-
Notifications
You must be signed in to change notification settings - Fork 87
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
Ignore paths in node_modules #239
Comments
Also, it may be that case that some coverage reports are broken because I see mock receives requests for map files and returns null for them... Also if i was patien enought something like this solves problem but takes ages to create:
|
Just ran into the exact same error; ajv requires a json file in a function call, leading to this issue. Any mock-fs friendly way to deal with this other than the very manual method suggested by the OP? :) Thanks! |
Got the exact same issue as OP. |
Same issue I think, function I'm testing calls @babel/traverse and results in:
|
Same issue with dynamic mock({
'node_modules/ajv/lib/refs/json-schema-draft-06.json':
fs.readFileSync('node_modules/ajv/lib/refs/json-schema-draft-06.json'),
}) |
^^ Solved with #213 (comment) |
Hi all, Not sure if people are still trying to find a nice way of managing this or not but I came up with a cleaner solution compared to #213 (comment). My solution enforces lazy loading and skips non-essential directories using only import fs from 'fs'
import path from 'path'
import mockFS from 'mock-fs'
import type FileSystem from 'mock-fs/lib/filesystem'
function lazyLoadNodeModules(mockedFileSystem: FileSystem.DirectoryItems, from: string, depth = 6): void {
const stat = fs.lstatSync(from)
if (stat.isDirectory() && depth > 0) {
for (const item of fs.readdirSync(from)) {
if (item.startsWith('.') || item === '@types') {
continue
}
lazyLoadNodeModules(mockedFileSystem, path.join(from, item), depth - 1)
}
} else if (stat.isFile()) {
mockedFileSystem[from] = mockFS.load(from, { lazy: true })
}
}
export default lazyLoadNodeModules To use it, import the above code as a separate file in your test code and then run it with: import mockFS from 'mock-fs'
beforeAll(() => {
const mockedFileSystem = {}
lazyLoadNodeModules(mockedFileSystem, path.resolve(path.join(process.cwd(), 'node_modules')))
mockFS(mockedFileSystem)
})
afterAll(() => {
mockFS.restore()
}) By default this will:
I recommend only mocking what you need for smaller projects, but mocking everything – if like me – you're doing webpack testing. Hope this helps! |
Im trying to test if webpack builds with provided configuration but test fail on some libraries lazy requiring json configuration like for example:
Either give a way to disable mocking
require
or disable mocking paths by some reqexp.The text was updated successfully, but these errors were encountered: