Best JavaScript code snippet using qawolf
autoupdater.test.js
Source: autoupdater.test.js
1const autoupdater = require("../src/autoupdater");2jest.mock("@actions/core");3async function expectError(callback, expected) {4 let error;5 try {6 await callback();7 } catch (e) {8 error = e;9 }10 expect(error).toEqual(expected);11}12function createOctokitMock({ prs = [], repos, pulls }) {13 const checksMock = {14 listForRef: jest.fn().mockReturnValue(15 new Promise((resolve) =>16 resolve({17 data: {18 check_runs: [],19 },20 })21 )22 ),23 };24 const reposMock = {25 createCommitStatus: jest.fn(),26 getStatusChecksProtection: jest.fn().mockReturnValue(27 new Promise((resolve) =>28 resolve({29 data: { contexts: [] },30 })31 )32 ),33 ...repos,34 };35 const pullsMock = {36 updateBranch: jest.fn(),37 list: jest38 .fn()39 .mockReturnValue(new Promise((resolve) => resolve({ data: prs }))),40 ...pulls,41 };42 return {43 repos: reposMock,44 pulls: pullsMock,45 checks: checksMock,46 };47}48function createGithubMock({ octokit = {}, context = {} } = {}) {49 const contextMock = {50 repo: jest.mock(),51 ...context,52 };53 const octokitMock = {54 pulls: {55 updateBranch: jest.fn(),56 list: jest57 .fn()58 .mockReturnValue(new Promise((resolve) => resolve({ data: [] }))),59 },60 ...octokit,61 };62 return {63 context: contextMock,64 getOctokit: jest.fn().mockReturnValue(octokitMock),65 };66}67describe("autoupdater", () => {68 it("should throw an error if the GITHUB_TOKEN is falsey", () => {69 const githubMock = createGithubMock();70 return expectError(71 () => autoupdater(githubMock),72 new Error("GITHUB_TOKEN must be set")73 );74 });75 it("should make the octokit call to update the correct prs", async () => {76 const repo = { repo: "repo", owner: "owner" };77 const context = { repo, ref: "ref", eventName: "push" };78 const prs = [79 {80 labels: [{ name: "autoupdate" }],81 head: { ref: "head-ref0", sha: "head-sha0" },82 base: { ref: "base-ref0" },83 number: 0,84 },85 {86 labels: [],87 head: { ref: "head-ref1", sha: "head-sha1" },88 base: { ref: "base-ref1" },89 number: 1,90 },91 {92 labels: [{ name: "autoupdate" }, { name: "other" }],93 head: { ref: "head-ref2", sha: "head-sha2" },94 base: { ref: "base-ref2" },95 number: 2,96 },97 {98 labels: [{ name: "other" }],99 head: { ref: "head-ref3", sha: "head-sha3" },100 base: { ref: "base-ref3" },101 number: 3,102 },103 ];104 const octokitMock = createOctokitMock({ prs });105 const githubMock = createGithubMock({ context, octokit: octokitMock });106 await autoupdater(githubMock, { GITHUB_TOKEN: "GITHUB_TOKEN" });107 expect(octokitMock.pulls.list).toHaveBeenCalledTimes(1);108 expect(octokitMock.pulls.list).toHaveBeenCalledWith({109 ...repo,110 base: "ref",111 state: "open",112 });113 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledTimes(2);114 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledWith({115 ...repo,116 pull_number: 0,117 });118 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledWith({119 ...repo,120 pull_number: 2,121 });122 });123 it("should update commit statuses to reflect errors updating", async () => {124 const repo = { repo: "repo", owner: "owner" };125 const context = { repo, ref: "ref", eventName: "push" };126 const prs = [127 {128 labels: [{ name: "autoupdate" }],129 head: { ref: "head-ref0", sha: "head-sha0" },130 base: { ref: "base-ref" },131 number: 0,132 },133 {134 labels: [],135 head: { ref: "head-ref1", sha: "head-sha1" },136 base: { ref: "base-ref" },137 number: 1,138 },139 {140 labels: [{ name: "autoupdate" }, { name: "other" }],141 head: { ref: "head-ref2", sha: "head-sha2" },142 base: { ref: "base-ref" },143 number: 2,144 },145 {146 labels: [{ name: "other" }],147 head: { ref: "head-ref3", sha: "head-sha3" },148 base: { ref: "base-ref" },149 number: 3,150 },151 ];152 const pullsMock = {153 updateBranch: jest154 .fn()155 .mockRejectedValueOnce(new Error("My Error"))156 .mockResolvedValue(),157 };158 const octokitMock = createOctokitMock({ prs, pulls: pullsMock });159 const githubMock = createGithubMock({ context, octokit: octokitMock });160 await autoupdater(githubMock, { GITHUB_TOKEN: "GITHUB_TOKEN" });161 expect(octokitMock.repos.createCommitStatus).toHaveBeenCalledTimes(4);162 // head-sha0 fails163 expect(octokitMock.repos.createCommitStatus).toHaveBeenCalledWith({164 ...repo,165 context: "autoupdate from base-ref",166 description: "Updating",167 sha: "head-sha0",168 state: "pending",169 });170 expect(octokitMock.repos.createCommitStatus).toHaveBeenCalledWith({171 ...repo,172 context: "autoupdate from base-ref",173 description:174 "There was an error. Check the action output for more information.",175 sha: "head-sha0",176 state: "error",177 });178 // head-sha2 success179 expect(octokitMock.repos.createCommitStatus).toHaveBeenCalledWith({180 ...repo,181 context: "autoupdate from base-ref",182 description: "Updating",183 sha: "head-sha2",184 state: "pending",185 });186 expect(octokitMock.repos.createCommitStatus).toHaveBeenCalledWith({187 ...repo,188 context: "autoupdate from base-ref",189 description: "Successfully updated",190 sha: "head-sha2",191 state: "success",192 });193 expect(octokitMock.pulls.list).toHaveBeenCalledTimes(1);194 expect(octokitMock.pulls.list).toHaveBeenCalledWith({195 ...repo,196 base: "ref",197 state: "open",198 });199 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledTimes(2);200 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledWith({201 ...repo,202 pull_number: 0,203 });204 expect(octokitMock.pulls.updateBranch).toHaveBeenCalledWith({205 ...repo,206 pull_number: 2,207 });208 });...
create-status.test.ts
Source: create-status.test.ts
...7 }).then((instance) => {8 octokit = instance;9 });10 });11 it("octokit.rest.repos.createCommitStatus()", () => {12 return Promise.all([13 octokit.rest.repos.createCommitStatus({14 owner: "octokit-fixture-org",15 repo: "create-status",16 sha: "0000000000000000000000000000000000000001",17 state: "failure",18 target_url: "https://example.com",19 description: "create-status failure test",20 context: "example/1",21 }),22 octokit.rest.repos.createCommitStatus({23 owner: "octokit-fixture-org",24 repo: "create-status",25 sha: "0000000000000000000000000000000000000001",26 state: "success",27 target_url: "https://example.com",28 description: "create-status success test",29 context: "example/2",30 }),31 ])32 .then(() => {33 return octokit.rest.repos.listCommitStatusesForRef({34 owner: "octokit-fixture-org",35 repo: "create-status",36 ref: "0000000000000000000000000000000000000001",...
index.ts
Source: index.ts
...7 }8 const statusArgs1 = context.repo({sha: context.payload.after, context: "great-app"});9 const statusArgs2 = context.repo({sha: context.payload.after, context: "great-app-2"});10 const statusArgs3 = context.repo({sha: context.payload.after, context: "great-app-3"});11 await context.octokit.repos.createCommitStatus({state: "pending", description: "started 1", ...statusArgs1});12 await context.octokit.repos.createCommitStatus({state: "pending", description: "started 2", ...statusArgs2});13 await context.octokit.repos.createCommitStatus({state: "pending", description: "started 3", ...statusArgs3});14 await delay(10000);15 await context.octokit.repos.createCommitStatus({state: "success", description: "AOK BOSS 1", ...statusArgs1});16 context.octokit.repos.createCommitStatus({state: "failure", description: "AOK BOSS 2", ...statusArgs2});17 context.octokit.repos.createCommitStatus({state: "success", description: "AOK BOSS 3", ...statusArgs3});18 });19};20function delay(ms: number) {21 return new Promise( resolve => setTimeout(resolve, ms) );...
createCommitStatuses.js
Source: createCommitStatuses.js
1module.exports = async ({ github, context, solutionNamesString }) => {2 console.log(solutionNamesString)3 let solutionNamesArray = solutionNamesString.split(",")4 await solutionNamesArray.forEach(createCommitStatus)5 async function createCommitStatus(solutionName) {6 let contextToUse = "build-deploy-" + solutionName7 await github.rest.repos.createCommitStatus({8 owner: context.repo.owner,9 repo: context.repo.repo,10 sha: context.payload.pull_request.head.sha,11 context: contextToUse,12 state: "pending"13 })14 }...
create-commit-status.js
Source: create-commit-status.js
...17 } catch (error) {18 console.error(error);19 }20};...
Using AI Code Generation
1const { createCommitStatus } = require("qawolf");2const { createCommitStatus } = require("qawolf");3const { createCommitStatus } = require("qawolf");4const { createCommitStatus } = require("qawolf");5const { createCommitStatus } = require("qawolf");6SyntaxError: Unexpected token {7const { createCommitStatus } = require("qawolf");8SyntaxError: Unexpected token {9const { createCommitStatus } = require("qawolf");10SyntaxError: Unexpected token {
Check out the latest blogs from LambdaTest on this topic:
Companies are using DevOps to quickly respond to changing market dynamics and customer requirements.
In today’s tech world, where speed is the key to modern software development, we should aim to get quick feedback on the impact of any change, and that is where CI/CD comes in place.
With the rising demand for new services and technologies in the IT, manufacturing, healthcare, and financial sector, QA/ DevOps engineering has become the most important part of software companies. Below is a list of some characteristics to look for when interviewing a potential candidate.
ChatGPT broke all Internet records by going viral in the first week of its launch. A million users in 5 days are unprecedented. A conversational AI that can answer natural language-based questions and create poems, write movie scripts, write social media posts, write descriptive essays, and do tons of amazing things. Our first thought when we got access to the platform was how to use this amazing platform to make the lives of web and mobile app testers easier. And most importantly, how we can use ChatGPT for automated testing.
With the rise of Agile, teams have been trying to minimize the gap between the stakeholders and the development team.
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!!