Skip to content

hirezio/vscode-testing-snippets

Repository files navigation

HiRez.io Testing Snippets

VS Code snippets that will make your testing life easier.

Version Installs Code of Conduct License: MIT

Want to learn more about how to use these snippets correctly?


Available Snippets:

Snippet Description
desc Generates an a describe function
giv Generates an a Given function
when Generates an a When function
then Generates an a Then function
testcase describe with Given and Then
descwhen describe with When
acomptest Generates an Angular Component test
aservtest Generates an Angular Service test
ngtestbase Generates a base for an Angular micro test
addSpy Adds a jasmine/jest auto spy boilerplate code
addDep shortest way to add a private typed dependency to a class constructor
methodplaceholder Generates an empty method that throws an "unimplemented" error

Snippets Usage

SNIPPET: desc

Output:

describe('<Your Description>', () => {
  
});

SNIPPET: giv

Output:

Given(() => {
  
});

SNIPPET: when

Output:

When(() => {
  
});

SNIPPET: then

Output:

Then(() => {
  
});

SNIPPET: testcase

Output:

describe('GIVEN: <--Your Given Description-->', () => {
  
  Given(() => {
  
  });
  
  Then('<Your THEN description>', () => {
  
  });
});

SNIPPET: descwhen

Output:

describe('METHOD: <Method Name>', () => {
  
  When(() => {
    serviceUnderTest.<Method Name>()
  });
  
});

SNIPPET: acomptest

⚠ INSTRUCTIONS:

  1. Choose between "jasmine" or "jest"
  2. Hit "tab" to jump between variables

Output:

import { TestBed } from '@angular/core/testing';
import { Spy, provideAutoSpy } from '<--Your Testing Framework-->-auto-spies';

describe('<--Your Component Type-->', () => {
  let componentUnderTest: <--Your Component Type-->;

  Given(() => {
    TestBed.configureTestingModule({
      providers: [<--Your Component Type-->]
    });

    componentUnderTest = TestBed.inject(<--Your Component Type-->);

  });

  describe('METHOD: <--Method Name-->', () => {

    When(() => {
      componentUnderTest.<--Method Name-->();
    });

    describe('GIVEN <--Your Given Description-->', () => {
      Given(() => {
        // <--Your Given Description-->
        
      });
      Then('<--Your Then Description-->', () => {
        // <--Your Then Description-->
      });
    });

  });
});

SNIPPET: aservtest

⚠ INSTRUCTIONS:

  1. Choose between "jasmine" or "jest"
  2. Hit "tab" to jump between variables

Output:

import { TestBed } from '@angular/core/testing';
import { Spy, provideAutoSpy } from '<--Your Testing Framework-->-auto-spies';

describe('<--Your Service Type-->', () => {
  let serviceUnderTest: <--Your Service Type-->;

  let actualResult: any;

  Given(() => {
    TestBed.configureTestingModule({
      providers: [<--Your Service Type-->]
    });

    serviceUnderTest = TestBed.inject(<--Your Service Type-->);

    actualResult = undefined;

  });

  describe('METHOD: <--Method Name-->', () => {

    When(() => {
      serviceUnderTest.<--Method Name-->();
    });

    describe('GIVEN <--Your Given Description-->', () => {
      Given(() => {
        // <--Your Given Description-->
        
      });
      Then('<--Your Then Description-->', () => {
        // <--Your Then Description-->
      });
    });

  });
});

SNIPPET: ngtestbase

⚠ INSTRUCTIONS:

  1. Choose between "jasmine" or "jest"
  2. Hit "tab" to jump between variables
  3. Choose between "component", "service", "directive" or "pipe"

Output:

import { TestBed } from '@angular/core/testing';
import { Spy, provideAutoSpy } from '<--Your Testing Framework-->-auto-spies';

describe('<--Type To Test-->', () => {
  let <--Angular Type Choice-->UnderTest: <--Type To Test-->;

  let actualResult: any;

  Given(() => {
    TestBed.configureTestingModule({
      providers: [<--Type To Test-->]
    });

    <--Angular Type Choice-->UnderTest = TestBed.inject(<--Type To Test-->);

    actualResult = undefined;

  });

  describe('METHOD: <--Method Name-->', () => {

  });
});

SNIPPET: addSpy

Makes adding an "auto spy" very easy.

⚠ INSTRUCTIONS:

  1. Write / paste your class type
  2. Hit "tab" to turn the variable to camelCase ("lowercase"s the first letter)
  3. Cut and paste each line to its appropriate place

Output:

// This goes in your first "Describe":
let myServiceTypeSpy: Spy<MyServiceType>;

// This goes in your TestBed's "providers" array:
provideAutoSpy(MyServiceType),

// This goes in your first Given:
MyServiceTypeSpy = TestBed.inject<any>(MyServiceType);

SNIPPET: addDep

Makes adding a class constructor dependency very easy.

⚠ INSTRUCTIONS:

  1. Write or paste your class type
  2. Hit "tab" to turn the variable to camelCase ("lowercase"s the first letter)

Output:

private myServiceType: MyServiceType,

SNIPPET: methodplaceholder

Output:

<--myMethod-->() {
  throw new Error('Method not implemented.');
}

Contributing

Want to contribute? Yayy! 🎉

Please read and follow our Contributing Guidelines to learn what are the right steps to take before contributing your time, effort and code.

Thanks 🙏


Code Of Conduct

Be kind to each other and please read our code of conduct.


License

MIT