{"id":8551,"date":"2020-09-04T21:17:26","date_gmt":"2020-09-04T21:17:26","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=8551"},"modified":"2020-09-04T21:17:26","modified_gmt":"2020-09-04T21:17:26","slug":"building-and-testing-typescript-code-using-github-actions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/building-and-testing-typescript-code-using-github-actions\/","title":{"rendered":"Building and testing TypeScript code using Github actions"},"content":{"rendered":"<p>In previous posts we&#8217;ve used Github actions to build .NET core code, now let&#8217;s create a workflow to build\/transpile a TypeScript application.<\/p>\n<ul>\n<li>Create a file (mine&#8217;s named build.yml) in your Github repo. in the folder .github\/workflows<\/li>\n<li>As per other examples of such a .yml action file we need to give the build a name and set up the triggers, so let&#8217;s do that, here&#8217;s the code\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nname: Build\r\n\r\non:\r\n  push:\r\n    branches: &#x5B; master ]\r\n  pull_request:\r\n    branches: &#x5B; master ]\r\n\r\njobs:\r\n  build:\r\n\r\n    runs-on: ubuntu-latest\r\n<\/pre>\n<p>In the above we&#8217;ve simply named the action <em>Build<\/em> which is triggered by pushes to master (and pull requests). We then set the action up to run on an ubuntu VM<\/li>\n<li>\n<li>Next we&#8217;ll add the following code to create a strategy (basically think, job configurations and application wide variables)\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nstrategy:\r\n  matrix:\r\n    node-version: &#x5B;12.x]\r\n<\/pre>\n<p>In this case we&#8217;re creating a variable named <em>node-version<\/em> with the version of <em>node<\/em> we want to use.\n<\/li>\n<li>Now we&#8217;ll create the steps for the build\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nsteps:\r\n  - uses: actions\/checkout@v2\r\n  - name: Node.js\r\n    uses: actions\/setup-node@v1\r\n    with:\r\n      node-version: ${{ matrix.node-version }}\r\n  - run: npm install -g yarn    \r\n  - name: yarn install, build and test\r\n    run: | \r\n      yarn \r\n      yarn build\r\n      yarn test\r\n<\/pre>\n<p>Firstly we declare the <em>checkout<\/em> action to be used, then we used the <em>setup-node<\/em> action to (as the name suggests) setup node with the version we defined. In this example we then run <em>npm<\/em> to install <em>yarn<\/em>. Obviously we could have simply used <em>npm<\/em> instead (and you can replace the <em>yarn<\/em> commands with <em>npm<\/em> if you wish).<\/p>\n<p>Finally we have a step to install, build and test our code running the relevant <em>yarn commands<\/em> to carry out these tasks.\n<\/li>\n<\/ul>\n<p>The above expects the package.json of your project to have scripts named <em>test<\/em> and <em>build<\/em>. For example<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n&quot;scripts&quot;: {\r\n  &quot;test&quot;: &quot;jest&quot;,\r\n  &quot;build&quot;: &quot;tsc&quot;\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In previous posts we&#8217;ve used Github actions to build .NET core code, now let&#8217;s create a workflow to build\/transpile a TypeScript application. Create a file (mine&#8217;s named build.yml) in your Github repo. in the folder .github\/workflows As per other examples of such a .yml action file we need to give the build a name and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[304,46,242],"tags":[],"class_list":["post-8551","post","type-post","status-publish","format-standard","hentry","category-github-actions","category-typescript","category-yarn"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=8551"}],"version-history":[{"count":2,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8551\/revisions"}],"predecessor-version":[{"id":8553,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/8551\/revisions\/8553"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=8551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=8551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=8551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}