How to use prevTest method in backstopjs

Best JavaScript code snippet using backstopjs

add-build.js

Source:add-build.js Github

copy

Full Screen

1// Copyright 2020 Google LLC2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// https://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14/*15* Code to add a build including test cases16* Updates the analytics for the modified test cases17* and builds18* returns analytics object and adds such data to database19*/20const { Firestore } = require('@google-cloud/firestore');21const { TestCaseAnalytics } = require('../lib/analytics.js');22const { v4: uuidv4 } = require('uuid');23const awaitInBatches = require('../lib/promise-util.js');24const AGGREGATE_SIZE = 25;25const RESET_SUCCESS_COUNT = 10;26const api = { addTestRun, addBuild, calculateFlaky, updateQueue, updateTest, RESET_SUCCESS_COUNT };27/*28* Adds a list of TestCaseRun objects to the database, described by the meta information in buildInfo29* Does not assume repository has been initializ30* Particular build MAY NOT be added multiple times with different information, but may be added multiple times with same in31* @param buildInfo must have attributes of organization, timestamp, url, environment, buildId, buildmessage, sha, na32* @param client is the firebase client33* @param collectionName is the collection to use in firestore34*35*/36async function addBuild (testCases, buildInfo, client, collectionName) {37 const dbRepo = client.collection(collectionName).doc(buildInfo.repoId);38 // Create new Buildid this build has already been posted, skip39 buildInfo.buildIdReadable = buildInfo.buildId;40 buildInfo.buildId = uuidv4(); // buildId will always be UUID41 const [computedData] = await Promise.all([updateAllTests(testCases, buildInfo, dbRepo), isMostRecentBuild(buildInfo, dbRepo)]);42 await Promise.all([addBuildDoc(testCases, buildInfo, computedData, dbRepo), updateRepoDoc(buildInfo, dbRepo)]);43}44async function updateAllTests (testCases, buildInfo, dbRepo) {45 // For the test cases46 const metrics = {47 flaky: 0,48 failcount: 0,49 passcount: 0,50 percentpassing: 0,51 buildflaky: 0,52 total: 053 };54 // see if this is the first build for the repository55 const isFirstBuild = await dbRepo.collection('builds').get().then(collectionSnapshot => {56 if (collectionSnapshot.size === 0) {57 return true;58 } else {59 return false;60 }61 });62 const testsToProcess = testCases.map((testCase) => {63 return () => {64 return updateQueue(isFirstBuild, testCase, buildInfo, dbRepo);65 };66 });67 const allTestMetrics = await awaitInBatches(testsToProcess, 50);68 allTestMetrics.forEach(testStatus => {69 const { passed, flaky } = testStatus;70 if (flaky) { metrics.flaky += 1; }71 if (passed) { metrics.passcount += 1; }72 if (!passed) { metrics.failcount += 1; }73 if (!passed && flaky) { metrics.buildflaky += 1; }74 metrics.total += 1;75 });76 metrics.percentpassing = (metrics.total === 0) ? 0 : metrics.passcount / metrics.total;77 return metrics;78}79async function addTestRun (testCase, buildInfo, dbRepo) {80 return dbRepo.collection('queued').doc(testCase.encodedName).collection('runs').doc(buildInfo.buildId).set(81 {82 environment: buildInfo.environment,83 status: testCase.successful ? 'OK' : testCase.failureMessage,84 timestamp: buildInfo.timestamp,85 buildId: buildInfo.buildIdReadable, // as value decoded86 buildIdInternal: buildInfo.buildId,87 sha: decodeURIComponent(buildInfo.sha)88 }89 );90}91async function updateQueue (isFirstBuild, testCase, buildInfo, dbRepo) {92 // see if the test exists in the queue93 const prevTest = await dbRepo.collection('queued').doc(testCase.encodedName).get();94 const cachedSuccess = (prevTest.exists && prevTest.data().cachedSuccess) ? prevTest.data().cachedSuccess : [];95 const cachedFails = (prevTest.exists && prevTest.data() && prevTest.data().cachedFails) ? prevTest.data().cachedFails : [];96 // We should update the queue of tests:97 // 1. if this is the very first build.98 // 2. if there have been less than a threshold of successes in a row.99 // 3. if the testCase is itself a failure.100 if (!testCase.successful || shouldContinueUpdating(prevTest) || isFirstBuild) {101 const testCaseAnalytics = new TestCaseAnalytics(testCase, cachedSuccess, cachedFails, buildInfo, prevTest);102 await api.addTestRun(testCase, buildInfo, dbRepo);103 return api.updateTest(prevTest, testCaseAnalytics, testCase, buildInfo, dbRepo);104 }105 // return metrics106 return {107 flaky: calculateFlaky(prevTest, testCase),108 passed: testCase.successful109 };110}111function shouldContinueUpdating (prevTest) {112 if (!prevTest.exists) return false;113 const test = prevTest.data();114 return (test.flaky || !test.passed || test.subsequentpasses < RESET_SUCCESS_COUNT) &&115 test.hasfailed;116}117function calculateFlaky (prevTest, testCase) {118 if (prevTest.exists && prevTest.data().hasfailed) {119 // test is in the queue and has failed previously120 if (testCase.successful) {121 // currently passing122 if ((prevTest.data().subsequentpasses >= RESET_SUCCESS_COUNT) || (!prevTest.data().shouldtrack)) {123 // NOT FLAKY124 return false;125 } else {126 // FLAKY127 return true;128 }129 } else {130 // currently failing131 if (!prevTest.data().passed) {132 // not flaky, just failing multiple times in a row133 return false;134 } else {135 // FLAKY136 return true;137 }138 }139 } else {140 // test is not in the queue so NOT FLAKY141 return false;142 }143}144async function updateTest (prevTest, testCaseAnalytics, testCase, buildInfo, dbRepo) {145 const failing = !testCase.successful;146 const updateObj = {147 percentpassing: testCaseAnalytics.computePassingPercent(),148 historicaltests: testCaseAnalytics.numberOfHistoricalTests(),149 hasfailed: ((prevTest.exists && prevTest.data().hasfailed) || failing),150 shouldtrack: failing || testCaseAnalytics.calculateTrack(),151 flaky: calculateFlaky(prevTest, testCase),152 passed: testCase.successful,153 failuremessageiffailing: (!testCase.successful) ? testCase.failureMessage : 'None',154 searchindex: failing ? 2 : (calculateFlaky(prevTest, testCase) ? 1 : 0),155 lastupdate: buildInfo.timestamp,156 name: testCase.name,157 subsequentpasses: testCaseAnalytics.incrementSubsequentPasses(prevTest, testCase) === 0 ? 0 : Firestore.FieldValue.increment(1),158 lifetimepasscount: (testCase.successful) ? Firestore.FieldValue.increment(1) : Firestore.FieldValue.increment(0),159 lifetimefailcount: (testCase.successful) ? Firestore.FieldValue.increment(0) : Firestore.FieldValue.increment(1)160 };161 for (const prop in buildInfo.environment) {162 updateObj['environments.' + prop] = Firestore.FieldValue.arrayUnion(buildInfo.environment[prop]);163 }164 const firestoreTimestamp = new Firestore.Timestamp(Math.floor(buildInfo.timestamp.getTime() / 1000), 0);165 if (testCase.successful) {166 updateObj.cachedSuccess = Firestore.FieldValue.arrayUnion(firestoreTimestamp);167 } else {168 updateObj.cachedFails = Firestore.FieldValue.arrayUnion(firestoreTimestamp);169 }170 await dbRepo.collection('queued').doc(testCase.encodedName).update(updateObj, { merge: true });171 while (testCaseAnalytics.deleteQueue.length > 0) {172 const deleteMe = testCaseAnalytics.deleteQueue.pop();173 const deleteObj = {};174 const deleteDate = new Firestore.Timestamp(Math.floor(deleteMe.date.getTime() / 1000), 0);175 if (deleteMe.passed) {176 deleteObj.cachedSuccess = Firestore.FieldValue.arrayRemove(deleteDate);177 } else {178 deleteObj.cachedFails = Firestore.FieldValue.arrayRemove(deleteDate);179 }180 await dbRepo.collection('queued').doc(testCase.encodedName).update(deleteObj, { merge: true });181 }182 return {183 flaky: calculateFlaky(prevTest, testCase),184 passed: testCase.successful185 };186}187async function isMostRecentBuild (buildInfo, dbRepo) {188 const curRepo = await dbRepo.get();189 if (!curRepo.exists) {190 return true;191 }192 return buildInfo.timestamp.getTime() >= curRepo.data().lastupdate.toDate().getTime();193}194async function addBuildDoc (testCases, buildInfo, computedData, dbRepo) {195 // For the Builds196 const alltests = testCases.map(x => x.encodedName); // not currently used in MVP but would be useful for finding all tests in a build197 await dbRepo.collection('builds').doc(buildInfo.buildId).set({198 percentpassing: computedData.percentpassing,199 passcount: computedData.passcount,200 failcount: computedData.failcount,201 environment: buildInfo.environment,202 timestamp: buildInfo.timestamp,203 tests: alltests,204 sha: buildInfo.sha,205 buildId: buildInfo.buildIdReadable,206 buildIdInternal: buildInfo.buildId,207 buildmessage: buildInfo.buildmessage,208 flaky: computedData.buildflaky209 });210}211async function updateRepoDoc (buildInfo, dbRepo) {212 const repoId = decodeURIComponent(buildInfo.repoId);213 const repoUpdate = {214 organization: buildInfo.organization,215 url: buildInfo.url,216 repoId: repoId,217 name: buildInfo.name,218 description: buildInfo.description,219 lower: {220 repoId: decodeURIComponent(buildInfo.repoId).toLowerCase(),221 name: buildInfo.name.toLowerCase(),222 organization: buildInfo.organization.toLowerCase()223 }224 };225 for (const prop in buildInfo.environment) {226 repoUpdate['environments.' + prop] = Firestore.FieldValue.arrayUnion(buildInfo.environment[prop]);227 }228 // The top level repository object tracks aggregate test stats from the last229 // AGGREGATE_SIZE test runs:230 // TODO: we should consider moving towards calculating this daily to save231 // on read operations.232 const queuedTestLookup = await dbRepo233 .collection('queued')234 .orderBy('searchindex', 'desc')235 .orderBy('lastupdate', 'desc')236 .offset(0)237 .limit(AGGREGATE_SIZE)238 .get();239 const queuedTests = queuedTestLookup.docs.map(doc => doc.data());240 const results = queuedTests.reduce((a, test) => {241 if (!test.passed) {242 a.failing++;243 }244 if (test.flaky) {245 a.flaky++;246 }247 return a;248 }, { flaky: 0, failing: 0 });249 repoUpdate.flaky = results.flaky;250 repoUpdate.numfails = results.failing;251 repoUpdate.searchindex = results.failing * 10000 + results.flaky;252 repoUpdate.numtestcases = queuedTestLookup.size;253 repoUpdate.lastupdate = buildInfo.timestamp;254 await dbRepo.update(repoUpdate, { merge: true });255}...

