Skip to content

Commit 501ef3e

Browse files
committed
accept testcase snapshot
1 parent 65896b6 commit 501ef3e

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

packages/selianize/__tests__/testcase.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,33 @@ describe("test case code emitter", () => {
188188
}
189189
});
190190
});
191+
it("should send the snapshot to the command", () => {
192+
const test = {
193+
id: "1",
194+
name: "example test case",
195+
commands: [
196+
{
197+
id: "2",
198+
command: "anUnknownCommand",
199+
target: "",
200+
value: ""
201+
}
202+
]
203+
};
204+
const snapshot = {
205+
commands: {
206+
"2": "command code"
207+
},
208+
setupHooks: [],
209+
teardownHooks: []
210+
};
211+
expect(TestCaseEmitter.emit(test, undefined, snapshot)).resolves.toEqual({
212+
id: "1",
213+
name: "example test case",
214+
test: "it(\"example test case\", async () => {await tests.example_test_case(driver, vars);await driver.getTitle().then(title => {expect(title).toBeDefined();});});",
215+
function: "tests.example_test_case = async function example_test_case(driver, vars, opts) {command code}"
216+
});
217+
});
191218
it("should emit a snapshot for a test case with setup and teardown hooks when skipStdLibEmitting is set", () => {
192219
const test = {
193220
id: "1",
@@ -207,4 +234,22 @@ describe("test case code emitter", () => {
207234
}
208235
});
209236
});
237+
it("should append the snapshot of the setup and teardown hooks to the test case", () => {
238+
const test = {
239+
id: "1",
240+
name: "example test case",
241+
commands: []
242+
};
243+
const snapshot = {
244+
commands: {},
245+
setupHooks: ["more setup"],
246+
teardownHooks: ["more teardown"]
247+
};
248+
expect(TestCaseEmitter.emit(test, undefined, snapshot)).resolves.toEqual({
249+
id: "1",
250+
name: "example test case",
251+
test: "it(\"example test case\", async () => {setup codemore setupawait tests.example_test_case(driver, vars);await driver.getTitle().then(title => {expect(title).toBeDefined();});teardown codemore teardown});",
252+
function: "tests.example_test_case = async function example_test_case(driver, vars, opts) {}"
253+
});
254+
});
210255
});

packages/selianize/src/testcase.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { convertToSnake } from "./utils";
2121

2222
const hooks = [];
2323

24-
export function emit(test, options = config) {
24+
export function emit(test, options = config, snapshot) {
2525
return new Promise(async (res, rej) => { // eslint-disable-line no-unused-vars
2626
const hookResults = await Promise.all(hooks.map((hook) => hook(test)));
2727
const setupHooks = hookResults.map((hook) => hook.setup || "");
@@ -30,7 +30,7 @@ export function emit(test, options = config) {
3030

3131
let errors = [];
3232

33-
const commands = (await Promise.all(test.commands.map((command, index) => (CommandEmitter.emit(command, options).catch(e => {
33+
const commands = (await Promise.all(test.commands.map((command, index) => (CommandEmitter.emit(command, options, snapshot ? snapshot.commands[command.id] : undefined).catch(e => {
3434
if (options.silenceErrors) {
3535
return `throw new Error("${e.message}");`;
3636
} else {
@@ -49,10 +49,10 @@ export function emit(test, options = config) {
4949
if (!options.skipStdLibEmitting) {
5050
// emit everything
5151
let emittedTest = `it("${test.name}", async () => {`;
52-
emittedTest += setupHooks.join("");
52+
emittedTest += setupHooks.join("").concat(snapshot ? snapshot.setupHooks.join("") : "");
5353
emittedTest += `await tests.${testName}(driver, vars);`;
5454
emittedTest += "await driver.getTitle().then(title => {expect(title).toBeDefined();});";
55-
emittedTest += teardownHooks.join("");
55+
emittedTest += teardownHooks.join("").concat(snapshot ? snapshot.teardownHooks.join("") : "");
5656
emittedTest += "});";
5757

5858
let func = `tests.${testName} = async function ${testName}(driver, vars, opts) {`;

0 commit comments

Comments
 (0)