Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

Update tests

Ondra Urban1bf3b25b0e58
diff --git a/src/key_value_store.js b/src/key_value_store.js
index 4fbe8a05f..30c31b25c 100644
--- a/src/key_value_store.js
+++ b/src/key_value_store.js
@@ -282,17 +282,18 @@ export class KeyValueStoreLocal {
const result = results.filter(r => r && r.returnValue !== null);
return result.length
? result[0]
- : this._fullDirectoryLookup(handler);
+ : this._fullDirectoryLookup(key, handler);
});
}
/**
* Performs a lookup for a file in the local emulation directory's file list.
+ * @param {String} key
* @param {Function} handler
* @returns {Promise}
* @ignore
*/
- _fullDirectoryLookup(handler) {
+ _fullDirectoryLookup(key, handler) {
return readdirPromised(this.localEmulationPath)
.then((files) => {
const regex = getFileNameRegexp(key);
@@ -300,8 +301,7 @@ export class KeyValueStoreLocal {
return fileName
? handler(this._getPath(fileName)).then(returnValue => ({ returnValue, fileName }))
: null;
- })
- .catch(() => null);
+ });
}
/**
diff --git a/test/key_value_store.js b/test/key_value_store.js
index b37eb5979..fb907d93b 100644
--- a/test/key_value_store.js
+++ b/test/key_value_store.js
@@ -60,6 +60,8 @@ describe('KeyValueStore', () => {
await store.setValue('key-buffer', buffer, { contentType: 'image/jpeg' });
await store2.setValue('key-obj', { foo: 'hotel' });
await store2.setValue('key-string', 'yyyy', { contentType: 'text/plain' });
+ await store2.setValue('key-ctype', buffer, { contentType: 'video/mp4' });
+ await store2.setValue('key-badctype', buffer, { contentType: 'nonexistent/content-type' });
// Try to read store2/key-string.
expect(await store2.getValue('key-string')).to.be.eql('yyyy');
@@ -69,20 +71,26 @@ describe('KeyValueStore', () => {
await store2.setValue('key-string', null, { contentType: 'text/plain' });
throw new Error('This should throw!!!');
} catch (err) {
- expect(err).to.be.a('error');
+ expect(err).to.be.an('error');
+ expect(err.message).not.to.include('This should throw!!!');
}
- // Try to delete store2/key-string again.
+ // Check that it still exists.
expect(await store2.getValue('key-string')).to.be.eql('yyyy');
- // Check that it doesn't exist.
+ // Try to delete store2/key-string again.
await store2.setValue('key-string', null);
+ // Check that it doesn't exist.
+ expect(await store2.getValue('key-string')).to.be.eql(null);
+
expect(await store.getValue('key-obj')).to.be.eql({ foo: 'bar' });
expect(await store.getValue('key-string')).to.be.eql('xxxx');
expect(await store.getValue('key-buffer')).to.be.eql(buffer);
expect(await store.getValue('key-nonexist')).to.be.eql(null);
expect(await store2.getValue('key-obj')).to.be.eql({ foo: 'hotel' });
+ expect(await store2.getValue('key-ctype')).to.be.eql(buffer);
+ expect(await store2.getValue('key-badctype')).to.be.eql(buffer);
// Delete works.
const storeDir = path.join(LOCAL_EMULATION_DIR, LOCAL_EMULATION_SUBDIR, 'my-store-id');