diff --git a/__tests__/rethinkdb-iterable.test.ts b/__tests__/rethinkdb-iterable.test.ts index dbf02ca..29daec8 100644 --- a/__tests__/rethinkdb-iterable.test.ts +++ b/__tests__/rethinkdb-iterable.test.ts @@ -1,84 +1,80 @@ /// -import rethinkdb, { Connection } from "rethinkdb"; +import rethinkdb, { Connection } from 'rethinkdb' -import RethinkdbIterable from ".."; -import _assert from "assert"; +import RethinkdbIterable from '..' +import _assert from 'assert' function assert(val: any, msg: string): asserts val { - _assert(val, msg); + _assert(val, msg) } -describe("RethinkdbIterable", () => { - let index = 0; +describe('RethinkdbIterable', () => { + let index = 0 const ctx: { - conn: Connection | null; - db: string; - intervalId: NodeJS.Timeout | null; - table: string; + conn: Connection | null + db: string + intervalId: NodeJS.Timeout | null + table: string } = { conn: null, - db: "rethinkdb_iterator_test", + db: 'rethinkdb_iterable_test', intervalId: null, - table: "rethinkdb_iterator_test", - }; + table: 'rethinkdb_iterable_test', + } beforeEach(async () => { ctx.conn = await rethinkdb.connect({ - host: "localhost", - db: "", - }); + host: 'localhost', + db: '', + }) await rethinkdb .dbCreate(ctx.db) .run(ctx.conn) .catch((err) => { - if (/exists/.test(err.message)) return; - }); + if (/exists/.test(err.message)) return + }) await rethinkdb .db(ctx.db) .tableCreate(ctx.table) .run(ctx.conn) .catch((err) => { - if (/exists/.test(err.message)) return; - }); + if (/exists/.test(err.message)) return + }) await rethinkdb .db(ctx.db) .table(ctx.table) - .indexCreate("value", rethinkdb.row("value")) + .indexCreate('value', rethinkdb.row('value')) .run(ctx.conn) .catch((err) => { - if (/exists/.test(err.message)) return; - }); - await rethinkdb - .db(ctx.db) - .table(ctx.table) - .indexWait("value") - .run(ctx.conn); - }); + if (/exists/.test(err.message)) return + }) + await rethinkdb.db(ctx.db).table(ctx.table).indexWait('value').run(ctx.conn) + }) afterEach(async () => { if (ctx.conn) { await rethinkdb .dbDrop(ctx.db) .run(ctx.conn) .catch((err) => { - if (/exists/.test(err.message)) return; - }); + if (/exists/.test(err.message)) return + }) await rethinkdb .db(ctx.db) .tableDrop(ctx.table) .run(ctx.conn) .catch((err) => { - if (/exists/.test(err.message)) return; - }); - await ctx.conn.close(); + if (/exists/.test(err.message)) return + }) + await ctx.conn.close() } - }); + }) - describe("rows being created in table on interval", () => { - const activePromises = new Set(); + describe('rows being created in table on interval', () => { + const activePromises = new Set() beforeEach(() => { - assert(ctx.conn != null, "connection is missing"); - const conn = ctx.conn; + assert(ctx.conn != null, 'connection is missing') + const conn = ctx.conn // insert new rows on interval ctx.intervalId = setInterval(() => { const p = rethinkdb @@ -88,34 +84,34 @@ describe("RethinkdbIterable", () => { id: index++, }) .run(conn) - .finally(() => activePromises.delete(p)); - activePromises.add(p); - }, 100); - }); + .finally(() => activePromises.delete(p)) + activePromises.add(p) + }, 100) + }) afterEach(async () => { - if (ctx.intervalId) clearInterval(ctx.intervalId); - await Promise.all(Array.from(activePromises)); - }); + if (ctx.intervalId) clearInterval(ctx.intervalId) + await Promise.all(Array.from(activePromises)) + }) - it("should create an async iterable, iterate through it, and eventually close the cursor", async () => { - assert(ctx.conn != null, "connection is missing"); - const conn = ctx.conn; + it('should create an async iterable, iterate through it, and eventually close the cursor', async () => { + assert(ctx.conn != null, 'connection is missing') + const conn = ctx.conn const cursor = await rethinkdb .db(ctx.db) .table(ctx.table) .changes() - .run(conn); - const iterator = new RethinkdbIterable(cursor); + .run(conn) + const iterable = new RethinkdbIterable(cursor) - let done = false; + let done = false setTimeout(() => { - done = true; - }, 300); + done = true + }, 300) - const results = []; - for await (const val of iterator) { - results.push(val); - if (done) break; + const results = [] + for await (const val of iterable) { + results.push(val) + if (done) break } expect(results).toMatchInlineSnapshot(` Array [ @@ -138,15 +134,15 @@ describe("RethinkdbIterable", () => { "old_val": null, }, ] - `); - }); - }); + `) + }) + }) - describe("one row exists", () => { - const indexValue = "indexValue"; + describe('one row exists', () => { + const indexValue = 'indexValue' beforeEach(async () => { - assert(ctx.conn != null, "connection is missing"); - const conn = ctx.conn; + assert(ctx.conn != null, 'connection is missing') + const conn = ctx.conn // insert new rows on interval await rethinkdb .db(ctx.db) @@ -155,7 +151,7 @@ describe("RethinkdbIterable", () => { id: index++, value: indexValue, }) - .run(conn); + .run(conn) await rethinkdb .db(ctx.db) .table(ctx.table) @@ -163,7 +159,7 @@ describe("RethinkdbIterable", () => { id: index++, value: indexValue, }) - .run(conn); + .run(conn) await rethinkdb .db(ctx.db) .table(ctx.table) @@ -171,25 +167,25 @@ describe("RethinkdbIterable", () => { id: index++, value: indexValue, }) - .run(conn); - }); + .run(conn) + }) afterEach(() => { - if (ctx.intervalId) clearInterval(ctx.intervalId); - }); + if (ctx.intervalId) clearInterval(ctx.intervalId) + }) - it("should create an async iterable that gets row and completes successfully", async () => { - assert(ctx.conn != null, "connection is missing"); - const conn = ctx.conn; + it('should create an async iterable that gets row and completes successfully', async () => { + assert(ctx.conn != null, 'connection is missing') + const conn = ctx.conn // @ts-ignore const cursor = await rethinkdb .db(ctx.db) .table(ctx.table) - .getAll(indexValue, { index: "value" }) - .run(ctx.conn); - const iterator = new RethinkdbIterable(cursor); - const results = []; - for await (const val of iterator) { - results.push(val); + .getAll(indexValue, { index: 'value' }) + .run(ctx.conn) + const iterable = new RethinkdbIterable(cursor) + const results = [] + for await (const val of iterable) { + results.push(val) } expect(results).toMatchInlineSnapshot(` Array [ @@ -206,22 +202,22 @@ describe("RethinkdbIterable", () => { "value": "indexValue", }, ] - `); - }); - }); -}); + `) + }) + }) +}) const toPromise = ( task: (cb: (err?: Error, val?: R) => void) => void ): Promise => { return new Promise((resolve, reject) => { task((...args) => { - const [err, val] = args; - if (err != null) return void reject(err); - if (args.length === 2) return void resolve(val); - resolve(); - }); - }); -}; + const [err, val] = args + if (err != null) return void reject(err) + if (args.length === 2) return void resolve(val) + resolve() + }) + }) +} -export default toPromise; +export default toPromise