What is the best way to conduct tests on Typescript modules that are not intended for

Even though the compiler accepts my current solution without any errors, the tests are still failing with the message "ReferenceError: myFunction is not defined".

I am interested in testing the functionality of the following module using TypeScript: File1.ts:

module MyModule {
  export function myFunction(): string {
    return "Hello, World!";
  }
}

Here is how the test setup currently looks like: File1.test.ts:

module MyModule {
  describe('Test1', () => {
    test('equal', () => {
      expect(myFunction()).toBe("Hello, World!");
    });
  });
}

Both files are included in the tsconfig.json file:

"files":[
  "File1.test.ts",
  "File1.ts"
]

I would appreciate any advice or assistance to resolve this issue. Thank you!

Answer №1

Did you happen to overlook the import and export of your module? Give this a try:

In File1.ts:

export module MyModule {
  export function myFunction(): string {
    return "Greetings, Earthlings!";
  }
}

And in File1.test.ts:

import { MyModule } from './File1';

describe('Testing Module', () => {
  test('check equality', () => {
    expect(MyModule.myFunction()).toBe("Greetings, Earthlings!");
  });
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a Static Injector error in Angular 5 while trying to inject a component from a shared module

Here lies the code for my component file. My goal is to develop a reusable modal component using ng-bootstrap's modal feature. However, upon trying to import the following component from the shared module, I encounter a static injector error. Despite ...

implementing firestore onsnapshotListner feature with pagination

I have a web application that needs real-time updates on a large collection of documents. However, due to the size of the collection, retrieving data without applying a limit is not feasible and inefficient. Therefore, it is crucial to implement a query li ...

Obtaining an OBJECT from a hashmap instead of a string

In my code, I am working with a hashmap and I want to extract all the values from this map without needing to reference a specific key. Here is a basic overview of the process: Create a hashmap using an external file with a structure of "id:value" Utili ...

Why aren't my arrays' characteristics being recognized when I create an instance of this class?

There is a puzzling issue with the arrays in my class. Despite creating them, when I try to access them in another class they are not recognized, only the otherProp is visible. To make matters worse, attempting to add elements to the arrays results in an ...

Bringing in Static Functions Typescript

Having trouble importing a static class function into my main.js file and encountering an error after compiling with tsc: TypeError: Cannot read property 'router' of undefined at Object. (path/to/main.js:36:27)> Various attempts to assign a ...

What strategies can be employed to create a system for managing multiple permission groups in MongoDB effectively?

Currently, I am tackling a complex project management application which involves the challenge of setting up resource permissions for various user profiles. The task at hand: User scenario Meet John, a user with a standard user profile. John initiates a ...

What is the best ECMAScript version to select for implementing the TypeScript compiler in an Electron application?

In my Electron 5.0.6 project, I currently have es3 as the default target in my tsconfig.json file. However, I received an error message indicating that I need to upgrade to at least es6 to utilize getter/setter functionality in TypeScript. I am now contem ...

Issue with debugging: BrowserHistory function causing test failure

I am relatively new to writing unit tests for my React+Redux application. I recently created a NotFound page/component and have been working on its unit test. However, I am encountering an issue with a failing onClick event test. 404.jsx const GenericNot ...

Tips on personalizing the formatting alert in Webclipse for Angular 2 using Typescript

Webclipse offers extensive formatting warnings for TypeScript code, such as detecting blank spaces and suggesting the use of single quotes over double quotes. However, some users find the recommendation to use single quotes annoying, as using double quotes ...

A mistake has occurred: Unhandled promise rejection TypeError: Unable to assign the property 'devices' to an undefined object in Ionic 4 with Angular

Within my MyDevicesPage class, I am attempting to manipulate the res object and then pass it to the updateDevicesToServer method of DataService for further actions. The code compiles without errors, but at runtime, an error is thrown: ERROR Error: Uncaught ...

Using i18next to efficiently map an array of objects in TypeScript

I am currently converting a React project to TypeScript and using Packages like Next.js, next-i18next, and styled-components. EDIT: The information provided here may be outdated due to current versions of next-i18next. Please check out: Typescript i18ne ...

Having trouble locating the npm version due to the error message "Cannot read property 'node' of undefined"?

I recently installed a few node modules using npm, including express, handlebars, mocha, and chai. Everything seemed to be working fine until I attempted to install the zombie module using npm install --save-dev zombie. However, this command resulted in ...

What is the method to utilize global mixin methods within a TypeScript Vue component?

I am currently developing a Vue application using TypeScript. I have created a mixin (which can be found in global.mixin.js) and registered it using Vue.mixin() (as shown in main.ts). Content of global.mixin.js: import { mathHttp, engHttp } from '@/ ...

Angular 2- Unable to bind to 'ngSwitchCase' as it is not recognized as a native property

I am facing an issue with my code where I have two lists that are displayed in my .html file. In order to avoid repetition, I decided to utilize ngSwitch. However, when I try to implement this approach, I encounter an error. Here is the snippet of code cau ...

What is the process for integrating Vue plugins into Vue TypeScript's template?

Seeking guidance on integrating Vue plugins into Vue TypeScript's template, for example with vue-webpack-typescript. Specifically interested in incorporating vue-meta. Included the following code in ./src/main.ts: import * as Meta from 'vue-me ...

Creating a dynamic union type in Typescript based on the same interface properties

Is it possible to create a union type from siblings of the same interface? For example: interface Foo { values: string[]; defaultValue: string; } function fooo({values, defaultValue}: Foo) {} fooo({values: ['a', 'b'], defaultVal ...

Viewing an image from a local file on a web browser

I am currently working on a project where I want the user to be able to select a local image that will then be displayed on the page. As someone who is new to web development, I did a lot of research and found some helpful information on StackOverflow. I t ...

I'm unsure how to utilize the generic type in this particular scenario. It's a bit confusing to me

Recently, I delved into TypeScript generics and applied them in specific scenarios. However, I encountered some challenges. While working with two different interfaces, I faced a need for flexibility. For instance, I needed to make server requests. func ...

Issue encountered while attempting to run storybook - Error: Command 'start-storybook' not recognized

Encountering an issue while trying to run storybook.... even after a fresh installation. npm run storybook > @ storybook /media/programmersedge/New_Volume/devs/demostorybook > start-storybook -p 9001 -c .storybook sh: 1: start-storybook: not foun ...

prolonging inner interface created by supabase

Below is the Typescript interface that has been generated by Supabase export interface definitions { Users: { /** Format: uuid */ id: string; /** * Format: timestamp with time zone * @default now() */ created_at?: string; ...