|
| 1 | +# Testing |
| 2 | + |
| 3 | +Automated testing helps prevent regressions and reproduce complex failure |
| 4 | +scenarios for bug fixing or feature implementation. This project comes with |
| 5 | +support for both unit and integration testing with your Screeps code. |
| 6 | + |
| 7 | +You can read more about [unit and integration testing on |
| 8 | +Wikipedia](https://en.wikipedia.org/wiki/Test-driven_development). |
| 9 | + |
| 10 | +This documentation will cover the testing setup for those already familiar with |
| 11 | +the process of test driven design. |
| 12 | + |
| 13 | +Tests are written via [Mocha](https://mochajs.org/) and executed as tests only |
| 14 | +if they include `.test.ts` in their filename. If you have written a test file |
| 15 | +but aren't seeing it executed, this is probably why. There are two separate test |
| 16 | +commands and configurations, as unit tests don't need the complete Screeps |
| 17 | +server run-time as integration tests do. |
| 18 | + |
| 19 | +## Running Tests |
| 20 | + |
| 21 | +The standard `npm test` will execute all unit and integration tests in sequence. |
| 22 | +This is helpful for CI/CD and pre-publish checks, however during active |
| 23 | +development it's better to run just a subset of interesting tests. |
| 24 | + |
| 25 | +You can use `npm run test-unit` or `npm run test-integration` to run just one of |
| 26 | +the test suites. Additionally you can supply Mocha options to these test |
| 27 | +commands to further control the testing behavior. As an example, the following |
| 28 | +command will only execute integration tests with the word `memory` in their |
| 29 | +description: |
| 30 | + |
| 31 | +``` |
| 32 | +npm run test-integration -- -g memory |
| 33 | +``` |
| 34 | + |
| 35 | +Note that arguments after the initial `--` will be passed to `mocha` directly. |
| 36 | + |
| 37 | +## Unit Testing |
| 38 | + |
| 39 | +You can test code with simple run-time dependencies via the unit testing |
| 40 | +support. Since unit testing is much faster than integration testing by orders of |
| 41 | +magnitude, it is recommended to prefer unit tests wherever possible. |
| 42 | + |
| 43 | +## Integration Testing |
| 44 | + |
| 45 | +Integration testing is for code that depends heavily on having a full game |
| 46 | +environment. Integration tests are completely representative of the real game |
| 47 | +(in fact they run with an actual Screeps server). This comes at the cost of |
| 48 | +performance and very involved setup when creating specific scenarios. |
| 49 | + |
| 50 | +Server testing support is implmented via |
| 51 | +[screeps-server-mockup](https://github.com/Hiryus/screeps-server-mockup). View |
| 52 | +this repository for more information on the API. |
| 53 | + |
| 54 | +By default the test helper will create a "stub" world with a 3x3 grid of rooms |
| 55 | +with sources and controllers. Additionally it spawns a bot called "player" |
| 56 | +running the compiled main.js file from this repository. |
| 57 | + |
| 58 | +It falls on the user to properly set up preconditions using the |
| 59 | +screeps-server-mockup API. Importantly, most methods exposed with this API are |
| 60 | +asynchronous, so using them requires frequent use of the `await` keyword to get |
| 61 | +a result and ensure order of execution. If you find that some of your |
| 62 | +preconditions don't seem to take effect, or that you receive a Promise object |
| 63 | +rather than an expected value, you're likely missing `await` on an API method. |
| 64 | + |
| 65 | +Finally, please note that screeps-server-mockup, and this repo by extension, |
| 66 | +come with a specific screeps server version at any given time. It's possible |
| 67 | +that either your local package.json, or the screeps-server-mockup package itself |
| 68 | +are out of date and pulling in an older version of the [screeps |
| 69 | +server](https://github.com/screeps/screeps). If you notice that test environment |
| 70 | +behavior differs from the MMO server, ensure that all of these dependencies are |
| 71 | +correctly up to date. |
0 commit comments