GithubHelp home page GithubHelp logo

Comments (6)

tommy-mitchell avatar tommy-mitchell commented on June 11, 2024

Seems like it's possible using testdouble / quibble:

// src/helpers.js
export const foo = () => "foo";
// src/cli.js
import { foo } from "./helpers.js";

console.log(foo());
//=> expected: "foo"
// test.js
import * as td from "test-double";

await td.replaceEsm("./src/helpers.js", { foo: () => "bar" });

await import("./src/cli.js");
//=> actual: "bar"

from esmock.

koshic avatar koshic commented on June 11, 2024

@tommy-mitchell may be I missed something, but why you can't use globalmocks from

await esmock(
  './to/module.js', // filepath to the source module being tested
  import.meta.url, // parentUrl from which mocked modules should be resolved
  { ...childmocks }, // mock definitions imported by the source module, directly
  { ...globalmocks }) // mock definitions imported anywhere in the source import tree

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

If I try running the first code snippet, the CLI is invoked without any mocks being applied.

Maybe the path should be "../src/helpers.js"?

await esmock("../src/cli.js", {
- "./helpers.js": {
+ "../src/helpers.js": {
    foo: someSpyOrStub(),
    bar: someSpyOrStub(),
  },
});

esmock should throw an error if no module is found at the "helpers.js" path. We can only guess about the layout and naming of your files

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

though I don't know what your cli file looks like and maybe the idea is not helpful, perhaps the cli file could be made into a basic wrapper file that imports and calls more typical javascript file. The typical javascript file could be mocked more easily as a target for unit-tests eg,

app.cli.js

#! /usr/bin/env node
import app from './app.mjs';

(async () => {
  try {
    const res = await app( process.env );

    console.log('[...] cli success!:', JSON.stringify(res, null, '  '));
  } catch (e) {
    console.error(`Error!!: ${e.message}`);

    throw new Error('cli failed!');
  }
})();

Using a cli file like this one, the regular "app" could be unit-tested in a typical sort of way where env vars would become params the app is called with

from esmock.

iambumblehead avatar iambumblehead commented on June 11, 2024

@tommy-mitchell I could be misunderstanding something but agree with @koshic that this form should work for your case

await esmock("../src/cli.js", {
  "../src/helpers.js": {
    foo: someSpyOrStub(),
    bar: someSpyOrStub()
  }
});

from esmock.

tommy-mitchell avatar tommy-mitchell commented on June 11, 2024

My issue was an incorrect module specifier and some confusion about how a module gets invoked. Besides the issue in #215, this is working as intended.

from esmock.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.