Skip to content

Commit c642fa4

Browse files
Test update and resolution for PR comments
1 parent 4a4005b commit c642fa4

File tree

2 files changed

+27
-76
lines changed

2 files changed

+27
-76
lines changed

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export function initializeFastifyPlugin(adapter: AsyncStoreAdapter = AsyncStoreA
9999
* Initialize the async store based on the adapter provided.
100100
*
101101
* @param {AsyncStoreAdapter} [adapter=AsyncStoreAdapter.DOMAIN]
102-
* @returns {(params: AsyncStoreParams) => void}
102+
* @returns {(params: AsyncStoreParams) => any}
103103
*/
104104
export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN) {
105105
if (isInitialized()) {

test/domain.test.ts

Lines changed: 26 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -840,119 +840,70 @@ describe('store: [adapter=DOMAIN]', () => {
840840
});
841841
});
842842

843-
it("should work even if the store is initialized under active domain w/o affecting the existing domain's attributes.", (done) => {
844-
// Existing domain.
845-
const d = domain.create() as any;
846-
847-
d.existingData = 'Hello world';
848-
849-
const callback = () => {
850-
Promise.resolve().then(first).then(second).then(third).then(done).catch(done);
851-
};
852-
853-
const first = () => {
843+
it('should return the response from async callback function.', async () => {
844+
const callback = async () => {
854845
globalStore.set({ foo: 'foo' });
855-
};
856-
857-
const second = () => {
858-
// Store should still have the data set.
859-
expect(globalStore.get('foo')).to.equal('foo');
860-
861-
// And the existing data in the domain before our store
862-
// was initialized should still be there.
863-
expect((process.domain as any).existingData).to.equal('Hello world');
864-
};
865846

866-
const third = () => {
867-
// Ensure the same existing domain is used instead of creating a new one.
868-
expect(process.domain).to.equal(d);
869-
};
847+
functionAccessingStore();
848+
const response = await asyncTask();
870849

871-
d.run(() => {
872-
// Ensure data in the existing domain is available at this point.
873-
expect((process.domain as any).existingData).to.equal('Hello world');
874-
875-
globalStore.initialize(adapter)(callback);
876-
});
877-
});
878-
it("should work even if the store is initialized under active domain w/o affecting the existing domain's attributes.", (done) => {
879-
// Existing domain.
880-
const d = domain.create() as any;
881-
882-
d.existingData = 'Hello world';
883-
884-
const callback = () => {
885-
Promise.resolve().then(first).then(second).then(third).then(done).catch(done);
850+
return response;
886851
};
887852

888-
const first = () => {
889-
globalStore.set({ foo: 'foo' });
890-
};
891-
892-
const second = () => {
893-
// Store should still have the data set.
853+
const functionAccessingStore = () => {
894854
expect(globalStore.get('foo')).to.equal('foo');
895-
896-
// And the existing data in the domain before our store
897-
// was initialized should still be there.
898-
expect((process.domain as any).existingData).to.equal('Hello world');
899855
};
900856

901-
const third = () => {
902-
// Ensure the same existing domain is used instead of creating a new one.
903-
expect(process.domain).to.equal(d);
904-
};
905-
906-
d.run(() => {
907-
// Ensure data in the existing domain is available at this point.
908-
expect((process.domain as any).existingData).to.equal('Hello world');
909-
910-
globalStore.initialize(adapter)(callback);
911-
});
912-
});
913-
914-
it('should return the same response from the callback function.', async () => {
915-
const callback = async () => {
916-
globalStore.set({ foo: 'foo' });
917-
918-
return Promise.resolve('Hello world');
857+
const asyncTask = () => {
858+
return new Promise((resolve) => {
859+
setTimeout(() => {
860+
resolve(globalStore.get('foo'));
861+
}, 1);
862+
});
919863
};
920864

921865
const response = await globalStore.initialize(adapter)(callback);
922-
923-
expect(response).to.equal('Hello world');
866+
expect(response).to.equal('foo');
924867
});
925868
});
926869

927870
describe('Error Handling:', () => {
928871
it('should bubble up the promise rejection from the callback.', async () => {
929-
const callback = async () => {
872+
const callback = () => {
930873
globalStore.set({ foo: 'foo' });
931874

932-
return Promise.reject('Hello world');
875+
return new Promise((resolve, rejects) => {
876+
setTimeout(() => {
877+
rejects('Hello world');
878+
}, 1);
879+
});
933880
};
934881

935882
try {
936883
await globalStore.initialize(adapter)(callback);
884+
expect.fail('Should not reach here.');
937885
} catch (e) {
938886
expect(e).to.equal('Hello world');
939887
}
940888
});
941889

942-
it('should bubble up the error thrown from the callback.', async () => {
943-
const callback = async () => {
890+
it('should bubble up the error thrown from the callback.', (done) => {
891+
const callback = () => {
944892
globalStore.set({ foo: 'foo' });
945893

946894
throw new Error('Hello world');
947895
};
948896

949897
try {
950-
await globalStore.initialize(adapter)(callback);
898+
globalStore.initialize(adapter)(callback);
899+
expect.fail('Should not reach here.');
951900
} catch (e) {
952901
if (e instanceof Error) {
953902
expect(e.message).to.equal('Hello world');
954903
}
955904
}
905+
906+
done();
956907
});
957908
});
958909
});

0 commit comments

Comments
 (0)