Questions tagged [rewire]
21 questions
6
votes
0 answers
ReferenceError 'navigator is not defined' when using jest, jsdom and rewire
I'm stuck with a jest test setup. I want to test some client side code, an electron preload script, to be precise and want to use rewire to test methods that are not exported.
The script under test depends on a another script that accesses…

Andreas Dolk
- 113,398
- 19
- 180
- 268
4
votes
1 answer
babel-plugin-rewire does not work in a Typescript project
Note: My goal it bring Rewire like functionally to the project. Be it using the Rewire package, babel-plugin-rewire or any other library that satisfies my goal. With that in mind, here is the detail:
I am trying to setup a new Typescript project…

Nishant
- 54,584
- 13
- 112
- 127
3
votes
1 answer
Rewire private Typescript class methods
I'm trying to write unit tests for some private methods in my class using Typescript for my Node.js module.
I tried using rewire, but it's unable to access any methods (even public ones).
Here is my setup:
myclass.ts
class MyClass{
private…

Wyctus
- 33
- 2
- 4
2
votes
0 answers
Jest coverage test does not include 'rewired' files
I am including my non-exported function (let's say A) to the Jest unit tests with the 'rewire' function. When I run the coverage test, the A function is not included to the coverage test. Is there any way to include rewired files to the coverage…

Евгения Гребенюк
- 23
- 3
2
votes
0 answers
The following changes are being made to your tsconfig.json file: - compilerOptions.paths must not be set (aliased imports are not supported)
I just cloned a react/typescript template repository and did npm install I should say that the node packages installed in this app is really large number and react rewired is installed too, also the required node version is 12.10.0
I came in to a…

Yusuf
- 2,295
- 7
- 15
- 34
2
votes
1 answer
Rewire a private function p0, but says: p0 not defined
The main.js is like this
const p0 = (a, b) => (a+b)
const p1 = (a, b) => {
return p0(a, b) + 10;
}
module.exports = { p1 }
The test code
const rewire = require('rewire');
const myMod = rewire('main');
it('.test p0', async() => {
…

user3552178
- 2,719
- 8
- 40
- 67
1
vote
0 answers
Rewire : How to access an exported class and a function and test them?
I have a class and a function in one file
export class MainClass{
....
}
export function test_function(){}
Previously, I just had the test_function exported and could easily test it using
rewire('file.ts').__get__("test_function")
How can I…

Prateek Narendra
- 1,837
- 5
- 38
- 67
1
vote
0 answers
Does Rewire set the current running module variables as well as the specified module variables?
So, I was doing some testing with Rewire with Mocha, and I noticed behavior that seems odd to me. using myModule.__set__() seems to actually set the specified variable in the the specified module (myModule), AND in the global scope of the current…

Mark Lindstrom
- 63
- 7
1
vote
0 answers
__Rewire__ is not a function
Using Rewire for testing..
Rewire is failing to rewire, and I'm wondering why.
Here is an example of rewire failing:
A file has a few exported functions...
export function foo() {}
export function bar() {}
export function baz() {}
In a test…

GN.
- 8,672
- 10
- 61
- 126
1
vote
1 answer
NodeJS: Unit Test for class calling constructor and functions of another class
I need to write an unit test for the following classA. I do not want to test methodA but set a dummy method instead:
const classB = require('classB');
function a() {
const b = classB();
b.methodA();
}
I tried to use rewire:
const classA =…

AntonSack
- 1,021
- 2
- 25
- 47
0
votes
1 answer
running mocha tests through vsCode throws error 'source not found'
all my tests are running with mocha.js and I can debug them easily, except for one specific test file that causes the error Could not load source 'some path here/fileNameToTest.js': Source not found.
to emerge.
In my launch.json file I have the…

Tzachi Elrom
- 385
- 1
- 2
- 19
0
votes
0 answers
How to mock non exported const variables with jest
I have below file with one non exported constant variable.
I am trying to mock the variable value for the purpose of unit testing functions.
This file is common and used across the project, so it is affecting UT of other files as well.
//…

User7723337
- 11,857
- 27
- 101
- 182
0
votes
0 answers
How to override ava test runner's env variables while testing?
Ava test runner in my project has its own env variables defined like this in package.json:
"ava": {
"require": [
"esm"
],
"files": [
"test/unit/**/**/ava-*"
],
"environmentVariables":…

quielfala
- 361
- 4
- 18
0
votes
1 answer
window.location is undefined when using Jest with rewire
I'm writing tests for a file(some-file.js) that calls functions from another file(url-manipulator.js). I'm getting an error regarding window.location being undefined. You can see in the error below that I logged window.location right before the…

Dustin
- 3
- 2
0
votes
1 answer
How to force a return value from a mocked non-exported function using sinon or proxyquire or rewire?
I am just getting started unit testing nodejs. I have been using mocha, chai and sinon.
I hit a snag when I wanted to test a function which is not exported. proxyquire looks good, as does rewire.
There are lots of tutorials, but they tend to be…

Mawg says reinstate Monica
- 38,334
- 103
- 306
- 551