I am new to Typescript and wanted to implement unit testing with Jest. I installed Jest, and when I run it I receive an error, I have simplified the code for this example, and it continues to fail:
FAIL src\__tests__\validateSender.ts
https validation
ReferenceError: validateSender is not defined
at Object.<anonymous>.test (src/__tests__/validateSender.ts:10:10)
at process._tickCallback (internal/process/next_tick.js:109:7)
The test which cannot find the function (./src/__tests__/validateSender.js
):
import {} from "jest";
const connection = {
encrypted: "undefined",
};
const req = {
connection : ("{connection}"),
};
test("https validation", () => {
expect(validateSender(req)).toThrow();
});
the function (./src/validateSender.js
):
function validateSender(req) {
if (typeof req.connection.encrypted === "undefined") {
throw new CustomException("validateSender", "connection must be secured.");
} else { return; }
}
my Jest package.json portion (./package.json
):
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json"
]
}
I have attempted adding an import statement for the validateSender file directly, but it fails in the same way (on npm test
from the root directory I added import {} from "./src/validateSender";
to the file and received the error above.)
It was also suggested I try import validateSender from "./src/validateSender";
but I receive error TS2307: Cannot find module './src/validateSender'.
It appears that I am missing some foundational knowledge for this process, but I have failed to find it in the TS handbook.