Git Product home page Git Product logo

Comments (5)

lsiden avatar lsiden commented on June 18, 2024

In order to resolve this, I had to add the following to any test file that indirectly imports abort-controller:

For example, in fetch.js:

import AbortController from "abort-controller"

Then, in test/fetch.spec.js

	import { func1, func2, ... } from "../fetch"

    jest.mock("event-target-shim", () => ({
      EventTarget: () => "EventTarget",
      defineEventAttribute: () => "defineEventAttribute"
    }))
    jest.mock(
      "abort-controller",
      () =>
        class AbortController {
          abort() {}
        }
    )

Do you think this warrants a mod to the README file? I can submit a PR if you are too busy.

from abort-controller.

mysticatea avatar mysticatea commented on June 18, 2024

Thank you for the investigation, and I'm sorry for my delay.

I'm not familiar with jest, but I feel odd if we have to know all indirect dependencies to use mocks because mocks should replace implementation and such indirect dependencies are the detail of implementation that should be replaced.

Does jest really require the knowledge of implementation details to use mocks? If yes, I can't add any new dependencies into this package since it will break jest's code even if it doesn't change any public API. It's a too strong restriction.

from abort-controller.

lsiden avatar lsiden commented on June 18, 2024

Toru, I don't know how to write tests for logic that makes asynchronous requests (HTTP) without writing mocks. I just didn't expect to have to write a mock for abort-controller and event-target-shim.

The problems appears to be that the node.js interpreter detects a circular reference when loading abort-controller.js and throws a run-time error.

There is no problem when the browser loads abort-controller. By that time, all libraries have been transpiled into ES5 and bundled by Webpack.

You might want to consider using babel to transpile the .js files in both libraries into ES5 for the dist/ directory for maximum compatibility.

from abort-controller.

mysticatea avatar mysticatea commented on June 18, 2024

Toru, I don't know how to write tests for logic that makes asynchronous requests (HTTP) without writing mocks. I just didn't expect to have to write a mock for abort-controller and event-target-shim.

I don't say "don't use mocks", but I suspect that jest's mock system has a critical defect that messes library to replace indirect dependencies with undefined.

The problems appears to be that the node.js interpreter detects a circular reference when loading abort-controller.js and throws a run-time error.

No. Any circular references don't exist in this library. The event-target-shim doesn't have any dependencies. I think that Jest hacks and overwrites Node.js's module loading behavior and it messes require("event-target-shim") of this library. Mock libraries need such hacks. But sane mocks would not replace internal dependencies with undefined silently.

You might want to consider using babel to transpile the .js files in both libraries into ES5 for the dist/ directory for maximum compatibility.

I don't think that I want to inflate the source code as unnecessary.


I'm laerning toward adding a note "minor/patch releases in future can break your Jest's tests because of the mock system."

from abort-controller.

lsiden avatar lsiden commented on June 18, 2024

from abort-controller.

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.