Trying to get into unit testing and was going to s...
# suitescript
s
Trying to get into unit testing and was going to setup jest. Does anyone have a good site, or sample project, they'd be willing to share?
👀 1
d
there's a flag when creating a new SDF Project command (https://docs.oracle.com/en/cloud/saas/netsuite/ns-online-help/section_162938506015.html) that should install/setup all the dependencies and project structure for NS unit testing framework via Jest (https://github.com/oracle/netsuite-suitecloud-sdk/tree/master/packages/unit-testing) is there something in particular that wasn't working with that setup?
👍🏼 1
s
That's how I started, but I get errors for N/log and anytime I have custom modules used via NAmdConfig :\
d
I think this can be solved multiple ways, but I ended up creating a "Globals.js" and using it as a setupFile (https://jestjs.io/docs/configuration#setupfiles-array) and putting log/util stuff in there to replicate what is available as global module in NS
👍🏼 1
I never explicitly import N/log in my scripts though, idk if that is relevant, if you do that you probably then have to create a custom stub like how they document instead not sure on the custom modules though, I think if the project structure paths is all as is expected by SDF/jest framework then should all just work "automagically"
👍🏼 1
s
I was able to get past that one with a different plugin. The one I'm still trying to figure out is, like with your Globals.js, how do I actually import and setup the custom amd modules
d
maybe need to update suitecloud.config.js to point to correct src defaultProjectFolder? been a while since I setup initially, but I think if everything is pointed to correct paths then should pickup on all the custom amd modules
s
I think it is. mine goes src --> SuiteScripts --> Libs --> helperConfig.json. The defaultProjectFolder for suitecloud.config.js is src
d
is yours SuiteApp projectType? mine goes like '[suiteAppId]/src' also the custom module you're trying to load is js or json?
s
Nah, mine is an account customization project
I'm trying to load a searchHelper.js file
This is my ignorant attempt lol.
Copy code
import * as searchHelper from '../../../../../src/FileCabinet/SuiteScripts/AccentDecor/Libs/HelperClasses/searchHelper'
jest.mock('searchHelper');
import allocationHelper from '../../../../../src/FileCabinet/SuiteScripts/AccentDecor/implementation/Order to Cash/Allocations/ad_bl_set_allocation_strategy.js';
Cannot find module 'searchHelper'
d
oh so it's in the test it's having trouble finding, I thought it was when trying to run the src I believe for the
jest.mock
, requires to put the full relative path there as well for one thing
s
Yeah, the test is not finding the custom modules I've written and using via the NSAmdConfig
d
when debugging test, if you put a breakpoint in jest.config.js and store
SuiteCloudJestConfiguration.build
as a variable and inspect , if you look at the
moduleNameMapper
property there should be one that is like "SuiteScripts" just to check that matches your corresponding src location I'm not sure what else might be the issue though 😅
s
MAN! that makes it so much easier. Created a global file that reads the helperConfig and then adds them to the moduleNameMapper
👀 1
d
hmm still seems like a bit more setup than I had to do personally, I believe more should be happening automatically via the
generate
method here https://github.com/oracle/netsuite-suitecloud-sdk/blob/master/packages/unit-testing/jest-configuration/SuiteCloudJestConfiguration.js , without additional global/config with regard to your custom amd modules within src/SuiteScripts specifically but hey if it is all working now together then great! :)
s
Agreed, but it works! Do you know if you were ever able to mock a private function? I don't' need to test it, I just need to return data when it is called in the code
d
I think that's more of just a general unit testing /jest type of thing, but I guess it's possible something like (https://stackoverflow.com/questions/43265944/is-there-any-way-to-mock-private-functions-with-jest) although idk if I ran into that usecase necessarily - or maybe just abstract the private stuff to another separate module to mock if is justified
s
Yeah, it is a generic unit testing thing, I was just hoping someone here had solved it already haha. I did that back when I wrote .net tests. I am surprised it's not more of a thing. Like if I call a public method, which calls a private method, why wouldn't I want to ensure it is able to be tested.
I was looking at that earlier. I guess because the business logic file is in a define statement, it works different than their examples on that link. Thanks though!