From 326c66825b1b3b117cdad968efdd28e7b17c2f20 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 2 Feb 2021 14:43:46 +0000 Subject: [PATCH 1/2] fix: fix async sharding tests `open - empty` was failing with an uncaught exception but this wasn't failing mocha or karma. Found this problem testing playwright-test in this repo. --- src/sharding.js | 3 +++ test/sharding.spec.js | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/sharding.js b/src/sharding.js index b0a722a..30715fe 100644 --- a/src/sharding.js +++ b/src/sharding.js @@ -100,6 +100,9 @@ class ShardingDatastore extends Adapter { */ static async create (store, shard) { const hasShard = await store.has(shardKey) + if (!hasShard && !shard) { + throw new Error('Shard is required when datastore doesn\'t have a shard key already.') + } if (!hasShard) { // @ts-ignore i have no idea what putRaw is or saw any implementation const put = typeof store.putRaw === 'function' ? store.putRaw.bind(store) : store.put.bind(store) diff --git a/test/sharding.spec.js b/test/sharding.spec.js index bc2758f..ed60f4b 100644 --- a/test/sharding.spec.js +++ b/test/sharding.spec.js @@ -21,21 +21,21 @@ describe('ShardingStore', () => { expect(utf8Decoder.decode(res[1])).to.eql(sh.readme) }) - it('open - empty', async () => { + it('open - empty', () => { const ms = new MemoryDatastore() // @ts-expect-error const store = new ShardingStore(ms) - expect(store.open()) + return expect(store.open()) .to.eventually.be.rejected() - .with.property('code', 'ERR_NOT_FOUND') + .with.property('message', 'Shard is required when datastore doesn\'t have a shard key already.') }) - it('open - existing', async () => { + it('open - existing', () => { const ms = new MemoryDatastore() const shard = new sh.NextToLast(2) const store = new ShardingStore(ms, shard) - expect(store.open()).to.eventually.be.fulfilled() + return expect(store.open()).to.eventually.be.fulfilled() }) it('basics', async () => { From 1cbdcc358e632119fcd4c04a4cd6ef72b6b37a6c Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Tue, 2 Feb 2021 15:19:19 +0000 Subject: [PATCH 2/2] fix: change error --- src/sharding.js | 4 ++-- test/sharding.spec.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/sharding.js b/src/sharding.js index 30715fe..15b5715 100644 --- a/src/sharding.js +++ b/src/sharding.js @@ -1,6 +1,6 @@ 'use strict' -const { Adapter, Key, utils: { utf8Encoder } } = require('interface-datastore') +const { Adapter, Key, utils: { utf8Encoder }, Errors } = require('interface-datastore') const sh = require('./shard') const KeytransformStore = require('./keytransform') @@ -101,7 +101,7 @@ class ShardingDatastore extends Adapter { static async create (store, shard) { const hasShard = await store.has(shardKey) if (!hasShard && !shard) { - throw new Error('Shard is required when datastore doesn\'t have a shard key already.') + throw Errors.dbOpenFailedError(Error('Shard is required when datastore doesn\'t have a shard key already.')) } if (!hasShard) { // @ts-ignore i have no idea what putRaw is or saw any implementation diff --git a/test/sharding.spec.js b/test/sharding.spec.js index ed60f4b..f037bc1 100644 --- a/test/sharding.spec.js +++ b/test/sharding.spec.js @@ -27,7 +27,7 @@ describe('ShardingStore', () => { const store = new ShardingStore(ms) return expect(store.open()) .to.eventually.be.rejected() - .with.property('message', 'Shard is required when datastore doesn\'t have a shard key already.') + .with.property('code', 'ERR_DB_OPEN_FAILED') }) it('open - existing', () => {