Mocking dotenv with Jest - jestjs

I have a test from a web application that was originally created using CRA. So It uses Jest and react-testing-library. I am also in a TypeScript environment.
The beginnings of the test I have:
import { render, screen } from "#testing-library/react";
import dotenv from 'dotenv';
import App from './App';
. . . .
jest.mock('dotenv');
//(what goes here?)
But here is where I need help. I am not sure how to mock the module. In the component I have something like:
if (process.env.REACT_APP_CONSTRUCTION === "true") {
return (<UnderConstruction />);
} else {
return (<App/ >);
}
In testing this component I would like to test the two scenarios, one where the environment returns "true" and the other otherwise.
Ideas or suggestions?

You can create mock env variables after each test in jest like below.
Also you don't need to mock jest.mock('dotenv'); so you can delete that part in your test file.
const OLD_ENV = process.env
afterEach(() => {
cleanup()
jest.clearAllMocks()
jest.resetModules()
process.env = { ...OLD_ENV }
delete process.env.NODE_ENV
})
it('scenario 1', () => {
// Given
process.env.REACT_APP_CONSTRUCTION = true // mock variable for scenario 1
// When
const { queryByText } = render(<App />)
const underConstructionText = queryByText('Under Construction')// Just an example
// Then
expect(underConstructionText).toBeInTheDocument()
})
it('scenario 2', () => {
// Given
process.env.REACT_APP_CONSTRUCTION = false // mock variable for scenario 2
...
// When
const { queryByText } = render(<App />)
const underConstructionText = queryByText('Under Construction')
// Then
expect(underConstructionText).not.toBeInTheDocument()
})

Related

How to cover unit test of "if statement" in jest dependeing of external variable in nodejs

I have the following Javscript code:
const authenticationTypeMapping = (payload) => {
const { API_CONFIG } = process.env;
try {
const apiConfig = JSON.parse(API_CONFIG.toString('utf8'));
// set authenticationType to Federated for production
if (apiConfig.API_BASE_URL.includes('prd')) {
payload.authenticationTypeName = 'Federated';
// set authenticationType to Federated for dev or UAT
} else if (apiConfig.API_BASE_URL.includes('dev') || apiConfig.API_BASE_URL.includes('uat')) {
payload.authenticationTypeName = 'Basic';
}
} catch (err) {
console.log(`Failed to map authenticationType. Unable to parse Secret: ${err}`);
}
return payload;
};
I have problem to cover unit test using jesty for the code for the lines inside try block.
If statement depends on external variable "apiConfig.API_BASE_URL" of "process.env" which I don't how to represent to jest code.
it('should call authenticationTypeMapping', async () => {
const payload = mapper.authenticationTypeMapping(basicPayload);
expect(payload.authenticationTypeName).toEqual('Basic');
});
What should be added to cover the unit test?
You can set the Environment in the test and check for the same condition in the unit test as follows
it('should call authenticationTypeMapping', async () => {
process.env.API_BASE_URL = 'prd...'
expect(mapper.authenticationTypeMapping(basicPayload).authenticationTypeName).toEqual('Federated');
process.env.API_BASE_URL = 'dev...'
expect(mapper.authenticationTypeMapping(basicPayload).authenticationTypeName).toEqual('Basic');
});
Maybe you can have more than one unit tests to make things clear like one to test 'prd' and one to test 'dev'

Jest and testing-library, mock prevPros on componentDidUpdate

