jmcdo29 / testing-nestjs

A repository to show off to the community methods of testing NestJS including Unit Tests, Integration Tests, E2E Tests, pipes, filters, interceptors, GraphQL, Mongo, TypeORM, and more!
MIT License
2.88k stars 379 forks source link

I can't seem to make the tests fails anywhere so far #2008

Closed Psycokwet closed 10 months ago

Psycokwet commented 10 months ago

Is there an existing issue for this?

Describe the bug

I have bee adding failure expect to tests in simple-sample as well as graphql-sample

        expect(0).toEqual(42);

But the tests keeps not failing. I've also changed 200 to 404 and cats to null, and so far, no attempts at breaking testes have had any effect, which is worrisome for the test actual working state

Minimum reproduction code and/or steps to reproduce

Adding

        expect(0).toEqual(42);

In varous test and pnpm test

For example : simple-test

import { Test, TestingModule } from '@nestjs/testing';
import * as request from 'supertest';
import { AppModule } from '../src/app.module';
import { INestApplication } from '@nestjs/common';

const requestFunction = (url: string, data: string, app: INestApplication) =>
  request(app.getHttpServer()).get(url).expect(200).expect(data);

describe('AppController (e2e)', () => {
  let app: INestApplication;

  beforeEach(async () => {
    const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = module.createNestApplication();
    await app.init();
  });

  describe('/ (GET)', () => {
    it('/ (GET)', () => {
      expect(0).toEqual(42);
      return requestFunction('/', 'Hello, World!', app);
    });
    it('/?name=Tester', () => {
      return requestFunction('/?name=Tester', 'Hello, Tester!', app);
    });
  });
});

Expected behavior

The test should fail after having been tempered with badly

In which operating systems have you tested?

Additional context

No response

jmcdo29 commented 10 months ago

Tests are failing as expected on my end

pnpm nx run simple-sample:test

> testing-nestjs@0.0.1 nx /home/jay/Documents/code/projects/testing-nestjs
> nx "run" "simple-sample:test"

> nx run simple-sample:test

● Validation Warning:

  Unknown option "collectCoverage" with value true was found.
  This is probably a typing mistake. Fixing it will remove this message.

  Configuration Documentation:
  https://jestjs.io/docs/configuration

ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typ
escript-2-7/#easier-ecmascript-module-interoperability for more information.
ts-jest[config] (WARN) message TS151001: If you have issues related to imports, you should consider setting `esModuleInterop` to `true` in your TypeScript configuration file (usually `tsconfig.json`). See https://blogs.msdn.microsoft.com/typescript/2018/01/31/announcing-typ
escript-2-7/#easier-ecmascript-module-interoperability for more information.
 FAIL  apps/simple-sample/src/app.service.spec.ts
  ● AppService › getHello › should fail for being blatantly wrong

    expect(received).toEqual(expected) // deep equality

    Expected: 42
    Received: 20

      21 |     });
      22 |     it('should fail for being blatantly wrong', () => {
    > 23 |       expect(20).toEqual(42);
         |                  ^
      24 |     });
      25 |   });
      26 | });

      at Object.<anonymous> (src/app.service.spec.ts:23:18)

 PASS  apps/simple-sample/src/app.controller.spec.ts
-------------------|---------|----------|---------|---------|-------------------
File               | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
-------------------|---------|----------|---------|---------|-------------------
All files          |     100 |      100 |     100 |     100 |
 app.controller.ts |     100 |      100 |     100 |     100 |
 app.service.ts    |     100 |      100 |     100 |     100 |
-------------------|---------|----------|---------|---------|-------------------

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 4 passed, 5 total
Snapshots:   0 total
Time:        4.251 s
Ran all test suites.

 ————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 >  NX   Ran target test for project simple-sample (5s)

    ✖    1/1 failed
    ✔    0/1 succeeded [0 read from cache]

 ELIFECYCLE  Command failed with exit code 1.

Try using the nx executor runner in the form of pnpm nx run <project-name>:test, e.g. pnpm nx run simple-sample:test.

If you're modifying a test in one of the test directories, you'll need to use the e2e executor instead, pnpm nx run <project-name>:e2e

Psycokwet commented 10 months ago

Ok thank you, the e2e worked, I did change tests in test folder ! I didn't think of changing specs files. Thanks !

jmcdo29 commented 10 months ago

There are e2e tests in the apps/<name>/test directories and unit tests in apps/<name>/src/**/*.spec.ts files