Skip to content

Commit aa0fbe6

Browse files
author
qeffects
committed
trigger workflows
prettier few changes
1 parent c9772c5 commit aa0fbe6

File tree

1 file changed

+78
-72
lines changed

1 file changed

+78
-72
lines changed

docs/advanced/automated-testing.md

Lines changed: 78 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
---
2-
title: Automated Testing
2+
title: Unit Testing
33
---
44

5-
import { SideBySide } from "@site/src/components/SideBySide";
6-
import { DeprecatedInVersion } from "@site/src/components/DeprecatedInVersion";
5+
## Getting started
76

87
This guide is going to explore unit testing, it’s assuming some familiarity with the subject, but it’s not required.
98

@@ -15,29 +14,31 @@ Our code is very simple for this example.
1514

1615
```typescript title=index.ts
1716
export const libraryFunction = (param: number[], reverse: boolean) => {
18-
let reversed: number[] = [];
17+
let reversed: number[] = [];
1918

20-
if (reverse) {
21-
param.forEach((n) => table.insert(reversed, 1, n));
22-
} else {
23-
reversed = param;
24-
}
19+
if (reverse) {
20+
param.forEach((n) => table.insert(reversed, 1, n));
21+
} else {
22+
reversed = param;
23+
}
2524

26-
return table.concat(reversed, ',');
27-
}
25+
return table.concat(reversed, ",");
26+
};
2827
```
2928

30-
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”.
3130

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?
3334

3435
To start off we’re going to need these tools installed:
3536

3637
https://luarocks.org/
3738
&
3839
https://olivinelabs.com/busted/
3940

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._
4142

4243
Now that we have busted installed and the `busted` command runs in your terminal of choice successfully we can proceed.
4344

@@ -51,6 +52,7 @@ And we need to add busted-tstl types to our tsconfig.json, you should already ha
5152
"lua-types/jit",
5253
]
5354
```
55+
5456
Simply add `"busted-tstl"` :
5557

5658
```json title=tsconfig.json
@@ -62,7 +64,7 @@ Simply add `"busted-tstl"` :
6264
```
6365

6466
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.
6668

6769
Now our project should look something like this:
6870

@@ -73,31 +75,33 @@ Alright, we can start writing our first test. Let’s explore the following exam
7375
```typescript title=index_spec.ts
7476
import { libraryFunction } from "..";
7577

76-
describe('Library Function',() => {
77-
it('Returns unreversed string correctly',() => {
78-
assert.is_equal('1,2,3,4', libraryFunction([1, 2, 3, 4], false));
79-
});
80-
})
78+
describe("Library Function", () => {
79+
it("Returns unreversed string correctly", () => {
80+
assert.is_equal("1,2,3,4", libraryFunction([1, 2, 3, 4], false));
81+
});
82+
});
8183
```
8284

83-
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.
8488

8589
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:
8690

8791
```typescript title=index_spec.ts
8892
import { libraryFunction } from "..";
8993

90-
describe('Library Function',() => {
91-
it('Returns unreversed string correctly',() => {
92-
assert.is_equal('1,2,3,4', libraryFunction([1, 2, 3, 4], false));
93-
});
94-
it('Returns unreversed string correctly',() => {
95-
assert.is_equal('4,3,2,1', libraryFunction([1, 2, 3, 4], true));
96-
});
97-
})
94+
describe("Library Function", () => {
95+
it("Returns unreversed string correctly", () => {
96+
assert.is_equal("1,2,3,4", libraryFunction([1, 2, 3, 4], false));
97+
});
98+
it("Returns reversed string correctly", () => {
99+
assert.is_equal("4,3,2,1", libraryFunction([1, 2, 3, 4], true));
100+
});
101+
});
98102
```
99103

100-
Now hit compile and change your terminal’s directory in to your tstl output folder, for me it’s the `dist` folder.
104+
Now hit compile and change your terminals directory in to your tstl output folder, for me it’s the `dist` folder.
101105
We can now just simply run `busted`, and you should see something like this:
102106

103107
![Project setup](/images/busted-output.png)
@@ -119,6 +123,7 @@ https://docs.github.com/en/actions
119123
So, first we’re going to create a workflow file
120124

121125
Set up a folder structure like so:
126+
122127
```
123128
Root project folder/
124129
.github/
@@ -131,34 +136,34 @@ name: TSTL Testing
131136

132137
on:
133138
push:
134-
branches: [ main ]
139+
branches: [main]
135140
pull_request:
136-
branches: [ main ]
141+
branches: [main]
137142

138143
jobs:
139144
build_tests:
140145
name: Busted Tests
141146
runs-on: ubuntu-latest
142147

143148
steps:
144-
- uses: actions/checkout@v2
145-
- name: Use Node.js [16.x]
146-
uses: actions/setup-node@v2
147-
with:
148-
node-version: 16.x
149-
cache: 'npm'
150-
- name: Npm Install && Build with Testing preset
151-
run: npm install && npm run-script build
152-
- name: Install Lua
153-
uses: leafo/gh-actions-lua@v8
154-
with:
155-
luaVersion: "luajit-2.1.0-beta3"
156-
- name: Install LuaRocks
157-
uses: leafo/gh-actions-luarocks@v4.0.0
158-
- name: Install Busted
159-
run: luarocks install busted
160-
- name: Running Tests
161-
run: cd dist && busted
149+
- uses: actions/checkout@v2
150+
- name: Use Node.js [16.x]
151+
uses: actions/setup-node@v2
152+
with:
153+
node-version: 16.x
154+
cache: "npm"
155+
- name: Npm Install && Build with Testing preset
156+
run: npm install && npm run-script build
157+
- name: Install Lua
158+
uses: leafo/gh-actions-lua@v8
159+
with:
160+
luaVersion: "luajit-2.1.0-beta3"
161+
- name: Install LuaRocks
162+
uses: leafo/gh-actions-luarocks@v4.0.0
163+
- name: Install Busted
164+
run: luarocks install busted
165+
- name: Running Tests
166+
run: cd dist && busted
162167
```
163168
164169
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
196201
}
197202
}
198203
```
199-
*(arrows are to indicate the changes, don't include them in the actual code)*
200204

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`
202208

203209
It will output the new build in to the testing folder and you can move your terminal in to the folder and run busted
204210

@@ -219,34 +225,34 @@ name: TSTL Testing
219225
220226
on:
221227
push:
222-
branches: [ main ]
228+
branches: [main]
223229
pull_request:
224-
branches: [ main ]
230+
branches: [main]
225231
226232
jobs:
227233
build_tests:
228234
name: Busted Tests
229235
runs-on: ubuntu-latest
230236
231237
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
250256
```
251257

252258
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

Comments
 (0)