BuckleScript bindings for Rewire
Rewire adds a special setter and getter to modules so you can modify their behaviour for better unit testing. bs-rewire provides the bindings to allow you to use the familiar rewire approach to test your ReasonML and OCaml code when targetting the Node ecosystem through Bucklescript (damn, that was amouthful, wasn't it?).
npm install --save-dev @gmmorris/bs-rewire
Then add @gmmorris/bs-rewire
to bs-dev-dependencies
in your bsconfig.json
:
{
...
"bs-dev-dependencies": ["@gmmorris/bs-rewire"]
}
Returns a rewired version of the module found at filename
.
Use Rewire.rewire()
exactly like the JS version rewire()
.
Sets the internal variable name
to the given value
. Returns a function which can be called to revert the change.
Takes all keys of dict
as variable names and sets the values respectively. Returns a function which can be called to revert the change.
Returns the private variable with the given name
.
Returns a function which - when being called - sets obj
, executes the given callback
and reverts obj
.
Returns a function which - when being called - sets obj
, executes the given callback
and expects a Promise to be returned.
obj
is only reverted after the promise has been resolved or rejected. For your convenience the returned function passes the received promise through.
The MakeRewired & MakeModuleRewiring Functors allow you to create a custom Rewire module tailerd to your Javscript module's API.
The following is the basic use case. In this use case the following approach is being taken:
Rewire.rewire
is called with a local module file being rewired at ./testAsset.js
.testAsset
is then overridden using the Rewire.set
API which takes the rewired module, the name of the variable being overridden and the mock value.The module we wish to rewire
/*
* This module returns a simple object: { param: "someValue" }
*/
let someModule = require('./someModule.js');
module.exports = {
getParam: function () {
return someModule.param;
}
};
The test file which uses rewire to test the testAsset.js file
open Jest;
[@bs.deriving jsConverter]
type oneParamModule = {param: string};
describe("testAsset.getParam", () => {
open Expect;
let getParam = [%raw
{|
function(rewiredModule) {
return rewiredModule.getParam();
}
|}
];
test("getParam returns the value in the global `someModule.param`", () => {
let rewiredModule = Rewire.rewire("./testAsset.js");
Rewire.set(
rewiredModule,
"someModule",
oneParamModuleToJs({param: "someMockedValue"}),
);
expect(
getParam(rewiredModule)
) |> toEqual("someMockedValue");
})
});
open Jest
type oneParamModule = {
param: string;}[@@bs.deriving jsConverter]
let _ =
describe "testAsset.getParam"
(fun () ->
let open Expect in
let getParam =
[%raw
{|
function(rewiredModule) {
return rewiredModule.getParam();
}
|}] in
test "getParam returns the value in the global `someModule.param`"
(fun () ->
let rewiredModule = Rewire.rewire "./testAsset.js" in
Rewire.set rewiredModule "someModule"
(oneParamModuleToJs { param = "someMockedValue" });
(expect (getParam rewiredModule)) |>
(toEqual "someMockedValue")))
The downside to Example #1 is that we have to use Raw JS to trick the compiler. This means our tests are no longer type safe - seems to defeat the purpose of using ReasonML, doesn't it?
Start by defining a custom API which mirrors the API of the JS module you're testing.
If we take our testAsset.js as an example, we have a single function, getParam
, which returns a string.
module TestAssetModule = {
include
MakeRewired(
{
type t;
},
);
[@bs.send] external getParam : t => string = "";
};
module TestAssetModule =
struct
include MakeRewired(struct type t end)
external getParam : t -> string = ""[@@bs.send ]
end
Using MakeRewired
allows us to define our single function on our module and extend this module with the Rewire API.
Now that we have a type which mirrors our JS module, we can use MakeModuleRewiring
to create a custom 'Rewire.rewire()' function which will return our custom module type.
This makes it possible for us to call getParam
directly.
open Jest;
open Rewire;
module TestAssetModule = {
include
MakeRewired(
{
type t;
},
);
[@bs.send] external getParam : t => string = "";
};
module TestAssetRewiring = {
include MakeModuleRewiring(TestAssetModule);
};
describe("testAsset.getParam", () =>
Expect.(
test(
"getParam returns the value in the global `someModule.param`",
() => {
let rewiredModule = TestAssetRewiring.rewire("./assets/testAsset.js");
expect(TestAssetModule.getParam(rewiredModule))
|> toEqual("someValue");
})
)
);
open Jest
open Rewire
module TestAssetModule =
struct
include MakeRewired(struct type t end)
external getParam : t -> string = ""[@@bs.send ]
end
module TestAssetRewiring =
struct include MakeModuleRewiring(TestAssetModule) end
let _ =
describe "testAsset.getParam"
(fun () ->
let open Expect in
test "getParam returns the value in the global `someModule.param`"
(fun () ->
let rewiredModule =
TestAssetRewiring.rewire "./assets/testAsset.js" in
(expect (TestAssetModule.getParam rewiredModule)) |>
(toEqual "someValue")))
See the tests for more examples.