Best JavaScript code snippet using ava
RedisPromise.test.js
Source: RedisPromise.test.js
...7const RedisPromise_1 = require("../../lib/redis/RedisPromise");8describe('RedisPromise', function () {9 const redisClient = Redis.createClient();10 const redisPromise = new RedisPromise_1.RedisPromise(redisClient);11 describe('static .promisify()', function () {12 it('simply calls RedisClient.prototype[method] with custom callback', function () {13 const redisStub = Sinon.stub(Redis.RedisClient.prototype, 'get');14 expect(isPromise_1.isPromise(RedisPromise_1.RedisPromise.promisify('get', redisClient, ['test']))).toBe(true);15 expect(redisStub.calledWith('test')).toBe(true);16 expect(typeof redisStub.lastCall.args[1] === 'function').toBe(true);17 expect(redisStub.lastCall.thisValue === redisClient).toBe(true);18 redisStub.restore();19 });20 it('calls Promise.resolve if there is no error', async function () {21 const result = await RedisPromise_1.RedisPromise.promisify('append', redisClient, ['test', 'a']);22 expect(result).toBeGreaterThan(0);23 });24 it('call Promise.reject if there is any error', async function () {25 const redisStub = Sinon.stub(Redis.RedisClient.prototype, 'get');26 try {27 redisStub.callsFake(function (key, done) {28 done(new Error(key));29 });30 await RedisPromise_1.RedisPromise.promisify('get', redisClient, ['test']);31 }32 catch (error) {33 expect(error.message).toEqual('test');34 redisStub.restore();35 return;36 }37 expect('should not reach this line').toEqual('hm');38 });39 });40 function promisify_test_for(facadeMethod, redisMethod, args) {41 describe('.' + facadeMethod + '()', function () {42 it('returns a promise', function () {43 const promisifyStub = Sinon.stub(RedisPromise_1.RedisPromise, 'promisify');44 promisifyStub.callsFake(async function () { });...
RedisPromise.test.ts
Source: RedisPromise.test.ts
...5import { RedisPromise } from '../../lib/redis/RedisPromise'6describe('RedisPromise', function() {7 const redisClient = Redis.createClient()8 const redisPromise = new RedisPromise(redisClient)9 describe('static .promisify()', function() {10 it('simply calls RedisClient.prototype[method] with custom callback', function() {11 const redisStub = Sinon.stub(Redis.RedisClient.prototype, 'get')12 expect(isPromise(RedisPromise.promisify('get', redisClient, ['test']))).toBe(true)13 expect(redisStub.calledWith('test')).toBe(true)14 expect(typeof redisStub.lastCall.args[1] === 'function').toBe(true)15 expect(redisStub.lastCall.thisValue === redisClient).toBe(true)16 redisStub.restore()17 })18 it('calls Promise.resolve if there is no error', async function() {19 const result = await RedisPromise.promisify('append', redisClient, ['test', 'a'])20 expect(result).toBeGreaterThan(0)21 })22 it('call Promise.reject if there is any error', async function() {23 const redisStub = Sinon.stub(Redis.RedisClient.prototype, 'get')24 try {25 redisStub.callsFake(function(key, done) {26 done(new Error(key))27 })28 await RedisPromise.promisify('get', redisClient, ['test'])29 } catch (error) {30 expect(error.message).toEqual('test')31 redisStub.restore()32 return33 }34 expect('should not reach this line').toEqual('hm')35 })36 })37 function promisify_test_for(facadeMethod: string, redisMethod: string, args: any[]) {38 describe('.' + facadeMethod + '()', function() {39 it('returns a promise', function() {40 const promisifyStub = Sinon.stub(RedisPromise, 'promisify')41 promisifyStub.callsFake(async function() {})42 expect(isPromise(redisPromise[facadeMethod](...args))).toBe(true)...
webext.js
Source: webext.js
...80 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage81 storage: {82 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/local83 local: {84 clear: promisify(chrome.storage.local, 'clear'),85 get: promisify(chrome.storage.local, 'get'),86 getBytesInUse: promisify(chrome.storage.local, 'getBytesInUse'),87 remove: promisify(chrome.storage.local, 'remove'),88 set: promisify(chrome.storage.local, 'set'),89 },90 },91 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs92 tabs: {93 get: promisifyNoFail(chrome.tabs, 'get', tab => tab instanceof Object ? tab : null),94 executeScript: promisifyNoFail(chrome.tabs, 'executeScript'),95 insertCSS: promisifyNoFail(chrome.tabs, 'insertCSS'),96 removeCSS: promisifyNoFail(chrome.tabs, 'removeCSS'),97 query: promisifyNoFail(chrome.tabs, 'query', tabs => Array.isArray(tabs) ? tabs : []),98 reload: promisifyNoFail(chrome.tabs, 'reload'),99 remove: promisifyNoFail(chrome.tabs, 'remove'),100 update: promisifyNoFail(chrome.tabs, 'update', tab => tab instanceof Object ? tab : null),101 },102 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webNavigation103 webNavigation: {104 getFrame: promisify(chrome.webNavigation, 'getFrame'),105 getAllFrames: promisify(chrome.webNavigation, 'getAllFrames'),106 },107 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/windows108 windows: {109 get: promisifyNoFail(chrome.windows, 'get', win => win instanceof Object ? win : null),110 create: promisifyNoFail(chrome.windows, 'create', win => win instanceof Object ? win : null),111 update: promisifyNoFail(chrome.windows, 'update', win => win instanceof Object ? win : null),112 },113};114115// browser.privacy entries116{117 const settings = [118 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/network119 [ 'network', 'networkPredictionEnabled' ],120 [ 'network', 'webRTCIPHandlingPolicy' ],121 // https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/privacy/websites122 [ 'websites', 'hyperlinkAuditingEnabled' ],123 ];124 for ( const [ category, setting ] of settings ) {125 let categoryEntry = webext.privacy[category];126 if ( categoryEntry instanceof Object === false ) {127 categoryEntry = webext.privacy[category] = {};128 }129 const settingEntry = categoryEntry[setting] = {};130 const thisArg = chrome.privacy[category][setting];131 settingEntry.clear = promisifyNoFail(thisArg, 'clear');132 settingEntry.get = promisifyNoFail(thisArg, 'get');133 settingEntry.set = promisifyNoFail(thisArg, 'set');134 }135}136137// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/managed138if ( chrome.storage.managed instanceof Object ) {139 webext.storage.managed = {140 get: promisify(chrome.storage.managed, 'get'),141 };142}143144// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/storage/sync145if ( chrome.storage.sync instanceof Object ) {146 webext.storage.sync = {147 QUOTA_BYTES: chrome.storage.sync.QUOTA_BYTES,148 QUOTA_BYTES_PER_ITEM: chrome.storage.sync.QUOTA_BYTES_PER_ITEM,149 MAX_ITEMS: chrome.storage.sync.MAX_ITEMS,150 MAX_WRITE_OPERATIONS_PER_HOUR: chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_HOUR,151 MAX_WRITE_OPERATIONS_PER_MINUTE: chrome.storage.sync.MAX_WRITE_OPERATIONS_PER_MINUTE,152153 clear: promisify(chrome.storage.sync, 'clear'),154 get: promisify(chrome.storage.sync, 'get'),155 getBytesInUse: promisify(chrome.storage.sync, 'getBytesInUse'),156 remove: promisify(chrome.storage.sync, 'remove'),157 set: promisify(chrome.storage.sync, 'set'),158 };159}160161// https://bugs.chromium.org/p/chromium/issues/detail?id=608854162if ( chrome.tabs.removeCSS instanceof Function ) {163 webext.tabs.removeCSS = promisifyNoFail(chrome.tabs, 'removeCSS');164}165166return webext;167168// <<<<< end of private scope
...
awsFunctions.js
Source: awsFunctions.js
...9const sts = new AWS.STS({ apiVersion, region });10const sqs = new AWS.SQS({ apiVersion, region });11const iam = new AWS.IAM({ apiVersion, region });12// lambda13const asyncLambdaCreateFunction = promisify(lambda.createFunction.bind(lambda));14const asyncDeleteFunction = promisify(lambda.deleteFunction.bind(lambda));15const asyncAddPermission = promisify(lambda.addPermission.bind(lambda));16const asyncListEventSourceMappings = promisify(lambda.listEventSourceMappings.bind(lambda));17const asyncDeleteEventSourceMapping = promisify(lambda.deleteEventSourceMapping.bind(lambda));18const asyncCreateEventSourceMapping = promisify(lambda.createEventSourceMapping.bind(lambda));19const asyncPutFunctionConcurrency = promisify(lambda.putFunctionConcurrency.bind(lambda));20const asyncGetFunction = promisify(lambda.getFunction.bind(lambda));21const asyncInvokeLambda = promisify(lambda.invoke.bind(lambda));22// api23const asyncCreateDeployment = promisify(api.createDeployment.bind(api));24const asyncPutMethod = promisify(api.putMethod.bind(api));25const asyncPutIntegration = promisify(api.putIntegration.bind(api));26const asyncCreateApi = promisify(api.createRestApi.bind(api));27const asyncGetResources = promisify(api.getResources.bind(api));28const asyncCreateResource = promisify(api.createResource.bind(api));29const asyncDeleteResource = promisify(api.deleteResource.bind(api));30// ec231const asyncGetRegions = promisify(ec2.describeRegions.bind(ec2));32const asyncCreateKeyPair = promisify(ec2.createKeyPair.bind(ec2));33const asyncRunInstances = promisify(ec2.runInstances.bind(ec2));34const asyncDescribeInstances = promisify(ec2.describeInstances.bind(ec2));35const asyncStopInstances = promisify(ec2.stopInstances.bind(ec2));36const asyncDescribeImages = promisify(ec2.describeImages.bind(ec2));37const asyncDescribeKeyPairs = promisify(ec2.describeKeyPairs.bind(ec2));38const asyncCreateSecurityGroup = promisify(ec2.createSecurityGroup.bind(ec2));39const asyncDescribeVpcs = promisify(ec2.describeVpcs.bind(ec2));40const asyncDescribeSubnets = promisify(ec2.describeSubnets.bind(ec2));41const asyncAuthorizeSecurityGroupIngress = promisify(ec2.authorizeSecurityGroupIngress.bind(ec2));42const asyncTerminateInstances = promisify(ec2.terminateInstances.bind(ec2));43const asyncDeleteSecurityGroup = promisify(ec2.deleteSecurityGroup.bind(ec2));44const asyncDescribeSecurityGroups = promisify(ec2.describeSecurityGroups.bind(ec2));45// sts46const asyncGetCallerIdentity = promisify(sts.getCallerIdentity.bind(sts));47// sqs48const asyncCreateSQS = promisify(sqs.createQueue.bind(sqs));49const asyncDeleteQueue = promisify(sqs.deleteQueue.bind(sqs));50const asyncGetQueueAttributes = promisify(sqs.getQueueAttributes.bind(sqs));51const asyncListQueues = promisify(sqs.listQueues.bind(sqs));52const asyncReceiveMessage = promisify(sqs.receiveMessage.bind(sqs));53const asyncSendMessage = promisify(sqs.sendMessage.bind(sqs));54// iam55const asyncCreateRole = promisify(iam.createRole.bind(iam));56const asyncCreatePolicy = promisify(iam.createPolicy.bind(iam));57const asyncAttachPolicy = promisify(iam.attachRolePolicy.bind(iam));58const asyncListRolePolicies = promisify(iam.listAttachedRolePolicies.bind(iam));59const asyncGetPolicy = promisify(iam.getPolicy.bind(iam));60const asyncGetRole = promisify(iam.getRole.bind(iam));61module.exports = {62 asyncCreateRole,63 asyncAttachPolicy,64 asyncLambdaCreateFunction,65 asyncAddPermission,66 asyncPutMethod,67 asyncPutIntegration,68 asyncCreateApi,69 asyncGetResources,70 asyncGetRegions,71 asyncGetCallerIdentity,72 asyncCreateDeployment,73 asyncCreateKeyPair,74 asyncRunInstances,...
promisify.js
Source: promisify.js
1'use strict';2const util = require('util');3module.exports = function (redisClient) {4 redisClient.async = {5 send_command: util.promisify(redisClient.send_command).bind(redisClient),6 exists: util.promisify(redisClient.exists).bind(redisClient),7 del: util.promisify(redisClient.del).bind(redisClient),8 get: util.promisify(redisClient.get).bind(redisClient),9 set: util.promisify(redisClient.set).bind(redisClient),10 incr: util.promisify(redisClient.incr).bind(redisClient),11 rename: util.promisify(redisClient.rename).bind(redisClient),12 type: util.promisify(redisClient.type).bind(redisClient),13 expire: util.promisify(redisClient.expire).bind(redisClient),14 expireat: util.promisify(redisClient.expireat).bind(redisClient),15 pexpire: util.promisify(redisClient.pexpire).bind(redisClient),16 pexpireat: util.promisify(redisClient.pexpireat).bind(redisClient),17 hmset: util.promisify(redisClient.hmset).bind(redisClient),18 hset: util.promisify(redisClient.hset).bind(redisClient),19 hget: util.promisify(redisClient.hget).bind(redisClient),20 hdel: util.promisify(redisClient.hdel).bind(redisClient),21 hgetall: util.promisify(redisClient.hgetall).bind(redisClient),22 hkeys: util.promisify(redisClient.hkeys).bind(redisClient),23 hvals: util.promisify(redisClient.hvals).bind(redisClient),24 hexists: util.promisify(redisClient.hexists).bind(redisClient),25 hincrby: util.promisify(redisClient.hincrby).bind(redisClient),26 sadd: util.promisify(redisClient.sadd).bind(redisClient),27 srem: util.promisify(redisClient.srem).bind(redisClient),28 sismember: util.promisify(redisClient.sismember).bind(redisClient),29 smembers: util.promisify(redisClient.smembers).bind(redisClient),30 scard: util.promisify(redisClient.scard).bind(redisClient),31 spop: util.promisify(redisClient.spop).bind(redisClient),32 zadd: util.promisify(redisClient.zadd).bind(redisClient),33 zrem: util.promisify(redisClient.zrem).bind(redisClient),34 zrange: util.promisify(redisClient.zrange).bind(redisClient),35 zrevrange: util.promisify(redisClient.zrevrange).bind(redisClient),36 zrangebyscore: util.promisify(redisClient.zrangebyscore).bind(redisClient),37 zrevrangebyscore: util.promisify(redisClient.zrevrangebyscore).bind(redisClient),38 zscore: util.promisify(redisClient.zscore).bind(redisClient),39 zcount: util.promisify(redisClient.zcount).bind(redisClient),40 zcard: util.promisify(redisClient.zcard).bind(redisClient),41 zrank: util.promisify(redisClient.zrank).bind(redisClient),42 zrevrank: util.promisify(redisClient.zrevrank).bind(redisClient),43 zincrby: util.promisify(redisClient.zincrby).bind(redisClient),44 zrangebylex: util.promisify(redisClient.zrangebylex).bind(redisClient),45 zrevrangebylex: util.promisify(redisClient.zrevrangebylex).bind(redisClient),46 zremrangebylex: util.promisify(redisClient.zremrangebylex).bind(redisClient),47 zlexcount: util.promisify(redisClient.zlexcount).bind(redisClient),48 lpush: util.promisify(redisClient.lpush).bind(redisClient),49 rpush: util.promisify(redisClient.rpush).bind(redisClient),50 rpop: util.promisify(redisClient.rpop).bind(redisClient),51 lrem: util.promisify(redisClient.lrem).bind(redisClient),52 ltrim: util.promisify(redisClient.ltrim).bind(redisClient),53 lrange: util.promisify(redisClient.lrange).bind(redisClient),54 llen: util.promisify(redisClient.llen).bind(redisClient),55 };...
Using AI Code Generation
1const test = require('ava');2const fs = require('fs');3const util = require('util');4const readFile = util.promisify(fs.readFile);5test('my passing test', async t => {6 const data = await readFile('test.js');7 t.is(data.length, 32);8});9const test = require('ava');10const fs = require('fs');11test('my passing test', async t => {12 const data = await fs.readFile('test.js');13 t.is(data.length, 32);14});
Using AI Code Generation
1const test = require('ava');2const promisify = require('util').promisify;3const fs = require('fs');4const readFile = promisify(fs.readFile);5test('read file', async t => {6 const data = await readFile('./data.json', 'utf8');7 t.is(data, 'hello');8});9{10 "scripts": {11 },12 "devDependencies": {13 }14}15{16}17{18 "env": {19 },20 "rules": {
Using AI Code Generation
1const test = require('ava');2const {promisify} = require('util');3const {exec} = require('child_process');4const execPromise = promisify(exec);5test('test', async t => {6 const {stdout, stderr} = await execPromise('node test2.js');7 t.is(stdout.trim(), 'test2');8});9console.log('test2');10 6: const {stdout, stderr} = await execPromise('node test2.js');11 7: t.is(stdout.trim(), 'test2');12 8: });13 AssertionError {14 }15 test2.js (1:1)16 1| console.log('test2');17 AssertionError {18 }19 test2.js (1:1)20 1| console.log('test2');
Using AI Code Generation
1const fs = require('fs');2const util = require('util');3const readFile = util.promisify(fs.readFile);4const writeFile = util.promisify(fs.writeFile);5readFile('test.txt', 'utf8')6 .then((data) => {7 console.log(data);8 return writeFile('test2.txt', data);9 })10 .then(() => {11 console.log('File successfully written');12 })13 .catch((err) => {14 console.log(err);15 });16const fs = require('fs');17const util = require('util');18const readFile = util.promisify(fs.readFile);19const writeFile = util.promisify(fs.writeFile);20const stat = util.promisify(fs.stat);21readFile('test.txt', 'utf8')22 .then((data) => {23 console.log(data);24 return writeFile('test2.txt', data);25 })26 .then(() => {27 return stat('test2.txt');28 })29 .then((stats) => {30 console.log(stats);31 })32 .catch((err) => {33 console.log(err);34 });35{ dev: 16777220,
Using AI Code Generation
1const util = require('util');2const fs = require('fs');3const readFile = util.promisify(fs.readFile);4async function read() {5 const data = await readFile('test.txt', 'utf8');6 console.log(data);7}8read();9const promise = new Promise((resolve, reject) => {10 if (/* everything turned out fine */) {11 resolve("Stuff worked!");12 }13 else {14 reject(Error("It broke"));15 }16});17const promise = new Promise((resolve, reject) => {18 resolve('Hello World');19});20 .then((data) => {21 console.log(data);22 })23 .catch((err) => {
Check out the latest blogs from LambdaTest on this topic:
Screenshots! These handy snippets have become indispensable to our daily business as well as personal life. Considering how mandatory they are for everyone in these modern times, every OS and a well-designed game, make sure to deliver a built in feature where screenshots are facilitated. However, capturing a screen is one thing, but the ability of highlighting the content is another. There are many third party editing tools available to annotate our snippets each having their own uses in a business workflow. But when we have to take screenshots, we get confused which tool to use. Some tools are dedicated to taking best possible screenshots of whole desktop screen yet some are browser based capable of taking screenshots of the webpages opened in the browsers. Some have ability to integrate with your development process, where as some are so useful that there integration ability can be easily overlooked.
This article is a part of our Content Hub. For more in-depth resources, check out our content hub on Automation Testing Tutorial.
Working in IT, we have often heard the term Virtual Machines. Developers working on client machines have used VMs to do the necessary stuffs at the client machines. Virtual machines are an environment or an operating system which when installed on a workstation, simulates an actual hardware. The person using the virtual machine gets the same experience as they would have on that dedicated system. Before moving on to how to setup virtual machine in your system, let’s discuss why it is used.
There is no other automation framework in the market that is more used for automating web testing tasks than Selenium and one of the key functionalities is to take Screenshot in Selenium. However taking full page screenshots across different browsers using Selenium is a unique challenge that many selenium beginners struggle with. In this post we will help you out and dive a little deeper on how we can take full page screenshots of webpages across different browser especially to check for cross browser compatibility of layout.
Cross browser compatibility can simply be summed up as a war between testers and developers versus the world wide web. Sometimes I feel that to achieve browser compatibility, you may need to sell your soul to devil while performing a sacrificial ritual. Even then some API plugins won’t work.(XD)
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!