Does anyone have an example of unit testing the NS...
# suitescript
d
Does anyone have an example of unit testing the NS search module? i'm having trouble stubbing the methods.
s
There are several existing Node projects that stub or mock all of the suitescript modules https://github.com/KyleJonesWinsted/suitecloud-unit-testing-stubs https://github.com/mycsHQ/ns-suitescript-mocks https://github.com/emorris00/suitescript-mocks Probably manys others, too. They should provide a few good ideas for different ways to create mocks or stubs of the suitescript API
e
Could you share the function you're trying to unit test? I've found it rare to need to mock much of the query or search module, but I tend to isolate my searches/queries into their own functions
If I find myself mocking lots of SuiteScript APIs, I view it as a code smell in my design and try to refactor that rather than mock a dozen functions. I realize that's not always an option though
s
For small UE and Client scripts that access the current record, I don’t usually try to isolate that layer from the rest of my code since the business logic and the record fields are often interwoven, so I have found it useful to mock or stub in that instance. Then again, those are the type of scripts that often don’t need a lot of unit testing due to their size and limited scope. For other script types I try to avoid that and keep data access separate from business logic, to make unit testing easier. I really only want to unit test my logic, not the APIs I am calling.
d
So its just a simple function that loads a search and returns the results code
Copy code
let searchResults = [];
                const savedSearch = search.load({id: savedSearchID});
                
                let startIndex = 0;
                const pageSize = 1000;  

                do {
                    let pagedResults = savedSearch.run().getRange({
                        start: startIndex,
                        end: startIndex + pageSize
                    });

                    if (pagedResults.length === 0) {
                        break;
                    }
                    searchResults.push(...pagedResults);
                    startIndex += pageSize;
                } while (true);

                return searchResults;
test using https://github.com/KyleJonesWinsted/suitecloud-unit-testing-stubs
Copy code
search.load.mockReturnValue(search.Search)
        search.run.mockReturnValue('placeholder')
search.Search
resolves to undefined, so I'm unable to mock a value for
search.run
. Trying to find an example of how to start off a test with the search module
e
I don't see any business logic here; it's just running a search and returning results. What is your goal with the test? Just to see how you might mock the module?
s
the best thing would be to put this section of code into a function, then stub that function. in fact, if you do this a lot, it might be worth creating your own module that includes the N/search module and adds in your own convenience functions. it will simplify unit testing, and avoid the need to stub or mock the SuiteScript API
d
thanks for getting back to me, yeah i just wanted to see how i would stub a module like search. ill keep all of this in mind for future test 👍
s
the tricky thing with many SuiteScript APIs is that various functions return objects that themselves have functions, so mocking them properly is not simple, and probably best to avoid it altogether if you can