Replies: 2 comments 1 reply
-
Here is what I ended up doing, in case anyone finds it useful. Add the functions // foo.ts
function A() {
funcs.B();
}
function B() {
}
const funcs = {
A,
B,
}; Export // foo.test.ts
import sinon from "https://cdn.skypack.dev/sinon@12.0.1?dts";
import { funcs } from "./foo.ts";
const spy = sinon.spy(funcs, "B");
// Deno.test something that calls A, then assert that B was called once:
assertEquals(spy.getCalls().length, 1); |
Beta Was this translation helpful? Give feedback.
0 replies
-
@GJZwiers I suggest to upstream this solution to https://github.com/denoland/manual |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a function A that calls another function B:
And I want to write a test that wraps function
B
usingsinon
and makes some assertions, such as ifB
has been called after invokingA
:The only way this works is if I add a parameter to
A
so I can replaceB
with the spy function for the test:but in reality
A
is more complex and I don't really want to add parameters purely for testing. In other JavaScript environmentsB
can be accessed from a global such aswindow
, like in the example from this post:Is there an equivalent of this in Deno? I've tried
window
andself
, but those error sayingB
is undefined on those objects. Also in my projectB
is first imported from another file, and I've triedimport * as foo from "my_file.ts"
and thensinon.spy(foo, 'B')
, but in that casesinon
errors sayingES modules cannot be spied
. Can I spy onB
without having to rewriteA
to take an extra parameter?Beta Was this translation helpful? Give feedback.
All reactions