Recently, I've joined a mvc.net project that utilizes typescript on the frontend.
There are numerous typescript files wrapped within module Foo {...}
, with Foo representing the primary module or namespace.
All these typescript files are transpiled into a single file named Foo.js
.
The challenge lies in the fact that none of the typescript files have export module Foo{...}
to export the namespace.
As I aim to write unit tests for this setup, I've opted for the Jest framework.
The question now is how can I import objects from my typescript files into my test?
In my package.json file:
{
"version": "1.0.0",
"name": "Foo-Web",
"private": true,
"devDependencies": {
"@types/bootstrap": "4.1.2",
"@types/jquery": "^3.3.16",
... and a variety of other libraries
},
"scripts": {
"test": "jest"
}
}
In my tsconfig.json file:
{
"compileOnSave": true,
"compilerOptions": {
"isolatedModules": false,
"allowSyntheticDefaultImports": true,
"jsx": "react",
"lib": [
"dom",
"es2017"
],
"noImplicitAny": false,
"noEmitOnError": true,
"outFile": "Out/dist/Foo.js",
"removeComments": true,
"sourceMap": true,
"target": "es5"
},
"exclude": [
"**/bower_components/*",
"**/node_modules/*",
"**/dist/*"
],
}
My jest.config.js file looks like:
module.exports = {
"testMatch": [
"**/__tests__/**/*.+(ts|tsx|js)",
"**/?(*.)+(spec|test).+(ts|tsx|js)"
],
"transform": {
"^.+\\.(ts|tsx)$": "ts-jest"
},
"moduleFileExtensions": ['js', 'jsx', 'ts', 'tsx']
}
The format of my typescript files typically follows:
module Foo {
interface blablabla ....
export class hahaha{
... class implementation
}
}
An example test file snippet:
...
var ct = new Foo.SomeClass(testProp)
....
When running the jest test, I encounter the error:
Foo is not defined