You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So our function takes an array of numbers like “[1,2,3,4]” and returns a string like “1,2,3,4” or “4,3,2,1”This is where unit testing comes in, say a piece of code depends on this function working like it does, and for it to keep working exactly like it does right now, obviously you could use the tried and true method of doing the testing yourself, but unit testing provides an alternative to the manual work.
29
+
So our function takes an array of numbers like “[1,2,3,4]” and returns a string like “1,2,3,4” or “4,3,2,1”.
31
30
32
-
So, automated testing, where do we begin?
31
+
This is where unit testing comes in, say a piece of code depends on this function working like it does, and for it to keep working exactly like it does right now, obviously you could use the tried and true method of doing the testing yourself, but unit testing provides an alternative to the manual work.
32
+
33
+
So, unit testing, where do we begin?
33
34
34
35
To start off we’re going to need these tools installed:
35
36
36
37
https://luarocks.org/
37
38
&
38
39
https://olivinelabs.com/busted/
39
40
40
-
*Luarocks is known to be hard to impossible to install on Windows systems sucessfully, if that’s the case for you, definitely keep reading on, we’re gonna explore a (free) option to do away with running tests locally entirely.*
41
+
_Luarocks is known to be hard to impossible to install on Windows systems sucessfully, if that’s the case for you, definitely keep reading on, we’re gonna explore a (free) option to do away with running tests locally entirely._
41
42
42
43
Now that we have busted installed and the `busted` command runs in your terminal of choice successfully we can proceed.
43
44
@@ -51,6 +52,7 @@ And we need to add busted-tstl types to our tsconfig.json, you should already ha
51
52
"lua-types/jit",
52
53
]
53
54
```
55
+
54
56
Simply add `"busted-tstl"` :
55
57
56
58
```json title=tsconfig.json
@@ -62,7 +64,7 @@ Simply add `"busted-tstl"` :
62
64
```
63
65
64
66
Now let’s set up a folder for our tests, by default busted takes a folder named `spec` and runs the files in there, now
65
-
this is personal preference, but usually I name my tests `[filename to be tested]_spec.ts`, the _spec suffix is once again what busted searches for by default, so we’ll stick to that.
67
+
this is personal preference, but usually I name my tests `[filename to be tested]_spec.ts`, the \_spec suffix is once again what busted searches for by default, so we’ll stick to that.
66
68
67
69
Now our project should look something like this:
68
70
@@ -73,31 +75,33 @@ Alright, we can start writing our first test. Let’s explore the following exam
So the short version of what’s happening there is the ‘describe’ block is our named collection of tests, this block pertains to the library function and library function alone, next we call it(‘’,()=>{}) in there to name and describe the behaviour of a single test, it’s a regular function so you can call and do anything in there. Right here, we’re just using a function in the assert namespace which passes the test if the 2 parameters are exactly equal, and fails if they’re different.
85
+
So the short version of what’s happening there is the ‘describe’ block is our named collection of tests, this collection tests the library function, so we've named it `"Library Function"`
86
+
87
+
Next we call it(‘’,()=>{}) in there to name and describe the behaviour of a single test, it’s a regular function, so you can call and do anything in there. Here, we’re just using a function in the assert namespace which passes the test if the 2 parameters are exactly equal, and fails if they’re different.
84
88
85
89
Alright, so we call our libraryFunction with a set of parameters, and get back some return, then we assert that the return value is equal to a known (correct) value. For illustration let’s add another test, since we have a second signature of the function that returns a reversed string, we can try that:
Without going in to much detail, this workflow triggers whenever you push a commit to your github repository, installs Node.js, Lua, Luarocks and Busted installs all of your project dependencies and compiles a fresh version of the project, at the very end it moves in to the dist folder and runs your busted tests exactly like you would. Now, if you commit and push these changes to your GitHub repository you should be able to see, Under the Actions tab at the top, something like this:
@@ -196,9 +201,10 @@ This section applies to projects described in [Publishing Modules](publishing-mo
196
201
}
197
202
}
198
203
```
199
-
*(arrows are to indicate the changes, don't include them in the actual code)*
200
204
201
-
Now you can use this new tsconfig like: `tstl —project tsconfig.test.json`
205
+
_(arrows are to indicate the changes, don't include them in the actual code)_
206
+
207
+
Now you can use this new tsconfig like: `tstl —-project tsconfig.test.json`
202
208
203
209
It will output the new build in to the testing folder and you can move your terminal in to the folder and run busted
204
210
@@ -219,34 +225,34 @@ name: TSTL Testing
219
225
220
226
on:
221
227
push:
222
-
branches: [main]
228
+
branches: [main]
223
229
pull_request:
224
-
branches: [main]
230
+
branches: [main]
225
231
226
232
jobs:
227
233
build_tests:
228
234
name: Busted Tests
229
235
runs-on: ubuntu-latest
230
236
231
237
steps:
232
-
- uses: actions/checkout@v2
233
-
- name: Use Node.js [16.x]
234
-
uses: actions/setup-node@v2
235
-
with:
236
-
node-version: 16.x
237
-
cache: 'npm'
238
-
- name: Npm Install && Build with Testing preset
239
-
run: npm install && npm run-script test-build
240
-
- name: Install Lua
241
-
uses: leafo/gh-actions-lua@v8
242
-
with:
243
-
luaVersion: "luajit-2.1.0-beta3"
244
-
- name: Install LuaRocks
245
-
uses: leafo/gh-actions-luarocks@v4.0.0
246
-
- name: Install Busted
247
-
run: luarocks install busted
248
-
- name: Running Tests
249
-
run: cd testing && busted
238
+
- uses: actions/checkout@v2
239
+
- name: Use Node.js [16.x]
240
+
uses: actions/setup-node@v2
241
+
with:
242
+
node-version: 16.x
243
+
cache: "npm"
244
+
- name: Npm Install && Build with Testing preset
245
+
run: npm install && npm run-script test-build
246
+
- name: Install Lua
247
+
uses: leafo/gh-actions-lua@v8
248
+
with:
249
+
luaVersion: "luajit-2.1.0-beta3"
250
+
- name: Install LuaRocks
251
+
uses: leafo/gh-actions-luarocks@v4.0.0
252
+
- name: Install Busted
253
+
run: luarocks install busted
254
+
- name: Running Tests
255
+
run: cd testing && busted
250
256
```
251
257
252
258
Now your build should include all the Lua libraries you installed with npm/yarn, include the lualib bundle file and the GitHub action should work.
0 commit comments