inline expects returning testResults correctly

This commit is contained in:
Nicholas Palenchar
2020-01-18 13:26:07 -05:00
parent 3ecf62857c
commit dd970af6b1
2 changed files with 20 additions and 18 deletions

View File

@@ -1,11 +1,14 @@
import {parse} from "graphql"; import {parse} from "graphql";
const PASS = 'PASS',
FAIL = 'FAIL',
ERROR = 'ERROR';
export default function runTestScriptWitVariables(script, variables) { export default function runTestScriptWitVariables(script, variables) {
let pw = { let pw = {
_errors: [], _errors: [],
_testResults: [],
_report: '', _report: '',
expect, expect: function(value) { return expect(value, this._testResults) },
test test
// globals that the script is allowed to have access to. // globals that the script is allowed to have access to.
}; };
@@ -17,9 +20,10 @@ export default function runTestScriptWitVariables(script, variables) {
new Function("pw", script)(pw); new Function("pw", script)(pw);
} }
catch (e) { catch (e) {
debugger;
errors = e; errors = e;
} }
return errors; return {report: pw._report, errors: pw._errors, testResults: pw._testResults};
} }
function test(descriptor, func) { function test(descriptor, func) {
@@ -29,6 +33,7 @@ function test(descriptor, func) {
testReports.push({descriptor: true, message: descriptor}); testReports.push({descriptor: true, message: descriptor});
func(); func();
}; };
//TODO shadow pw object to override _testReports
let xit = (descriptor, func) => { let xit = (descriptor, func) => {
testReports.push({descriptor: true, message: `${descriptor} [skipped]`}) testReports.push({descriptor: true, message: `${descriptor} [skipped]`})
}; };
@@ -39,8 +44,8 @@ function test(descriptor, func) {
// add checkmark or x depending on if each testReport is pass=true or pass=false // add checkmark or x depending on if each testReport is pass=true or pass=false
} }
function expect(expectValue) { function expect(expectValue, _testReports) {
return new Expectation(expectValue); return new Expectation(expectValue, null, _testReports);
} }
class Expectation { class Expectation {
@@ -73,18 +78,10 @@ class Expectation {
} }
} }
_fail(message) { _fail(message) {
if (this._testReports) { this._testReports.push({result: FAIL, message})
this._testReports.push({pass: false, message})
} else {
throw {message}
}
} }
_pass(message) { _pass(message) {
if (this._testReports) { this._testReports.push({result: PASS});
this._testReports.push({pass: true, message});
} else {
return true;
}
} }
// TEST METHODS DEFINED BELOW // TEST METHODS DEFINED BELOW
// these are the usual methods that would follow expect(...) // these are the usual methods that would follow expect(...)

View File

@@ -359,7 +359,7 @@
</div> </div>
</div> </div>
<Editor <Editor
v-model="postRequestTests" v-model="testScript"
:lang="'javascript'" :lang="'javascript'"
:options="{ :options="{
maxLines: responseBodyMaxLines, maxLines: responseBodyMaxLines,
@@ -1190,6 +1190,7 @@
import textareaAutoHeight from "../directives/textareaAutoHeight"; import textareaAutoHeight from "../directives/textareaAutoHeight";
import parseCurlCommand from "../assets/js/curlparser.js"; import parseCurlCommand from "../assets/js/curlparser.js";
import getEnvironmentVariablesFromScript from "../functions/preRequest"; import getEnvironmentVariablesFromScript from "../functions/preRequest";
import runTestScriptWitVariables from '../functions/postWomanTesting';
import parseTemplateString from "../functions/templating"; import parseTemplateString from "../functions/templating";
import AceEditor from "../components/ace-editor"; import AceEditor from "../components/ace-editor";
import {tokenRequest, oauthRedirect} from "../assets/js/oauth"; import {tokenRequest, oauthRedirect} from "../assets/js/oauth";
@@ -1266,7 +1267,7 @@
showPreRequestScript: false, showPreRequestScript: false,
testsEnabled: false, testsEnabled: false,
preRequestScript: "// pw.env.set('variable', 'value');", preRequestScript: "// pw.env.set('variable', 'value');",
postRequestTests: "// pw.expect('variable').toBe('value);", testScript: "// pw.expect('variable').toBe('value');",
copyButton: '<i class="material-icons">file_copy</i>', copyButton: '<i class="material-icons">file_copy</i>',
downloadButton: '<i class="material-icons">get_app</i>', downloadButton: '<i class="material-icons">get_app</i>',
doneButton: '<i class="material-icons">done</i>', doneButton: '<i class="material-icons">done</i>',
@@ -2141,6 +2142,9 @@
icon: "done" icon: "done"
}); });
// tests
const testResults = runTestScriptWitVariables(this.testScript, {});
(() => { (() => {
const status = (this.response.status = payload.status); const status = (this.response.status = payload.status);
const headers = (this.response.headers = payload.headers); const headers = (this.response.headers = payload.headers);
@@ -2184,7 +2188,8 @@
url: this.url, url: this.url,
path: this.path, path: this.path,
usesScripts: Boolean(this.preRequestScript), usesScripts: Boolean(this.preRequestScript),
preRequestScript: this.preRequestScript preRequestScript: this.preRequestScript,
testCode: this.testCode
}; };
this.$refs.historyComponent.addEntry(entry); this.$refs.historyComponent.addEntry(entry);
return; return;