Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add to documentation for writing unit tests #856

Open
dainkaplan opened this issue Apr 28, 2020 · 2 comments
Open

Add to documentation for writing unit tests #856

dainkaplan opened this issue Apr 28, 2020 · 2 comments
Labels

Comments

@dainkaplan
Copy link

@dainkaplan dainkaplan commented Apr 28, 2020

It would be great to see the documentation describe how to write unit tests when the code is written in TS but needing to run in Lua (due to for example dependencies).

I found the following TS definitions for busted (lua unit testing framework) but is this the recommended way to write unit tests?

https://github.com/hazzard993/busted-tstl

@lolleko
Copy link
Member

@lolleko lolleko commented Apr 28, 2020

There is no recommended way.

You have a couple of options:

Write your tests in TS with a TS/JS Test Framework (jest, mocha....)
This assumes our transpiler always produces a correct output that is functional equivalent to the original TS code.
Since good tests test behaviour not implementation, it should not matter if your test runs in TS or Lua.
Therefore this variant can be used for code that does not have a dependency to your lua runtime.

For Example if you use a lua game engine and want to write a dungeon generator. You can write your dungeon generator without using any lua functions declared by the game engine.
Then you can just go ahead and test that dungeon generator in TS via jest etc...

source/
    shared/
        dungeon_generator.ts <- craeates only data structures that hold information about the dungeon (no game engine functions used)
    game/
        dungeon_instance.ts <- uses dungeon generator to create a level (with game engine functions)
test/
    dungeon_generatore.spec.ts <- includes tests for the dungeon generator that can run in TS since it doesnt rely on Lua functios.

Use a Lua test framework
But in practice you usally have places where you cant avoid a dependency to a lua environment and you still want to write a test. In that case you will have to use a lua testing framework like busted and run your tests in Lua.
This has the additional benefit that you may find issues related to the transpilation proccess (e.g. transpiler produces an incorrect output or you use a TS feature that is not supported).
The downside is that you are limited to the Lua ecosystem when it comes to test libs.

Use a JS Lua vm
Thats what we do for TSTL: we transpile TS code and run it in a JS based Lua VM(https://fengari.io/).
This is the most complex option and will only work if you can get your lua enviroment to run inside the VM.

@Perryvw
Copy link
Member

@Perryvw Perryvw commented Apr 28, 2020

To elaborate on the Lua test framework option: Using its definitions would mean the following workflow:

  1. Write tests in TS using the declarations for the framework.
  2. Call tstl on your tests to traspile them to Lua.
  3. Run your tests in Lua with the test framework you chose.
@ark120202 ark120202 added the docs label May 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.