-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a suggested way to write unit tests with Mocha/Chai/Sinon? #171
Comments
Hi, no there are no extra tools for that in this orm. But I would like to work in this direction, so if you have suggestions what extra tools should be added to typeorm to improve user experience of writing unit tests - we can discuss and add them. |
I will give it a try and come back with suggestions. Sorry, did close to early. |
Sorry for the delay. I have done a bit of work on the API and I have also done some unit tests. As the requests to the db have been quite simple so far, I have been putting spys on a mock repository and am asserting on the parameters passed to find, findOneById, persist, etc. This works, but I now have to build more complex queries using the query builder. And I am not quite sure where to hook in now to assert what happens. Let's assume that there would be a specific driver (or driver type) specifically designed to be used during unit testing, and let's further assume that there is a place in the code where finally all queries need to be passed through, I'd vote for a possibility to spy on this place to assert on the query object or plain SQL passed through it. In case of a query object, it should be possible to test on fields selected, tables joined, where clauses set, etc. In case of SQL, the SQL could be using a specific or standard SQL dialect which would always return the same results. Does this make sense? |
I need real world examples from you, small as possible to reproduce your issues and try to implement what you are suggesting. Can you collaborate in this direction by providing some repo with such sample? |
Sorry for the delay. I have been crafting an example at https://github.com/Tobias4872/typeorm-unit-testing. Please see the README file for details. |
I've been working on an Express/TypeORM boilerplate recently which uses Mocha and Supertest. |
Thanks, very promising. ///<reference path="./node_modules/@types/node/index.d.ts"/>
///<reference path="./node_modules/@types/chai/index.d.ts"/>
///<reference path="./node_modules/@types/mocha/index.d.ts"/>
import {
Gulpclass
, Task
, SequenceTask
// , MergedTask
} from 'gulpclass';
const gulp = require('gulp');
const del = require('del');
const shell = require('gulp-exec');
// const replace = require('gulp-replace');
// const rename = require('gulp-rename');
// const file = require('gulp-file');
// const uglify = require('gulp-uglify');
const mocha = require('gulp-mocha');
const chai = require('chai');
const tslint = require('gulp-tslint');
const stylish = require('tslint-stylish');
// const sourcemaps = require('gulp-sourcemaps');
const istanbul = require('gulp-istanbul');
const remapIstanbul = require('remap-istanbul/lib/gulpRemapIstanbul');
// const ts = require('gulp-typescript');
const args = require('yargs').argv;
@Gulpclass()
export class Gulpfile
{
/**
* Cleans dist folder.
*/
@Task()
clean(cb: Function) {
return del(['./dist/**', './coverage/**', './coverage-remap/**'], cb);
}
@Task()
compile() {
return gulp.src('package.json', { read: false })
.pipe(shell(['tsc']))
.pipe(shell.reporter());
}
/**
* Runs before test coverage, required step to perform a test coverage.
*/
@Task()
coveragePre() {
return gulp.src(['./dist/server/**/*.js'])
.pipe(istanbul())
.pipe(istanbul.hookRequire());
}
/**
* Runs post coverage operations.
*/
@Task('coveragePost', ['coveragePre'])
coveragePost() {
chai.should();
chai.use(require('sinon-chai'));
chai.use(require('chai-as-promised'));
return gulp.src(['./dist/server/test/*Tests.js'])
.pipe(mocha({
bail: true,
grep: !!args.grep ? new RegExp(args.grep) : undefined,
timeout: 15000
}))
.pipe(istanbul.writeReports());
}
/**
* Shows coverage for TS files instead of JS
*/
@Task()
coverageRemap() {
return gulp.src('./coverage/coverage-final.json')
.pipe(remapIstanbul({
reports: {
'html': './coverage-remap',
'text-summary': null,
'lcovonly': './coverage/lcov.info'
}
}))
.pipe(gulp.dest('./coverage'));
}
/**
* Compiles the code and runs tests + makes coverage report.
*/
@SequenceTask()
tests() {
return ['clean', 'compile', 'coveragePost', 'coverageRemap'
];
}
}
|
@pleerock I've been working on mocking repositories so I can make tests that work on Travis-CI without a DB. Creating a simple IRepository interface has allowed me a lot of flexibility. I can use TypeMoq to automatically create a mock that conforms to the interface, while also allowing me the flexibility of overriding it with test behavior. Perhaps you would consider my pull request: |
I've had some really good luck with the testdouble package. For instance, to integrate with dataloader: // TypeormDataLoader.ts
import * as DataLoader from "dataloader";
import { Repository } from "typeorm";
export const typeOrmDataloader = <T, K extends keyof T>(repository: Repository<T>, keyProperty: K) =>
new DataLoader<T[K], T | undefined>(async (keys) => {
const es = await repository.createQueryBuilder("e")
.where(`e.${keyProperty} IN (:keys)`, { keys })
.getMany();
return keys.map((k) => es.find((e) => e[keyProperty] === k));
}); // TypeormDataLoader.spec.ts
import { expect } from "chai";
import { slow, suite, test, timeout } from "mocha-typescript";
import * as td from "testdouble";
import { Repository, SelectQueryBuilder } from "typeorm";
import { Portal } from "./Portal";
import { typeOrmDataloader } from "./TypeormDataLoader";
@suite(timeout(300), slow(50))
export class TypeormDataLoaderSpec {
public after() {
td.reset();
}
@test public async testSuccess() {
const repo = td.object<Repository<Portal>>("Repository");
const query = td.object<SelectQueryBuilder<Portal>>("SelectQueryBuilder");
td.when(repo.createQueryBuilder("e")).thenReturn(query);
td.when(query.where("e.uuid IN (:keys)", { keys: ["abcdef"] })).thenReturn(query);
td.when(query.getMany()).thenReturn([{ uuid: "abcdef" }]);
const dl = typeOrmDataloader(repo, "uuid");
await expect(dl.load("abcdef")).to.eventually.deep.equal({ uuid: "abcdef" });
}
} |
Let's keep discussion in a single thread. Closing in favour of #1267. |
Hi,
I am currently looking for an ORM to support a REST API written with Koa. I wonder whether there are some examples of how unit testing will work with typeorm let's say with Mocha/Chai/Sinon? Are there probably any mock classes for connections, repositories, etc.? I have scanned the repo and found files for internal testing purposes, but there are not distributed.
Any comment would be appreciated,
Tobias.
The text was updated successfully, but these errors were encountered: