I'm learning GitLab at the moment and I'm a complete beginner to it. I'm trying to test my JavaScript code to see if a function checkInputs() was called. This is my Jest file
/** @jest-environment jsdom */
import '@testing-library/jest-dom';
import * as script from './script.js'
describe('checkInputs', () => {
it('has to be called', () => {
script.checkInputs=jest.fn()
expect(script.checkInputs).toHaveBeenCalled()
});
})
And a part of the script.js file I'm trying to test:
export let checkInputs = function () {
passwordVal = password.value;
password2Val = password2.value;
reg = /[`!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?~ 0-9]/;
if (password2Val.length < 6 || passwordVal.length < 6) {
errorMsg(password, password2);
} else if (password2Val !== passwordVal) {
errorMsg(password, password2);
} else if (!reg.test(passwordVal) || !reg.test(password2Val)) {
errorMsg(password, password2);
}
};
checkInputs()
Pipelines trigger on commit and Jest should test if the checkInputs() was called, but it gives the following error:
TypeError: Cannot read properties of null (reading 'value')
22 |
23 | export let checkInputs = function () {
> 24 | passwordVal = password.value;
| ^
25 | password2Val = password2.value;
26 |
I've tried almost anything and I'm pretty much stuck. Also here are the dependecies in package.json file:
"devDependencies": {
"@babel/core": "^7.19.6",
"@babel/preset-env": "^7.19.4",
"@babel/preset-flow": "^7.18.6",
"@testing-library/jest-dom": "^5.16.5",
"babel-loader": "^8.2.5",
"jest": "^29.2.1",
"jest-environment-jsdom": "^29.2.1",
"jest-junit": "^14.0.1",
"webpack": "^5.74.0",
"webpack-cli": "^4.10.0"
This is my first post here so PLEASE go easy on me. Thanks in advance