Full Screen

Full Screen

main.ts

Source:main.ts Github

copy

Full Screen

1import { assert, expect, use } from 'chai';2import chaiAsPromised from 'chai-as-promised';3use(chaiAsPromised);4let functions: { [fnName: string]: any } = {};5let prevFn: any;6export function test<T extends Function>(asyncFn: T, suite: any) {7 prevFn = asyncFn;8 functions[prevFn.name] = asyncFn;9 return describe(prevFn.name, suite);10};11let prevMessage: string | undefined;12let curTest: Mocha.Test | undefined;13let prevTest: Mocha.Test | undefined;14let beforeTest: { [id: string]: (() => Promise<any>) | undefined } = {};15type Action = keyof ReturnType<typeof given>;16export function given(...input: any) {17 if (prevTest !== curTest) {18 prevMessage = undefined;19 curTest = prevTest;20 }21 const fn = functions[prevFn.name];22 const prevTestId = (prevTest as any)?.id ?? 0;23 const getMsg = (action: Action, expected?: any) => prevMessage ?? givenMessage(action, input, expected);24 return {25 /** Assert a boolean condition. */26 assert: (assertion: (result?: ReturnType<typeof fn>) => {}) => {27 prevTest = it(getMsg('assert', assertion), async () => {28 await beforeTest[prevTestId]?.();29 const result = await fn(...input);30 assert(assertion(result));31 });32 return given(...input);33 },34 /** Callback is called **before** test.35 * @info Called for *one assertion* only.36 * Called after `Mocha.beforeEach`. */37 before: (cb: () => any) => {38 beforeTest[prevTestId] = cb;39 return given(...input);40 },41 /** Assert the expected value deep equals the returned output. */42 expect: (expected: ReturnType<typeof fn>) => {43 prevTest = it(getMsg('expect', expected), async () => {44 await beforeTest[prevTestId]?.();45 const result = await fn(...input);46 expect(result).to.deep.equal(expected);47 });48 return given(...input);49 },50 /** Assert that a promise is not rejected. */51 resolve: () => {52 prevTest = it(getMsg('resolve'), async () => {53 await beforeTest[prevTestId]?.();54 const result = () => fn(...input);55 await expect(result()).not.to.be.rejected;56 });57 return given(...input);58 },59 /** Assert that a promise resolution deep equals a specific value. */60 resolveWith: (expected: ReturnType<typeof fn>) => {61 prevTest = it(getMsg('resolveWith', expected), async () => { 62 await beforeTest[prevTestId]?.();63 const result = await fn(...input);64 expect(result).to.deep.equal(expected);65 });66 return given(...input);67 },68 /** Add a custom message to the next assertion.69 * @info Called for *one assertion* only. */70 message: (content: string) => {71 prevMessage = content;72 return given(...input);73 },74 /** Assert that a promise is rejected. */75 reject: () => {76 prevTest = it(getMsg('reject'), async () => {77 await beforeTest[prevTestId]?.();78 const result = () => fn(...input);79 await expect(result()).to.be.rejected;80 });81 return given(...input);82 },83 /** Assert that a promise is rejected with a specific error message. */84 rejectWith: (errorMessage: string) => {85 prevTest = it(getMsg('rejectWith', errorMessage), async () => {86 await beforeTest[prevTestId]?.(); 87 const result = () => fn(...input);88 await expect(result()).to.be.rejectedWith(errorMessage);89 90 });91 return given(...input);92 },93 /** Assert that an error is thrown. */94 throw: (errorMessage: string) => {95 prevTest = it(getMsg('throw', errorMessage), async () => {96 await beforeTest[prevTestId]?.();97 const result = () => fn(...input);98 expect(result).to.throw(errorMessage);99 });100 return given(...input);101 },102 }103}104function givenMessage(type: Action, input: any, expected?: any): string {105 const given = (input.length) ? JSON.stringify(input) : '()'; 106 return {107 assert: `given: ${given}, assert: ${JSON.stringify(expected)}`,108 before: prevMessage,109 expect: `given: ${given}, return: ${JSON.stringify(expected)}`,110 resolve: `given: ${given}, resolved`,111 resolveWith: `given: ${given}, resolve: ${JSON.stringify(expected)}`,112 message: prevMessage,113 reject: `given: ${given}, rejected`,114 rejectWith: `given: ${given}, reject: ${JSON.stringify(expected)}`,115 throw: `given: ${given}, throw: ${JSON.stringify(expected)}`,116 }[type]!;...

Full Screen

Full Screen

parseBinaryTestList.ts

Source:parseBinaryTestList.ts Github

copy

Full Screen

1import * as R from 'ramda';2export type BinaryTest = {3 bin: string,4 test: string,5 code: string,6};7/**8 * Creates list of binary asm codes to be tested9 *10 * @export11 * @param {string} str12 * @returns {BinaryTest[]}13 */14export function parseBinaryTestList(str: string): BinaryTest[] {15 const truncated = R.compose(16 R.reject(R.isEmpty),17 R.split('\n'),18 )(str);19 return R.reduce(20 (tests, line) => {21 const prevTest = R.last(tests);22 if (line.indexOf(';=') === 0) {23 const colonIndex = line.indexOf(':');24 const [name, value] = [25 line.substr(3, colonIndex - 3),26 R.trim(line.substr(colonIndex + 1)),27 ];28 if (!prevTest || prevTest.code) {29 tests.push(30 <BinaryTest> {31 bin: '',32 test: '',33 code: '',34 [name]: value,35 },36 );37 } else38 prevTest[name] = value;39 } else if (prevTest)40 prevTest.code += `${line}\n`;41 return tests;42 },43 [],44 truncated,45 );...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var backstop = require('backstopjs');2backstop('test', {3}).then(function () {4 backstop('approve', {5 }).then(function () {6 backstop('test', {7 }).then(function () {8 backstop('approve', {9 }).then(function () {10 backstop('test', {11 }).then(function () {12 backstop('approve', {13 }).then(function () {14 backstop('test', {15 }).then(function () {16 backstop('approve', {17 }).then(function () {18 backstop('test', {19 }).then(function () {20 backstop('approve', {21 }).then(function () {22 backstop('test', {23 }).then(function () {24 backstop('approve', {25 }).then(function () {26 backstop('test', {27 }).then(function () {28 backstop('approve', {29 }).then(function () {30 backstop('test', {31 }).then(function () {32 backstop('approve', {33 }).then(function () {34 backstop('test', {35 }).then(function () {36 backstop('approve', {37 }).then(function () {38 backstop('test', {39 }).then(function () {40 backstop('approve', {41 }).then(function () {42 backstop('test', {43 }).then(function () {44 backstop('approve', {45 }).then(function () {46 backstop('test', {47 }).then(function () {48 backstop('approve', {

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = async (page, scenario, vp) => {2 await require('./backstopjs/core/command/prevTest')(page, scenario, vp);3};4module.exports = async (page, scenario, vp) => {5 await page.waitForSelector('body');6 await page.evaluate(() => {7 window.___backstopTools.prevTest();8 });9};10module.exports = async (page, scenario, vp) => {11 await page.waitForSelector('body');12 await page.evaluate(() => {13 window.___backstopTools.prevTest();14 });15};16module.exports = async (page, scenario, vp) => {17 await page.waitForSelector('body');18 await page.evaluate(() => {19 window.___backstopTools.prevTest();20 });21};22module.exports = async (page, scenario, vp) => {23 await page.waitForSelector('body');24 await page.evaluate(() => {25 window.___backstopTools.prevTest();26 });27};28module.exports = async (page, scenario, vp) => {29 await page.waitForSelector('body');30 await page.evaluate(() => {31 window.___backstopTools.prevTest();32 });33};34module.exports = async (page, scenario, vp) => {35 await page.waitForSelector('body');36 await page.evaluate(() => {37 window.___backstopTools.prevTest();38 });39};

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = function (casper, scenario, vp) {2casper.then(function () {3this.evaluate(function () {4window.callPhantom('prevTest');5});6});7};8{9{10}11"paths": {12},13"engineOptions": {14},15}

Full Screen

Using AI Code Generation

copy

Full Screen

1var engine = require('backstopjs');2var config = require('./backstop.json');3engine('test', { config: config })4.then(function () {5})6.catch(function (err) {7});8{9 {10 },11 {12 },13 {14 },15 {16 }17 {18 }19 "paths": {20 },21 "engineOptions": {},22}

Full Screen

Using AI Code Generation

copy

Full Screen

1const Backstop = require('backstopjs');2Backstop('test', {config: './backstop.json', filter: 'test1'})3 .then(() => {4 console.log('Test completed');5 })6 .catch((e) => {7 console.error(e);8 });9{10 {11 },12 {13 },14 {15 }16 {17 },18 {19 }20 "paths": {21 },22 "engineOptions": {

Full Screen

Using AI Code Generation

copy

Full Screen

1module.exports = require('backstopjs/core/command/test')2 .find(o => o._name === 'test')3 .action(function (args, callback) {4 const config = require('backstopjs/core/util/getConfig')();5 const scenarioManager = require('backstopjs/core/util/scenarioManager')(config);6 const scenarioList = scenarioManager.getFilteredScenarioList();7 const engine = require('backstopjs/core/engine')(config, scenarioList);8 engine.test();9 });10"scripts": {11 }

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run backstopjs automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful