1

I'd like to be able to test an Angular2 pipe purely in nodejs environment without including karma etc.

It is possible to use typescript files as test suites for mocha

https://templecoding.com/blog/2016/05/05/unit-testing-with-typescript-and-mocha/

But when I have a import {Pipe} from '@angular/core' it gives me

/Users/foo/node_modules/@angular/core/src/util/decorators.js:173
        throw 'reflect-metadata shim is required when using class decorators';
        ^
reflect-metadata shim is required when using class decorators

Even if I write require('reflect-metadata') in my test file it still breaks with the same error.

Angular internally has this check:

(function checkReflect() {
    if (!(Reflect && Reflect.getMetadata)) {
        throw 'reflect-metadata shim is required when using class decorators';
    }
})();

And after requireing reflect-matadata I indeed have Reflect on the global object, however it still doesn't work...

Anyway is there a way to test an Angular pipe purley in nodejs with mocha?

I'm using webpack to bundle the app so the file I'm requring in my test file looks like this:

import {Pipe} from '@angular/core';

@Pipe({
    name: 'filterBy'
})

export class FilterByPipe {
  transform(items = [], prop, val) {
    return items.filter(someFilteringAlgorithm(prop, val));
  }
}
kamilkp
  • 9,690
  • 6
  • 37
  • 56

1 Answers1

0

I didn't test pipes yet, but here a post that explain how to test Angular 2 application in Node with Mocha. But It use Webpack instead of ts-node : Here

Hope It can help.

HichamBI
  • 201
  • 2
  • 6