diff_months: 31

COSC2759 :linting and unit tests Lab Assessment

Download Solution Now
Added on: 2023-03-29 05:34:11
Order Code: 487939
Question Task Id: 0

COSC2759Lab/TutorialWeek4(2023)

Goalsofthislab

  • Buildonexperiencefromweek3slab(GitHubActions)andincorporateknowledgefromweek4slab(automatedtesting).
  • In this lab well cover linting and unit tests. A full CI pipeline (e.g. for assignment 1)wouldincludeadditionaljobs,butyoucanstarttoseeapatternforyoutofollowtofilloutthose

Createyourselfatestrepoforthislab,andcloneit

  1. Log in to GitHub in a web browser, and create a repo under your account. Call itanything,g.`lab4-test`.UndertheheadingInitializethisrepositorywithtickAddaREADMEfile.
  2. Findthesshurlofyournewrepo,itwilllooksomethinglike

`git@github.com:john-doe/lab4-test.git`.

  1. Clonetherepowith`gitclone`and`cd`intothereposworking
  2. PerhapsmakesomechangetotheREADMEfileandpushthemuptoGitHub(viewthem in the GitHub portal) to check you remember how to use git and have it set up(Youcanlookatthelab3notesandthegitcyclevideosformoretips.)

Createafeaturebranch

  1. InthislabweregoingtocreateaGitHubActionsCICreateanewfeaturebranch`gitcheckout-bfeature/add-pipeline`.
  2. PushthenewbranchuptoItwillhavetheexactsamecommitsasmain,butpush it up anyway just as a starting point. (Refer to lab 3 notes for detailedinstructionsifrequired.)

Fillinthesampleapp

YoucanthaveaCIpipelinewithoutanapp.Wewanttokeepthingssimplesowelluseadummyappthatjustaddstwonumberstogether.

  1. Create`sum.js`withthefollowingcontents:

functionsum(a,b){returna+b;

}

module.exports=sum;

  1. Create`sum.test.js`withthefollowingcontents:

constsum=require('./sum');

test('adds1+2toequal3',()=>{expect(sum(1,2)).toBe(3);

});

test('adds3+4toequal7',()=>{expect(sum(3,4)).toBe(7);

});

  1. Create`package.json`withthefollowingcontents:

{

"name":"lab-4-demo",

"version":"1.0.0",

"description":"",

"main":"sum.js","scripts":{

"lint":"eslint.",

"test-unit":"jest"

},

"keywords":[],

"author":"",

"license":"","devDependencies":{

"eslint":"^8.11.0",

"jest":"^27.5.1",

"jest-junit":"^13.0.0"

}

}

  1. Create`.eslintrc.yml`withthefollowingcontents:

env:

node:truecommonjs:truees6:truejest:true

extends:

-eslint:recommendedglobals:

Atomics:readonlySharedArrayBuffer:readonly

parserOptions:ecmaVersion:2018

rules:

no-trailing-spaces:"error"

  1. Create`jest.config.js`withthefollowingcontents:

module.exports={reporters:[

'default',

['jest-junit',{outputDirectory:'reports',outputName:'report.xml'}],

],

};

  1. Finallycreate`.gitignore`withthefollowingcontents:

node_modulesreports

Thiswillensureyoudontpushyourlocalcopyofdependenciesortestreportsintotherepotheydontbelongintherepo.

  1. Run`npminstall`.Thenhavealookatthe`node_modules`Whatdoes

`npminstall`do?

  1. Notethatwedidntrun`npminit`.Whatdoes`npminit`do?

Createthepipelineandaddlinting

  1. FirstwewillrunthelinterRun`npmrunlint`.Whatdoesthisdo?Howdoes

`npm`knowwhattodowhenyoutellittorunlint?

  1. NextweaddthisjobtotheCIInyourrepo,createthefile

`.github/workflows/ci-pipeline.yml`withthefollowingcontents:

name:Lab4CIPipelineon:

push:

branches:

  • mainpull_request:

branches:

  • mainjobs:

lint:

runs-on:ubuntu-lateststeps:

  • uses:actions/checkout@v3
  • name:Usejs18.xuses:actions/setup-node@v3with:

node-version:"18"

  • name:Installdependenciesrun:npmclean-install
  • name:Runlinterrun:npmrunlint

This creates a pipeline with a single job, called lint. The lint job contains severalsteps.Withineachjob,youhavetoinstalltoolssuchasnode,andyouhavetoinstallyourdependencies.EachjobshouldbeassumedtorunonanewVM,soinstallyourtoolsineveryjob,andpushartifactswithinthejobtoo.

  1. Use`gitadd`,`gitcommit`and`gitpush`topackageupyourchangesandcopythemupto
  2. GototheGitHubActionstabandwatchthepipeline
  3. Ifitfails,debug:-)

