-1

I have an angular project in which I use jest as test runner.

I use the pipe 'translate' for internationalisation, but in consequence in most of my test spec I have to add:

@Pipe({
    name: 'translate'
})
export class TranslateMockPipe implements PipeTransform {
    public name: string = 'translate';

    public transform(query: string, ...args: any[]): any {
        return query;
    }
}
  declarations: [..., TranslateMockPipe],

My issue is, I have to add this code in all my spec file.

Any idea how I can do it automatically in all my spec? ( maybe by putting it in the setup-jest.ts ? )

sab
  • 4,352
  • 7
  • 36
  • 60
  • You don't have to add that to every test. Just do it once and re-use it. – Liam Sep 28 '21 at 14:46
  • What's *the pipe 'translate'*? Do you mean [angular-l10n](https://github.com/robisim74/angular-l10n) or another library. There is no inbuilt translate pipe in angular – Liam Sep 28 '21 at 14:47
  • 1
    Does this answer your question? [How to mock Pipe when testing Component](https://stackoverflow.com/questions/39293258/how-to-mock-pipe-when-testing-component) – Liam Sep 28 '21 at 14:47

2 Answers2

-2

Below is an example of how I test a pipe that transforms bytes into presentable strings, if that helps.

import { FormatBytesPipe } from '@jfs/shared/pipes/format-bytes.pipe';

describe('Pipe: Format Bytes', () => {
  let pipe: FormatBytesPipe;

  beforeEach(() => {
    pipe = new FormatBytesPipe();
  });

  it('should transform 0 to "0 Bytes"', () => {
    expect(pipe.transform(0)).toEqual('0 Bytes');
  });
  it('should transform 1024 to "1 KB"', () => {
    expect(pipe.transform(Math.pow(1024, 1))).toEqual('1 KB');
  });
  it('should transform 1024^2 to "1 MB"', () => {
    expect(pipe.transform(Math.pow(1024, 2))).toEqual('1 MB');
  });
  it('should transform 1024^3 to "1 GB"', () => {
    expect(pipe.transform(Math.pow(1024, 3))).toEqual('1 GB');
  });
  it('should transform 1024^4 to "1 TB"', () => {
    expect(pipe.transform(Math.pow(1024, 4))).toEqual('1 TB');
  });
  it('should transform 1024^5 to "1 PB"', () => {
    expect(pipe.transform(Math.pow(1024, 5))).toEqual('1 PB');
  });
  it('should transform 1024^6 to "1 EB"', () => {
    expect(pipe.transform(Math.pow(1024, 6))).toEqual('1 EB');
  });
  it('should transform 1024^7 to "1 ZB"', () => {
    expect(pipe.transform(Math.pow(1024, 7))).toEqual('1 ZB');
  });
  it('should transform 1024^8 to "1 YB"', () => {
    expect(pipe.transform(Math.pow(1024, 8))).toEqual('1 YB');
  });
});
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
kottartillsalu
  • 90
  • 1
  • 10
  • Please don't ask questions in answers. If you need clarification you should add a comment when you have enough reputation – Liam Sep 28 '21 at 14:48
  • @Liam Thanks, I wasn't aware I could comment as I couldn't find the button "comment" . I was just trying to help the poster – kottartillsalu Sep 28 '21 at 14:52
-2

You might consider using a third-party testing library like ng-mocks, which allows you to mock easily services, components, pipes and many other things.

Have a look at official documentation on how to mock pipes.

Sergio Mazzoleni
  • 1,458
  • 15
  • 24