In my test I want to mock a dynamic path:
import * as path from 'path';
const file = path.join('path', 'file.js');
this.config = require(file);
I use the option virtual:true in my mock as it is suggested in this answer.
import * as path from 'path';
const mockRequire = jest.fn();
jest.mock('path\\file.js',
() => {
return mockRequire();
},
{ virtual: true }
);
Running tests using this mocked file on Windows passes just fine. However, it doesn't work on Linux because of the slashes. I try to normalize the path by using path.normalize('path\\file.js')
but it doesn't work.
import * as path from 'path';
const mockRequire = jest.fn();
jest.mock(
path.normalize('path\\file.js'),
() => {
return mockRequire();
},
{ virtual: true }
);
Error says: ReferenceError: Cannot access 'path' before initialization
Is there a way to make the code OS agnostic as alternative to using path.normalize?
Or is any way to normalize the path of this dynamic module path without having problems with initialization?
As final note, I would prefer to not add any new library if possible.