Unittesting

  1. FirstwewillruntheunittestsRun`npmruntest-unit`.(Doyounoticeapatternbetweenthissectionandtheprevioussection?)
  2. NextweaddthisjobtotheCIIn`.github/workflows/ci-pipeline.yml`,addajobtotheexistingjobssectionontotheend(carefulwiththeindentation):

unit-tests:

runs-on:ubuntu-lateststeps:

  • uses:actions/checkout@v3
  • name:Usejs18.xuses:actions/setup-node@v3with:

node-version:"18"

  • name:Installdependenciesrun:npmclean-install
  • name:Rununittestsrun:npmruntest-unit
  • if:success()||failure()

uses:actions/upload-artifact@v3with:

name:unit-test-${{github.sha}}path:reports/report.xml

Note that we tell GitHub Actions to upload the artifact regardless of successorfailureintheprecedingsteps.Bydefault,pipelinesbailonthefirstsignoftrouble; this is usually what you want. But with things like test reports, youwant to override that default and stash the report telling you about the testfailures. (Of course if your unit tests fail them you dont want to go on to runintegration tests or end-to-end tests. Just bail and give that feedback to thedeveloperASAP.)

  1. Use`gitadd`,`gitcommit`and`gitpush`topackageupyourchangesandcopythemupto
  2. Go to the GitHub Actions tab and watch the pipeline run. You might notice that thelinterandunittestsruninIsthiswhatwewant?Laterinthelabwewilllearnhowtoinsertdependencyrelationshipstoforcejobstoruninaparticularsequence.
  3. Ifitfails,debug:-)
  4. Canyoufindthetestreportartifact?

Disablingjobsinapipeline

  1. Editthelintjobtoaddanif:falseproperty,likeso:

lint:

if:false

runs-on:ubuntu-lateststeps:

  • uses:actions/checkout@v3
  • name:Usejs18.xuses:actions/setup-node@v3with:

node-version:"18"

  • name:Installdependenciesrun:npmclean-install
  • name:Runlinterrun:npmrunlint
  1. Use`gitadd`,`gitcommit`and`gitpush`topackageupyourchangesandcopythemupto
  2. GototheGitHubActionstabandwatchthepipelineWhathappenedtothelintjob?
  3. Thinkaboutwhatotherconditionsyoumightputintotheifpropertytocontrolyourpipeline
  4. Beforemovingon,revertthechangeyoumadetodisablethelint(Ifyoudiditas a separate commit, you can `git revert` which is handy.) Were removing thedisabling. Double negatives. Wow. Can I make that more confusing? Recursion!Summary:wewantthelintertostartrunningagain!:-)

Makingajobdependentonanother

  1. Maketheunittestsdependentonthelinterbyaddinganeeds:lintpropertytotheunittestjob,likeso:

unit-tests:

needs:lint

runs-on:ubuntu-lateststeps:

  • uses:actions/checkout@v3
  • name:Usejs18.xuses:actions/setup-node@v3with:

node-version:"18"

  • name:Installdependenciesrun:npmclean-install
  • name:Rununittestsrun:npmruntest-unit
  • if:success()||failure()

uses:actions/upload-artifact@v3with:

name:unit-test-${{github.sha}}path:reports/report.xml

  1. Use`gitadd`,`gitcommit`and`gitpush`topackageupyourchangesandcopythemupto
  2. GototheGitHubActionstabandwatchthepipelineHowdoesthispipelinerunlookdifferenttopreviousruns?

Failurescenarios

  1. Canyoumakethelinterfailinthepipeline,justbychanging`sum.js`?
  2. Canyoumaketheunittestsfailinthepipeline,justbychanging`sum.js`?
  3. CanyoucrashGitHubActionsandgetrootonaproductionserverinsideMicrosoft?(OK this last one is a joke, before you do anything like that please seehttps://bounty.github.com/#rules)
  • Uploaded By : Katthy Wills
  • Posted on : March 29th, 2023
  • Downloads : 0
  • Views : 445

Download Solution Now

Can't find what you're looking for?

Whatsapp Tap to ChatGet instant assistance

Choose a Plan

Premium

80 USD
  • All in Gold, plus:
  • 30-minute live one-to-one session with an expert
    • Understanding Marking Rubric
    • Understanding task requirements
    • Structuring & Formatting
    • Referencing & Citing
Most
Popular

Gold

30 50 USD
  • Get the Full Used Solution
    (Solution is already submitted and 100% plagiarised.
    Can only be used for reference purposes)
Save 33%

Silver

20 USD
  • Journals
  • Peer-Reviewed Articles
  • Books
  • Various other Data Sources – ProQuest, Informit, Scopus, Academic Search Complete, EBSCO, Exerpta Medica Database, and more