• Stars
    star
    142
  • Rank 251,068 (Top 6 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 6 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Angular6+ Unit Test Generator For Components, Directive, Services, and Pipes

ngentest

Angular5,6,7,8+ Unit Test Generator For Components, Directive, Services, and Pipes

Install & Run

$ npm install ngentest -g # to run this command anywhere
$ ngentest my.component.ts 
$ ngentest my.directive.ts -s # write unit test to my.directive.spec.ts
$ ngentest my.directive.ts -c ../ngentest.config.js # use the given config file.

To see the source file and generated examples, please take a look at examples directory. https://github.com/allenhwkim/ngentest/tree/master/examples

Config

You can override configuration by creating a file named as ngentest.config.js in your application directory and running ngentest from that directory. You can also provide a configuration file using -c my.config.js.

  • framework: jest or karma. The default is jest. This value determines how function mock and assert is to be done.

  • templates: template string for each type. Please specify your own template if you want to override the default template. There are five types;

    • klass: ejs template for an ES6 class without angular decorator.
    • component: ejs template for an Angular component.
    • directive: ejs template for an Angular directive.
    • injectable: ejs template for an Angular service.
    • pipe: ejs template for an Angular pipe.

    e.g.,

    templates: {
      klass: myKlassTemplate,
      component: myComponentTemplate,
      directive: myDirectiveTemplate,
      injectable: myInjectableTemplate, 
      pipe: myPipeTemplate 
    }
  • directives: Array of diretive names, necessary for a component test. e.g.,

    directives: ['myDirective']
  • pipes: Array of pipe names, necessary for a component test. e.g.

    pipes: ['translate', 'phoneNumber', 'safeHtml']
  • replacements: There are some codes, which causes error without proper environment. You need to replace these codes. You can specify from value with regular expression and to value with string. e.g.

    replacements: [
      { from: '^\\S+\\.define\\(.*\\);', to: ''}`
    ]
  • providerMocks: When the following class is used in a constructor parameter, create a mock class with the given statements. e.g.

    providerMocks: {
      ElementRef: ['nativeElement = {};'],
      Router: ['navigate() {};'],
      Document: ['querySelector() {};'],
      HttpClient: ['post() {};'],
      TranslateService: ['translate() {};'],
      EncryptionService: [],
    }
  • includeMatch: When ngentest runs with a directory, include only these files. e.g.,

    includeMatch: [/(component|directive|pipe|service).ts/],
  • excludeMatch: When ngentest runs with a directory, exclude these files. e.g.,

    excludeMatch: [/.*module.ts$/]

Full Example (https://github.com/allenhwkim/ngentest/blob/master/ngentest.config.js)

module.exports = {
  framework: 'karma', // or 'jest'
  templates: {
    klass: klassTemplate,  // ejs contents read from file
    component: componentTemplate,
    directive: directiveTemplate,
    injectable: injectableTemplate, 
    pipe: pipeTemplate 
  },
  // necessary directives used for a component test
  directives: [
    'oneviewPermitted'
  ], 
  // necessary pipes used for a component test
  pipes: [
    'translate', 'phoneNumber', 'safeHtml'
  ],
  // when convert to JS, some codes need to be replaced to work 
  replacements: [ // some 3rd party module causes an error
    { from: 'require\\("html-custom-element"\\)', to: '{}'}, 
    { from: '^\\S+\\.define\\(.*\\);', to: ''} // some commands causes error
  ],
  // when constructor param type is as following, create a mock class with this properties
  // e.g. @Injectable() MockElementRef { nativeElement = {}; }
  providerMocks: {
    ElementRef: ['nativeElement = {};'],
    Router: ['navigate() {};'],
    Document: ['querySelector() {};'],
    HttpClient: ['post() {};'],
    TranslateService: ['translate() {};'],
    EncryptionService: [],
  },
  // when ngentest runs with a directory, include only these files
  includeMatch: [/(component|directive|pipe|service).ts/],
  // when ngentest runs with a directory, exclude these files
  excludeMatch: []
}

How It works

  1. Parse a Typescript file and find these info.

    • imports: imports statements info.
    • inputs: @Input statements info.
    • outputs: @Output statements info.
    • component provider: providers info used in @Component decorator.
    • selector: selector info used in @Component or @Directove decorator.
  2. Compile Typescript to Javascript, then parse the Javascript, and get the following info.

    • constructor param data
    • provider mock data
    • accessor tests
    • function tests
  3. build ejs data from #1 and #2, and generate test code.

For Developers: To make it sure it does not break any feature

Genearate spec files for all examples and compare if there is any difference.

$ sh tools/all-examples.sh
$ git diff

More Repositories

1

angularjs-google-maps

The Simplest AngularJS Google Maps V3 Directive
HTML
1,524
star
2

react-openlayers

OpenLayer React Components
TypeScript
198
star
3

angular-jsdoc

AngularJS Template/Plugin for JSDoc 3.
HTML
148
star
4

angular-template

NodeJS AngularJS Server-Side Template Engine
JavaScript
34
star
5

geo-coder

Geo-Coding/Reverse-Lookup with Various Providers
JavaScript
32
star
6

elements-x

Collection of Customizable Custom Elements
TypeScript
23
star
7

mce

Material-Designed Custom Elements
CSS
19
star
8

ngui-common

Angular 6 Virtual Scroll, Lazy Rendering, Virtual List, Autocomplete, etc
HTML
17
star
9

vue-nuxt-practice

Code Example Of VueJS/Nuxt Implementation
Vue
15
star
10

remoteValidation

rails remote form validation
JavaScript
15
star
11

angularjs-autocomplete

AngularJS Autocomplete
JavaScript
15
star
12

html-custom-elements

Custom elements without any framework.
JavaScript
14
star
13

esbuild-x

esbuild + pre builds + post builds
JavaScript
11
star
14

touch-x

Touch Swipe Library For Mobile/Desktop Devices. With event `x-swipe`, you can build a resizer, a drawer, a slide, sortable, even a game.
HTML
11
star
15

class.extend

Copy/Paste node package implementation of John Resig's javascript object inheritance
JavaScript
9
star
16

touch-ui

Fire touch events; tap, double-tap, drag-enter, drop, and much more
JavaScript
8
star
17

erl_to_ruby

erlang term parser written in ruby
Ruby
8
star
18

jsanimation

Simple Javascript Animation
JavaScript
7
star
19

nextfile.vim

vim script to open related files
Vim Script
7
star
20

webtest

Selenium WebDriverJS Test In Plain English
JavaScript
6
star
21

mock-responses

NodeJS http request middleware with admin interface
SCSS
3
star
22

ngx-module-starter

Angular5+ library development seed using jest, codecept, ngx-packagr
TypeScript
3
star
23

ngd

Collection of Custom Angularjs Directives
JavaScript
2
star
24

gulp-page-json

Generates full index of all pages and pagination files in JSON format
JavaScript
2
star
25

nui-form-for

Graceful AngularJS Validation Injector
JavaScript
2
star
26

webpack-typescript-seed

JavaScript
2
star
27

proctree

Get or show process tree
JavaScript
1
star
28

html-custom-element

HTML5 Custom Element Wrapper (IE11 compatible)
JavaScript
1
star
29

java-itext-html2pdf

Convert HTML to PDF, iText7 + Maven + Spring Boot
HTML
1
star
30

drawpage

Draw outline of your web page and disappear.
HTML
1
star
31

js-template

Rewritten and Improved John Resig's micro javascipt template
JavaScript
1
star
32

play-akka-helloworld

Java
1
star
33

elements-x.new

Custom-built custom elements
TypeScript
1
star
34

waitnrun

start servers and run your test
JavaScript
1
star
35

html5-angular-responsive-layout

CSS
1
star
36

akka-http-helloworld

Akka-HTTP Minimal Example Helloworld
Java
1
star
37

akka-http-api-helloworld

Akka HTTP minimal JSON Request/Response example
Java
1
star
38

loadtesterl

loadtesterl
Erlang
1
star