-
Notifications
You must be signed in to change notification settings - Fork 0
/
customJestStubs.js
109 lines (108 loc) · 3.98 KB
/
customJestStubs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/** @format
* @name CustomJestStubs
* @description Template for creating custom stubs for your NetSuite custom modules
* @since 2022.2
* @exports customStubs
*/
/**
* @typedef CustomStub
* @type {Object}
* @property {String} name - "Directory/Path/ModuleName" -- CustomStub property to use
* @property {String} path - Pre-path to module for project -- generally returned as `\`<rootDir>/node_modules/${PKG_STUBS_PATH}/${CustomStubMap[filename]}.js\``
* @description Array of module names and paths to use for them in the stubs package
*/
/**
* @typedef UseMinified
* @type {Object}
* @description Options for loading minified file
* @property {boolean} value - T/F do you want to use a minified version in define statement
* @property {String} minName='.min' - Optional alternative post fix identifier to add to file
*/
/**
* @typedef ExcludeStub
* @type {Object}
* @property {String} name - "Directory/Path/ModuleName" -- CustomStub property to use
* @property {String} path - Path to module for project -- generally you will use `\`${__dirname}/src/FileCabinet\``
* @property {UseMinified} useMinified - Options for using or not using minified files locally
* @description Array of module names and paths to use for them in the stubs package
*/
/**
* @typedef ModuleStub
* @type {Object}
* @property {String} "Directory/Path/ModuleName" - Relative path to module (within src/FileCabinet/SuiteScripts/)"
* @description Object property/value pair for naming a module and the path for Jest to load it from.
* @summary Use un-minified named modules for your minified dependencies by using the non-minified path for the named
* modules. For example, `Modules/aModules.min` can load `Modules/aModules` without issues, but is easier to debug/test with.
*/
/**
* @name CustomStubMap
* @type {ModuleStub[]}
* @description Array of modules stubs to define
*/
const CustomStubMap = {
'Modules/aModule': 'Modules/aModule',
'Modules/aModule.min':
'Modules/aModule' /* Note we are not loading min for stubs */,
'Modules/bModule': 'Modules/bModule',
'Modules/bModule.min':
'Modules/bModule' /* Note we are not loading min for stubs */,
};
/**
* @name PKG_STUBS_PATH
* @type {String}
* @description Path to project within `node_modules` directory
* @summary This is the path used for loading custom stubs from the package. Update this value to match your stubs npm package name.
*/
const PKG_STUBS_PATH =
'@vnetwork-solutions/ns-custom-stubs-template/stubs/SuiteScripts';
/**
* @name customStubMap
* @description Converts CustomStubMap to Jest customStub array type
* @function
* @param {ExcludeStub[]} exclude - Array of named modules to override path location for
* @return {CustomStub[]}
*/
const customStubs = exclude =>
Object.keys(CustomStubMap).map(filename => {
if (!Array.isArray(exclude)) throw 'EXCLUDED_STUBS_TYPEERROR';
let stubModuleName = `/SuiteScripts/${filename}`;
let ext = '.js';
const Module = {
module: stubModuleName,
path: `<rootDir>/node_modules/${PKG_STUBS_PATH}/${CustomStubMap[filename]}`,
};
// Loop over array of excludes and link min/non-min to project overrides
for (const ExcludedModule of exclude) {
if (!RegExp(ExcludedModule.name).test(stubModuleName)) continue; //?
ExcludedModule.useMinified.minName =
ExcludedModule.useMinified.hasOwnProperty('minName')
? ExcludedModule.useMinified.minName
: '.min';
if (ExcludedModule.useMinified.value) {
Module.path = ExcludedModule.path + stubModuleName + ext;
break;
}
stubModuleName = stubModuleName.replace(
ExcludedModule.useMinified.minName,
''
);
Module.path = ExcludedModule.path + stubModuleName + ext;
break;
}
return Module;
});
/*
const ExcludeStubs = [
{
name: '/SuiteScripts/Modules/aModule',
path: `${__dirname}/src/FileCabinet`,
useMinified: {
value: false,
},
},
];
customStubs(ExcludeStubs); //?
*/
module.exports = {
customStubs,
};