Skip to content

Commit

Permalink
fix: correctly disable test execution timeout in repl mode
Browse files Browse the repository at this point in the history
  • Loading branch information
DudaGod committed Jan 23, 2024
1 parent ade44f8 commit 6980c44
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/browser/commands/switchToRepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export = async (browser: Browser): Promise<void> => {
return;
}

logger.log(chalk.yellow("You have entered REPL mode via terminal"));
logger.log(chalk.yellow("You have entered to REPL mode via terminal (test execution timeout is disabled)."));

const currCwd = process.cwd();
const testCwd = path.dirname(session.executionContext.ctx.currentTest.file!);
Expand Down
5 changes: 4 additions & 1 deletion src/test-reader/build-instructions.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const _ = require("lodash");
const validators = require("../validators");
const env = require("../utils/env");
const RuntimeConfig = require("../config/runtime-config");

class InstructionsList {
#commonInstructions;
Expand Down Expand Up @@ -51,7 +52,9 @@ function extendWithBrowserVersion({ treeBuilder, config }) {

function extendWithTimeout({ treeBuilder, config }) {
const { testTimeout } = config;
if (!_.isNumber(testTimeout)) {
const { replMode } = RuntimeConfig.getInstance();

if (!_.isNumber(testTimeout) || replMode?.enabled) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/test-reader/controllers/config-controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TestReaderEvents as ReadEvents } from "../../events";
import { EventEmitter } from "events";
import RuntimeConfig from "../../config/runtime-config";

type TreeBuilder = {
addTrap: (trap: (obj: { timeout: number }) => void) => void;
Expand All @@ -17,6 +18,12 @@ export class ConfigController {
}

testTimeout(timeout: number): this {
const { replMode } = RuntimeConfig.getInstance();

if (replMode?.enabled) {
return this;
}

this.#eventBus.emit(ReadEvents.NEW_BUILD_INSTRUCTION, ({ treeBuilder }: { treeBuilder: TreeBuilder }) => {
treeBuilder.addTrap(obj => (obj.timeout = timeout));
});
Expand Down
4 changes: 3 additions & 1 deletion test/src/browser/commands/switchToRepl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ describe('"switchToRepl" command', () => {
await switchToRepl_({ session });

assert.callOrder(
(logger.log as SinonStub).withArgs(chalk.yellow("You have entered REPL mode via terminal")),
(logger.log as SinonStub).withArgs(
chalk.yellow("You have entered to REPL mode via terminal (test execution timeout is disabled)."),
),
repl.start as SinonStub,
);
});
Expand Down
25 changes: 22 additions & 3 deletions test/src/test-reader/build-instructions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const { InstructionsList, Instructions } = require("src/test-reader/build-instru
const { TreeBuilder } = require("src/test-reader/tree-builder");
const validators = require("src/validators");
const env = require("src/utils/env");
const RuntimeConfig = require("src/config/runtime-config");
const { makeConfigStub } = require("../../utils");

describe("test-reader/build-instructions", () => {
Expand Down Expand Up @@ -164,10 +165,28 @@ describe("test-reader/build-instructions", () => {
});

describe("extendWithTimeout", () => {
it("should not add decorator to tree builder if 'testTimeout' is not specified in config", () => {
execTrapInstruction_(Instructions.extendWithTimeout, { config: {} });
beforeEach(() => {
sandbox.stub(RuntimeConfig, "getInstance").returns({ replMode: { enabled: false } });
});

assert.notCalled(TreeBuilder.prototype.addTrap);
describe("should not add decorator to tree builder if", () => {
it("'testTimeout' is not specified in config", () => {
execTrapInstruction_(Instructions.extendWithTimeout, { config: {} });

assert.notCalled(TreeBuilder.prototype.addTrap);
});

it("'replMode' is enabled (even if 'testTimeout' is specified)", () => {
RuntimeConfig.getInstance.returns({ replMode: { enabled: true } });

execTrapInstruction_(Instructions.extendWithTimeout, {
config: {
testTimeout: 100500,
},
});

assert.notCalled(TreeBuilder.prototype.addTrap);
});
});

it("should decorate with timeout if 'testTimeout' is specified in config", () => {
Expand Down
13 changes: 12 additions & 1 deletion test/src/test-reader/controllers/config-controller.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"use strict";

const { EventEmitter } = require("events");
const { ConfigController } = require("src/test-reader/controllers/config-controller");
const { TreeBuilder } = require("src/test-reader/tree-builder");
const { TestReaderEvents: ReadEvents } = require("src/events");
const { EventEmitter } = require("events");
const RuntimeConfig = require("src/config/runtime-config");

describe("test-reader/controllers/config-controller", () => {
const sandbox = sinon.sandbox.create();
Expand All @@ -18,13 +19,23 @@ describe("test-reader/controllers/config-controller", () => {

beforeEach(() => {
sandbox.stub(TreeBuilder.prototype, "addTrap");
sandbox.stub(RuntimeConfig, "getInstance").returns({ replMode: { enabled: false } });
});

afterEach(() => {
sandbox.restore();
});

describe("testTimeout", () => {
it("should do nothing if 'replMode' is enabled", () => {
RuntimeConfig.getInstance.returns({ replMode: { enabled: true } });
const controller = mkController_();

controller.testTimeout(100500);

assert.notCalled(TreeBuilder.prototype.addTrap);
});

it("should set trap for the test object", () => {
const controller = mkController_();

Expand Down

0 comments on commit 6980c44

Please sign in to comment.