I have a component (OOP) that has this in componentDidUpdate:
componentDidUpdate = (prevProps) => {
const prevLoading = prevProps.benefitsReducer.loading;
const { benefitsReducer: { loading } } = this.props;
if (prevLoading && !loading) {
this.setState({ showModal: true }); // I NEED TO REACH THIS LINE
}
}
Some onSubmit function changes the props, but I have mocked the props and also the store (I'm using Redux).
How could have 2 versions of the same prop? Currently I have a big object with all the props needed to the test, but I have this problem on how to use the previous one.

how to refer to a function when unit testing with NodeJS / Mocha

I'm making my first test with Mocha.
Dummy test is passing, but when I want to refer to my actual function that is in another file, it won't find it:
ReferenceError: main is not defined
I have a single index.js with :
async function main() {
function comparer(otherArray) {
return function (current) {
return otherArray.filter(function (other) {
return other.prm === current.prm && other.conso_prod === current.conso_prod
}).length === 0;
}
}
}
module.exports = main();
and in my test.js file, I do:
const {expect} = require('chai');
describe('Sum numbers', () => {
it('Compare 2 existing array', () => {
const meters = []
const perimeter = []
const onlyInMeters = meters.filter(main.comparer(perimeter));
expect(onlyInMeters).to.equal([]);
});
});
But when I refer to main.comparer, it can't find it:
ReferenceError: main is not defined
What am I forgetting? Sorry, I'm a NodeJS Noob!
It seems like you did not import the index.js file in test.js file. You are returning noting from main function as well.
Also, why are you exporting it like module.exports = main(); Instead you can do this:
// index.js
module.exports = {
comparer: (otherArray) => { ... }
}
// test.js
cosnt main = require('PATH_OF_index.js');
main.comparer();

nodejs/mocha/chai as promise : variables used in expected async function initialized outside

I am brand new to mocha/chai and I spent 2 days trying to solve the following issue without any success (please note that the code below is just to present the concept, it is not the real one).
I have got a JS file called "api.js" in which some variables such as SERVER_URL are initialized at the top of the file through dotenv framework.
api.js :
const SERVER_URL = process.env.SERVER_URL;
async function startAPI () {
return new Promise ( (resolve, reject) => {
console.log(`${SERVER_URL}`);
resolve();
});
exports = {startAPI};
Now I have got "test.js" file in which :
test.js:
require('../api');
it('the test', async () => {
return await expect(api.startAPI()).to.be.fulfilled;
});
The problem is that SERVER_URL is undefined during the test and I cannot modify the api.js (as I am not the owner), just the test.js.
How can I run the test with the SERVER_URL variable set correctly (to process.env.SERVER_URL value from api.js) ?
Is there a solution without any refactoring ?
And if not what is the best solution ?
Experts, thanks in advance for your precious help
A way to improve testability is to use process.env.SERVER_URL instead of SERVER_URL where possible - or getServerUrl():
const getServerUrl = () => process.env.SERVER_URL;
This way process.env.SERVER_URL can be mocked at any point.
An alternative is to import module after process.env.SERVER_URL was mocked. This should involve decaching if there's more than one test that uses this module, because it won't be re-evaluated otherwise.
const decache = require('decache');
...
let originalServerUrl;
beforeEach(() => {
originalServerUrl = process.env.SERVER_URL;
});
beforeEach(() => {
process.env.SERVER_URL = originalServerUrl;
});
it('the test', async () => {
decache('../api');
process.env.SERVER_URL = '...';
const api = require('../api');
await expect(api.startAPI()).to.be.fulfilled;
});
If it's expected that there's no SERVER_URL in tests, it can be just discarded after it was mocked:
The easiest way would be just to set these variables when you run your test from CLI:
e.g. in npm scripts:
"scripts": {
"test": "SERVER_URL='http://example.com' mocha"
}
or directly from terminal:
$ SERVER_URL='http://example.com' npm test
But better solution would be mock environment variables in your tests with little refactoring. And need proxyquire to be installed. And actually async/await is not needed here.
const proxyquire = require('proxyquire').noPreserveCache() // noPreserveCache is important to always have refreshed script with new process.env.SERVER_URL in each test
const MOCKED_SERVER_URL = 'http://example.com'
describe('example', () => {
let initialServerUrl
let api
beforeEach(() => {
initialServerUrl= process.env
})
afterEach(() => {
process.env = initialServerUrl
})
it('fulfilled', () => {
process.env.USE_OTHER_CODE_PATH = MOCKED_SERVER_URL
api = proxyquire('../api', {})
return expect(api.startAPI()).to.be.fulfilled
})
it('rejected', () => {
process.env.USE_OTHER_CODE_PATH = ''
api = proxyquire('../api', {})
return expect(api.startAPI()).to.be.rejected
})
})
You can set .env variables with mocha using the following line:
env SERVER_URL=htt://api.yourserver.com/ mocha test
This way mocha knows what to expect from your process.env.SERVER_URL

Mock.mockImplementation() not working

I have a service class
Service.js
class Service {
}
export default new Service();
And I am trying to provide a mock implementation for this. If I use something like this:
jest.mock('./Service', () => { ... my mock stuff });
It works fine, however I'm not able to access any variables declared outside of the mock, which is a bit limiting as I'd like to reconfigure what the mock returns, etc.
I tried this (inspired by this other StackOverflow article: Service mocked with Jest causes "The module factory of jest.mock() is not allowed to reference any out-of-scope variables" error)
import service from './Service';
jest.mock('./Service', () => jest.fn);
service.mockImplementation(() => {
return { ... mock stuff }
);
Unfortunately when I am trying to run this, I get the below error:
TypeError: _Service2.default.mockImplementation is not a function
I had same problem as #Janos, the other answers didn't help either. You could do two things :
If you need to mock only a function from Service, in your test file:
import service from './Service';
jest.mock('./Service', () => jest.fn());
service.yourFunction = jest.fn(() => { /*your mock*/ })
 
If you need to mock the entire Module:
Say your service.js is in javascript/utils, create a javascript/utils/_mocks_ and inside it create a service.js file, you can then mock the entire class in this file, eg:
const myObj = {foo: "bar"}
const myFunction1 = jest.fn(() => { return Promise.resolve(myObj) })
const myFunction2 = ...
module.exports = {
myFunction1,
myFunction2
}
then in your test file you just add:
jest.mock('./javascript/utils/service')
...functions exported from the mockfile will be then hit through your test file execution.
The mock is equal to jest.fn. You need to call jest.fn to create a mocked function.
So this:
jest.mock('./Service', () => jest.fn);
Should be:
jest.mock('./Service', () => jest.fn());
ran into similar issues and resolved it by using .mockImplementationOnce
jest.mock('./Service', () => jest.fn()
.mockImplementationOnce(() => {
return { ... mock stuff }
})
.mockImplementationOnce(() => {
return { ... mock other stuff }
})
);
now when you run another test it will return the second mock object.
You need to store your mocked component in a variable with a name prefixed by "mock" and make sure you return an object with a default property as you import your Service from the default in your "main.js" file.
// Service.js
class Service {
}
export default new Service();
// main.test.js (main.js contains "import Service from './Service';")
const mockService = () => jest.fn();
jest.mock('./Service', () => {
return {
default: mockService
}
});
I had similar problem, and the cause was that ".spec.js" file had an
import jest from "jest-mock";
After removing this line, it worked.
My mistake was that I was resetting the mock before each test. If you do that, be sure to reconfigure the mock implementation.
For example, change this:
let value;
let onPropertyChange: OnPropertyChangeCallback = jest.fn((changes: any) => {
value = changes["testValue"];
});
const user = userEvent.setup();
beforeEach(() => {
jest.resetAllMocks();
});
to this:
let value;
let onPropertyChange: OnPropertyChangeCallback;
const user = userEvent.setup();
beforeEach(() => {
jest.resetAllMocks();
onPropertyChange = jest.fn((changes: any) => {
value = changes["testValue"];
});
});

Resources