Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To perform a unit test on external functions with Angular's jasmine outside of class, follow these steps:

  1. Import the external module or function you want to test in your spec file.

  2. Describe the test using the 'describe' function. Provide a name for the test and a function to contain the test logic.

  3. Write your test cases using the 'it' function. Provide a name for the test case and a function that contains the actual test code.

  4. Use the 'expect' function to define the expected outcome of the test. This is where you define your assertions.

  5. Run the tests using the 'ng test' command in the terminal.

Example:

import { myExternalFunction } from './path/to/external/module';

describe('myExternalFunction', () => {

  it('should convert a string to uppercase', () => {
      const result = myExternalFunction('hello world');
      expect(result).toEqual('HELLO WORLD');
  });

  it('should return an empty string if input is null', () => {
      const result = myExternalFunction(null);
      expect(result).toEqual('');
  });

});

In this example, we have imported the 'myExternalFunction' module from an external location. We then use the 'describe' function to group our test cases together under the label 'myExternalFunction'. Next, we write two test cases using the 'it' function. In the first test case, we pass 'hello world' as the input and expect the output to be 'HELLO WORLD'. In the second test case, we pass null as the input and expect an empty string as the output. We then run the tests using the 'ng test' command in the terminal. If the tests pass, we will see a success message in the terminal.