# ng-tester

> A library containing structured, opinionated Angular testing schematics.

Latest version **9.0.0** (published 2020-03-16) · SEE LICENSE IN LICENSE.txt license · 0 weekly downloads

## Install

```sh
npm install ng-tester
pnpm add ng-tester
yarn add ng-tester
bun add ng-tester
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 9.0.0 |
| Published | 2020-03-16 |
| First published | 2020-02-22 |
| Weekly downloads | 0 |
| License | SEE LICENSE IN LICENSE.txt |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 85.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | bjsawyer |
| Keywords | Angular, Schematics, Unit, Testing |

## Links

- npm: https://www.npmjs.com/package/ng-tester
- Repository: https://github.com/bjsawyer/ng-tester
- Homepage: https://github.com/bjsawyer/ng-tester#readme
- Issues: https://github.com/bjsawyer/ng-tester/issues
- npm.io page: https://npm.io/package/ng-tester

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 9.0.0 (latest) — 2020-03-16
- 1.0.3 — 2020-03-10
- 1.0.2 — 2020-03-10
- 1.0.1 — 2020-02-22
- 1.0.0 — 2020-02-22

## README

# ng-tester ![npm](https://img.shields.io/npm/v/ng-tester)

`ng-tester` is an Angular library designed to create structured, opionated spec files for components. It incorporates [angular-unit-test-helper](https://www.npmjs.com/package/angular-unit-test-helper) to assist with mocking.

Each spec file is created with two subsections:

### "DOM Tests"

- Contains Angular TestBed
- Designed for tests that check state changes (i.e. `fixture.detectChanges()`) or require DOM access (i.e. testing UI elements)
- Runs slower than functional tests (however [as of Angular v9](https://blog.angular.io/version-9-of-angular-now-available-project-ivy-has-arrived-23c97b63cfa3), components are no longer recompiled for each test, significantly improving test run speed from previous Angular versions)

### "Functional Tests"

- Designed for functional logic testing
- Runs faster than "DOM Tests", since the DOM is not created for these tests

## Usage

1. Run `npm install -D ng-tester`
1. Navigate to project/directory you'd like to generate spec file
1. Run `ng generate ng-tester:unit`

## Output

Below is the `test.component.spec.ts` file generated for the command `ng g ng-tester:unit --name=test`:

```ts
import { ComponentFixture, TestBed, async } from '@angular/core/testing'
import {
  addProperty,
  addPropertyAsBehaviorSubject,
  autoSpyObj,
  createComponentMock,
  getAllFunctions,
  getAllProperties,
  injectClass,
  injectSpy
} from 'angular-unit-test-helper'

import { TestComponent } from './test.component'

describe('TestComponent', () => {
  let component: TestComponent
  // Declare service mocks
  let fakeServiceSpy: jasmine.SpyObj<FakeService>

  beforeEach(() => {
    // Initialize mocked services from actual services
    fakeServiceSpy = autoSpyObj(FakeService)
  })

  describe('DOM Tests', () => {
    let fixture: ComponentFixture<TestComponent>

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [TestComponent],
        imports: [],
        providers: [
          // Inject mocked services into TestBed
          { provide: FakeService, useValue: fakeServiceSpy }
        ],
      }).compileComponents()
    }))

    beforeEach(() => {
      // Get injected dependencies from TestBed
      fakeServiceSpy = injectSpy(FakeService)

      fixture = TestBed.createComponent(TestComponent)
      component = fixture.componentInstance
    })

    it('should create', () => {
      fixture.detectChanges()
      expect(component).toBeTruthy()
    })

    it('test goes here', () => {
      // arrange


      // act


      // assert

    })
  })

  describe('Functional Tests', () => {
    beforeEach(() => {
      // Create component instance with mocked services
      component = new TestComponent(fakeServiceSpy)
    })

    it('test goes here', () => {
      // arrange


      // act


      // assert

    })
  })
})
```

## Peer Dependencies

- @angular/common: >= 9.0.0
- @angular/core: >= 9.0.0
- angular-unit-test-helper: >= 9.3.0
- tslib: >= 1.10.0

## Testing Schematic Locally

1. In `ng-tester` directory, run `npm run package`
1. Navigate to project/directory you'd like to generate spec file
1. Install `.tgz` file created in step 1 as a devDependency (i.e. `npm i ../ng-tester/dist/ng-tester/ng-tester-9.0.0.tgz -D`)
1. Run `ng generate ng-tester:unit`

---
_Source: https://npm.io/package/ng-tester · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
