UNPKG

1.42 kBPlain TextView Raw
1const Debug: any = require('debug');
2import {each as asyncEach} from 'async';
3let httpsProxyAgent = require('https-proxy-agent');
4
5export class MockgooseHelper {
6 debug: any;
7
8 constructor(public mongoose: any, public mockgoose: any) {
9 this.debug = Debug('MockgooseHelper');
10 }
11
12 setDbVersion(version: string) {{
13 this.mockgoose.mongodHelper.mongoBin.mongoDBPrebuilt.mongoDBDownload.options.version = version;
14 }}
15
16 setProxy(proxy: string) {
17 this.mockgoose.mongodHelper.mongoBin.mongoDBPrebuilt.mongoDBDownload.options.http = {
18 agent: new httpsProxyAgent(proxy)
19 };
20 }
21
22 reset(): Promise<void> {
23 return new Promise<void>((resolve, reject) => {
24 asyncEach(this.mongoose.connections, (connection: any, callback: Function) => {
25 // check if it is mockgoose connection
26 if (!/mockgoose-temp-db-/.test(connection.name)) {
27 return callback();
28 }
29 if ( connection.readyState !== 1 ) {
30 return callback();
31 }
32 connection.dropDatabase((err: any) => {
33 callback();
34 }, (e: any) => {
35 this.debug(`@reset err dropping database ${e}`);
36 callback();
37 });
38 }, (err) => {
39 if ( err ) {
40 this.debug(`@reset err ${err}`);
41 reject();
42 } else {
43 resolve();
44 }
45 })
46 });
47 };
48
49 isMocked(): boolean {
50 return this.mongoose.mocked;
51 }
52
53}
\No newline at end of file