Best JavaScript code snippet using apimocker
util.spec.js
Source:util.spec.js  
...33    index = require("../..");34    expect(index.isHapi17()).false;35  });36  it("test is hapi 17", () => {37    mockRequire("hapi/package", { version: "17.2.2" });38    index = require("../..");39    expect(index.isHapi17()).true;40  });41  it("test is not hapi 17", () => {42    index = require("../..");43    expect(index.isHapi17()).false;44  });45  it("test allow tests to set isHapi17 flag", () => {46    index = require("../..");47    expect(index.isHapi17()).false;48    index._testSetHapi17(true);49    expect(index.isHapi17()).true;50  });51  it("test allow tests to set isFastify flag", () => {52    index = require("../..");53    expect(index.isFastify()).false;54    index._testSetFastify(true);55    expect(index.isFastify()).true;56  });57  it("test is hapi 18", () => {58    mockRequire("@hapi/hapi/package", { version: "18.3.2" });59    utils.tryResolve = () => "@hapi/hapi/package";60    index = require("../..");61    expect(index.isHapi18OrUp()).true;62  });63  it("test is not hapi 18", () => {64    index = require("../..");65    expect(index.isHapi18OrUp()).false;66  });67  it("test is not fastify", () => {68    index = require("../..");69    expect(index.isFastify()).false;70  });71  it("test is fastify", () => {72    mockRequire("fastify/package", {});73    utils.tryResolve = () => "fastify/package";74    index = require("../..");75    expect(index.isFastify()).true;76  });77  it("test allow tests to set isHapi18 flag", () => {78    index = require("../..");79    expect(index.isHapi18OrUp()).false;80    index._testSetHapi18(true);81    expect(index.isHapi18OrUp()).true;82  });83  it("test no hapi, defaults hapi 16", () => {84    //mockRequire("@hapi/hapi/package", null);85    mockRequire("hapi/package", null);86    utils.tryResolve = () => null;87    index = require("../..");88    expect(index.isHapi17()).false;89  });90  it("test universalHapiPlugin on Fastify", () => {91    mockRequire("fastify/package", {});92    utils.tryResolve = () => "fastify/package";93    index = require("../..");94    const registers = {95      hapi16: () => {},96      hapi17: () => {},97      fastify: () => {}98    };99    const pkg = { name: "Green" };100    const plugin = index.universalHapiPlugin(registers, pkg);101    expect(plugin).equal(registers.fastify);102  });103  it("test universalHapiPlugin when no Fastify version", async () => {104    mockRequire("fastify/package", {});105    utils.tryResolve = () => "fastify/package";106    index = require("../..");107    const registers = {108      hapi16: () => {},109      hapi17: () => {}110    };111    const pkg = { name: "Green" };112    const plugin = index.universalHapiPlugin(registers, pkg);113    try {114      await plugin();115      throw new Error("Expected plugin to throw on registration");116    } catch (e) {117      expect(e.message).to.equal("Plugin is not compatible with fastify");118    }119  });120  it("test universalHapiPlugin on Hapi 16", () => {121    index = require("../..");122    const registers = {123      hapi16: () => true,124      hapi17: () => false125    };126    const pkg = { name: "Green" };127    const plugin = index.universalHapiPlugin(registers, pkg);128    expect(plugin.attributes.pkg).equal(pkg);129    expect(plugin).equal(registers.hapi16);130  });131  it("test universalHapiPlugin on Hapi 17", () => {132    mockRequire("hapi/package", { version: "17.0.0" });133    index = require("../..");134    const registers = {135      hapi16: () => false,136      hapi17: () => true137    };138    const pkg = { name: "Yellow" };139    const plugin = index.universalHapiPlugin(registers, pkg);140    expect(plugin.pkg).equal(pkg);141    expect(plugin.register).equal(registers.hapi17);142  });143  it("test universalHapiPlugin on Hapi 18, using registers.hapi17", () => {144    mockRequire("hapi/package", { version: "18.3.2" });145    index = require("../..");146    const registers = {147      hapi16: () => false,148      hapi17: () => true149    };150    const pkg = { name: "Yellow" };151    const plugin = index.universalHapiPlugin(registers, pkg);152    expect(plugin.pkg).equal(pkg);153    expect(plugin.register).equal(registers.hapi17);154  });155  it("test universalHapiPlugin on Hapi 18, using registers.hapi17OrUp", () => {156    mockRequire("hapi/package", { version: "18.3.2" });157    index = require("../..");158    const registers = {159      hapi16: () => false,160      hapi17OrUp: () => true161    };162    const pkg = { name: "Yellow" };163    const plugin = index.universalHapiPlugin(registers, pkg);164    expect(plugin.pkg).equal(pkg);165    expect(plugin.register).equal(registers.hapi17OrUp);166  });167  it("_testSetHapi17 should set version to 17 for true", () => {168    compat.hapiVersion = 18;169    compat._testSetHapi17(true);170    expect(compat.hapiVersion).equals(17);171  });172  it("_testSetHapi17 should set version to 16 for 17 and false", () => {173    compat.hapiVersion = 17;174    compat._testSetHapi17(false);175    expect(compat.hapiVersion).equals(16);176  });177  it("_testSetHapi17 should set version to 16 for 17 and false on fresh require", () => {178    mockRequire("hapi/package", { version: "17.8.5" });179    index = require("../..");180    index._testSetHapi17(false);181    expect(index.hapiVersion).equals(16);182  });183  it("_testSetHapi17 should do nothing for version !== 17 and false", () => {184    compat.hapiVersion = 18;185    compat._testSetHapi17(false);186    expect(compat.hapiVersion).equals(18);187  });188  it("_testSetHapi18 should set version to 18 for true", () => {189    compat.hapiVersion = 16;190    compat._testSetHapi18(true);191    expect(compat.hapiVersion).equals(18);192  });193  it("_testSetHapi18 should set version to 17 for 18 and false", () => {194    compat.hapiVersion = 18;195    compat._testSetHapi18(false);196    expect(compat.hapiVersion).equals(17);197  });198  it("_testSetHapi18 should set version to 17 for 18 and false on fresh require", () => {199    mockRequire("@hapi/hapi/package", { version: "18.3.2" });200    utils.tryResolve = () => "@hapi/hapi/package";201    index = require("../..");202    index._testSetHapi18(false);203    expect(index.hapiVersion).equals(17);204  });205  it("_testSetHapi18 should do nothing for version !== 18 and false", () => {206    compat.hapiVersion = 16;207    compat._testSetHapi18(false);208    expect(compat.hapiVersion).equals(16);209  });210  describe("isPathAlignedToMe", function () {211    it("should return true if both path align", () => {212      expect(213        compat.isPathAlignedToMe(...server.spec.js
Source:server.spec.js  
...29            },30            path: require("path"),31            util: require("../../lib/util")()32        };33        mockRequire("../../lib/container", {34            register(key, value) {35                containerValues[key] = value;36            },37            resolve(thing) {38                return containerValues[thing];39            }40        });41        mockRequire("../../config.json", {42            server: {43                port: 44344            }45        });46        mockRequire("/override.json", {47            override: true48        });49    });50    afterEach(() => {51        mockRequire.stopAll();52        if (process.env.PORT) {53            delete process.env.PORT;54        }55        if (process.env.DEBUG) {56            delete process.env.DEBUG;57        }58    });59    it("uses override files", () => {60        neodocArgs["--override"] = "/override.json";...keep-alive.js
Source:keep-alive.js  
...19  on() {20    called.socketOn = true;21  }22}23mockRequire('ws', MockWebSocket);24mockRequire('request', {25  post: () => {26    called.post = true;27  }28});29mockRequire('i2c-bus', {30  openSync: () => {}31});32mockRequire('gpio', {33  export: () => {}34});35mockRequire('repl', {});36mockRequire('node-pty', {37  spawn: () => {38    called.spawn = true;39    return {40      on: () => {41        called.ptyOn = true;42      },43      write: () => {44        called.ptyWrite = true;45      }46    };47  }48});49function beforeEach() {50}...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
