Circular dependences are one way of mocking child functions inside a parent in Jest.js. I've seen ES6 examples on how to use circular imports, but I'm having trouble translating this to ES5 syntax using require. Here's what I have:
const currentFile = require('./targetFile.js');
const a = () => {
return currentFile.b();
};
const b = () => {
return 'from b'
};
module.exports = { a, b }
I then attempt to run the above code in a tester file that requires the above as targetFile:
const targetFile = require("../targetFile.js");
test('a and b', () => {
console.log(targetFile.a(), `=====targetFile.a()=====`);
});
FAIL views/admin/__tests__/targetFile.test.js
✕ a and b (12ms)
● a and b
TypeError: currentFile.b is not a function
26 |
27 | const a = () => {
> 28 | return currentFile.b();
| ^
29 | };
30 |
31 | const b = () => {
What is the correct syntax for the above using requires (or other ES5 browser compatible syntax)?