Comments (3)
Ah this was an issue of rehabbing the test from integration to e2e as that was the more appropriate term
from testing-nestjs.
Ah, actually, no I see what happened. I removed it because it was being ran with the unit tests and it required the running of a database server. It should have been moved to its own directory with its own test command and jest config, like the e2e tests, I just didn't set that up at the time.
For reference, here was the file
/**
* * Nest Modules
*/
import { Test, TestingModule } from '@nestjs/testing';
import { TypeOrmModule, getRepositoryToken } from '@nestjs/typeorm';
import { getConnection, Repository, DatabaseType } from 'typeorm';
/**
* * Services
*/
import { CatService } from './cat.service';
/**
* * Entities
*/
import { Cat } from './cat.entity';
/**
* * Modules
*/
import { CatModule } from './cat.module';
import { CatDTO } from './cat.dto';
/**
* Casting type as DatabaseType
*/
const postgresDatabase: DatabaseType = 'postgres';
/**
* Database Credentials
*/
const credentials = {
type: postgresDatabase,
host: process.env.POSTGRES_HOST,
port: Number(process.env.POSTGRES_PORT),
username: process.env.POSTGRES_USER,
password: process.env.POSTGRES_PASSWORD,
database: process.env.POSTGRES_DB,
entities: [__dirname + '/**/*.entity.{ts,js}'],
dropSchema: false,
synchronize: process.env.NODE_ENV.trim() !== 'production',
logging: false,
};
/**
* Test cat object
*/
const cat: Partial<CatDTO> = {
name: 'Test Cat Mike',
breed: 'Orange Tabby',
age: 5,
};
/**
* Second test cat object
*/
const cat2: Partial<CatDTO> = {
name: 'Test Cat Mona',
breed: 'Grey Shorthair',
age: 7,
};
describe('Cat Integration Tests', () => {
let service: CatService;
let repo: Repository<Cat>;
beforeAll(async () => {
const module: TestingModule = await Test.createTestingModule({
imports: [
CatModule,
TypeOrmModule.forFeature([Cat]),
TypeOrmModule.forRoot(credentials),
],
providers: [CatService],
}).compile();
service = module.get<CatService>(CatService);
repo = module.get<Repository<Cat>>(getRepositoryToken(Cat));
});
describe('Add', () => {
it('should be able to create a cat', async () => {
// create new cat
const newCat = await service.insertOne(cat);
expect(newCat).toMatchObject({ name: cat.name });
expect(newCat).toMatchObject({ breed: cat.breed });
expect(newCat).toMatchObject({ age: cat.age });
expect(newCat).toBeTruthy();
});
});
describe('Edit', () => {
const testNewName = 'Some Other Cat';
const testNewBreed = 'Grey Tabby';
const testNewAge = 7;
let catDataToUpdate = {
name: testNewName,
breed: testNewBreed,
age: testNewAge,
};
it('should be able to update a cat', async () => {
// create new cat
const newCat = await service.insertOne(cat);
// get new cat's id
const { id } = newCat;
// assign the newly created cat's id into the update object
catDataToUpdate = { ...catDataToUpdate, ...{ id } };
// update cat
const updatedCat = await service.updateOne(catDataToUpdate);
expect(updatedCat).not.toMatchObject({ name: cat.name });
expect(updatedCat).not.toMatchObject({ breed: cat.breed });
expect(updatedCat).not.toMatchObject({ age: cat.age });
expect(updatedCat).toBeTruthy();
});
});
describe('Delete', () => {
it('should be able to update a cat', async () => {
// create new cat
const newCat = await service.insertOne(cat);
// delete cat
const deletedCat = await service.deleteOne(newCat.id);
expect(deletedCat).toMatchObject({ deleted: true });
});
});
describe('Get', () => {
it('should be able to find all cats', async () => {
// create new cat
await service.insertOne(cat);
// create new cat
await service.insertOne(cat2);
const cats = await service.getAll();
expect(cats.length).toEqual(2);
});
});
describe('Get One', () => {
it('should be able to find a cat by id', async () => {
// create new cat
const newCat = await service.insertOne(cat);
const foundCat = await service.getOne(newCat.id);
expect(foundCat.id).toEqual(newCat.id);
});
it('should be able to find a cat by name', async () => {
// create new cat
const newCat = await service.insertOne(cat);
const foundCat = await service.getOneByName(newCat.name);
expect(foundCat.name).toEqual(newCat.name);
});
});
/**
* after each test, delete everything from cat table
*/
afterEach(async () => {
await repo.query(`DELETE FROM cat`);
});
/**
* after all tests are done, delete everything from cat table
*/
afterAll(async () => {
const connection = getConnection();
await connection.createQueryBuilder().delete().from(Cat).execute();
await connection.close();
});
});
from testing-nestjs.
Ah, ok thanks! Yeah, that makes sense.
Regarding naming things, in our project we try to distinguish between integration tests and e2e test in such a way that the integration is testing e.g a service in its smallest possible environment, just like the cat.integration.spec.ts, where as e2e attempt to test the whole app from its public APIs and from the perspective of a user journey..
But I see it's common to call both e2e tests
from testing-nestjs.
Related Issues (20)
- Suggestion: convert testing-nestjs into monorepo with Lerna HOT 3
- [Question] How I mock another service method? HOT 8
- Suggestion: create tests for Prisma (alternative to TypeORM) HOT 7
- [FAILING TEST] Unit testing a NestJS Service that consumes a TypeORM Repository HOT 6
- [NEW TEST] Testing for interceptor failure HOT 3
- [NEW TEST] KnexJs Sample test HOT 1
- Multiple mocked dependencies with the same content HOT 5
- [NEW TEST] Bull HOT 4
- Prisma transactions and rollbacks now available HOT 2
- [NEW TEST] - (Contract Testing) - Incorporate NestJS Consumer Driven Contract testing with nestjs-pact HOT 6
- Test against Solidity smart contracts
- [NEW TEST] Tests for mikro-orm
- [NEW TEST] Sequelize transactions HOT 1
- Add code comment for documenting the tests. HOT 1
- Unable to commit the file in file-up-and-down-sample HOT 1
- Can't push to sidebranch for PR - getting permission denied HOT 1
- I can't seem to make the tests fails anywhere so far HOT 3
- npm install doesn't work
- mongodb-transaction-sample/project.json incorrect jest.config reference HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testing-nestjs.