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

Issue #76 add cleaning testData before running each test file. #88

Merged
merged 8 commits into from
May 18, 2021
1 change: 1 addition & 0 deletions docs/api/reference/globals.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ These extensions are all available on a global associative array called `_brs_`:
{
getStackTrace: function,
global: associative array,
testData: associative array,
mockComponent: function,
mockComponentPartial: function,
mockFunction: function,
Expand Down
20 changes: 20 additions & 0 deletions docs/api/reference/test-utilities.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ print _brs_.global

------------

## \_brs_.testData

A reference to the temporary associative array, so that you can access it from everywhere inside your brs code. Mostly used for saving/accessing some test-specific data. Clears before running each test file.

### Usage
```brightscript
_brs_.testData._isFooAvailable = false
_brs_.mockFunction("isFooAvailable", sub()
return _brs_.testData._isFooAvailable
end sub)

isFooAvailable() ' => false

_brs_.testData._isFooAvailable = true ' something happened and now it is available
isFooAvailable() ' => true
```
<br/>

------------

# \_brs_.process

Allows you to access the command line arguments and locale.
Expand Down
3 changes: 2 additions & 1 deletion src/runner/JestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Config } from "@jest/types";
import { ExecuteWithScope, types as BrsTypes } from "brs";
import { resetTestData, ExecuteWithScope, types as BrsTypes } from "brs";
import { JestReporter } from "../reporter/JestReporter";
import { TestRunner } from "./TestRunner";

Expand Down Expand Up @@ -33,6 +33,7 @@ export class JestRunner extends TestRunner {
testFiles.forEach((filename, index) => {
this.reporter.onFileStart(filename);
try {
resetTestData();
lkipke marked this conversation as resolved.
Show resolved Hide resolved
execute([filename], [executeArgs]);
} catch (reason) {
this.reporter.onFileExecError(filename, index, reason);
Expand Down
3 changes: 2 additions & 1 deletion src/runner/TestRunner.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from "path";
import { types as BrsTypes, ExecuteWithScope } from "brs";
import { resetTestData, types as BrsTypes, ExecuteWithScope } from "brs";
import { formatInterpreterError } from "../util";

export class TestRunner {
Expand Down Expand Up @@ -41,6 +41,7 @@ export class TestRunner {
// Set the index so that our TAP reporting is correct.
executeArgs.elements.set("index", new BrsTypes.Int32(index));
try {
resetTestData();
execute([filename], [executeArgs]);
} catch (e) {
console.error(
Expand Down
6 changes: 4 additions & 2 deletions test/before-after/before-after.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ describe("before-after", () => {

expect(results.stats).toMatchObject({
suites: 4,
tests: 12,
passes: 9,
tests: 14,
passes: 11,
pending: 3,
failures: 0,
});
Expand All @@ -85,9 +85,11 @@ describe("before-after", () => {
{ fullTitle: "nested root case 1" },
{ fullTitle: "nested root case 2" },
{ fullTitle: "nested root case 3" },
{ fullTitle: "nested root check _brs_.testData" },
{ fullTitle: "single-level root case 1" },
{ fullTitle: "single-level root case 2" },
{ fullTitle: "single-level root case 3" },
{ fullTitle: "single-level root check _brs_.testData" },
]);
});
});
10 changes: 10 additions & 0 deletions test/before-after/mixed/tests/01-nested.test.brs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ function main(args as object) as object
m.assert.equal(m.counterB, 11, "counterB must be incremented for each test case")
end sub)

m.it("check _brs_.testData", sub()
m.assert.isTrue(_brs_.testData <> invalid, "_brs_.testData should be presented")
m.assert.isTrue(_brs_.testData.count() = 0, "by default _brs_.testData should be equal to {}")

' change value to check wheter it clear between different test files
Vasya-M marked this conversation as resolved.
Show resolved Hide resolved
_brs_.testData = {
ctx: "file 1"
}
end sub)

m.describe("suite 1", sub()
m.addContext({
counterC: 0
Expand Down
10 changes: 10 additions & 0 deletions test/before-after/mixed/tests/02-single-level.test.brs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,15 @@ function main(args as object) as object
m.assert.isInvalid(m.counterB, "afterEach state must not spread to sibling suites")
m.assert.equal(m.counterA, 5, "counterA must be incremented for each test case")
end sub)

m.it("check _brs_.testData", sub()
m.assert.isTrue(_brs_.testData <> invalid, "_brs_.testData should be presented")
m.assert.isTrue(_brs_.testData.count() = 0, "by default _brs_.testData should be equal to {}")

' change value to check wheter it clear between different test files
Vasya-M marked this conversation as resolved.
Show resolved Hide resolved
_brs_.testData = {
ctx: "file 1"
}
end sub)
end sub)
end function