i need your opinion as a community about using TS ...
# suitescript
m
i need your opinion as a community about using TS with a project like this https://github.com/headintheclouddev/typings-suitescript-2.0
m
I'm a huge fan, mainly due to the intellisense. I don't actually use Typescript at my current company due to other devs on my team but you can still use the typing with a jsconfig file and jsdoc.
💯 1
d
I'm a huge fan too, and I'm currently using TS for development and Jest unit testing
m
@Dmitry Masanov may I ask how you are using TS? I mean, have you used this project where types have been defined for NS objects or you just use it plain and then remove the unnecessary references with some snowpack task or the like? I am curious about how others have done it. Plus, about JEST my experience has been that it takes a long time to make modifications to match unit test scenarios using NS stubs. So are you using what comes out of the box with the suitecloud sdk for testing or have added some other recipe? Thanks for your feedback guys
d
Here is my tsconfig.json:
Copy code
{
  "compilerOptions": {
    "allowJs": false,
    "strictNullChecks": true,
    "strict": false,
    "module": "amd",
    "moduleResolution": "node",
    "target": "ES5",
    "sourceMap": false,
    "newLine": "LF",
    "noUnusedLocals": true,
    "experimentalDecorators": true,
    "baseUrl": "./",
    "rootDir": "../",
    "outDir": "./",
    "lib": [
      "es5","es2015.promise", "dom", "es6", "dom.iterable"
    ],
    "skipLibCheck": false,
    "esModuleInterop": true,
    "paths": {
      "N/*": [
        "node_modules/@hitc/netsuite-types/N/*", "../@hitc/netsuite-types/N/*"
      ],
      "N": [
        "node_modules/@hitc/netsuite-types/N", "../@hitc/netsuite-types/N"
      ]
    }
  },
  "exclude": [
    "node_modules",  "__mocks__", "__tests__"
  ],
}
So basically, I write code in TS, and any time I can compile it to JS
For Jest, I don't use SDF built-ins (so always create project without testing), but just add configuration with Babel. I also have a gulpfile to build and deploy everything, so I just added a gulp task to perform all the tests before deployment
m
oh i see
custom solution for jest
d
Here is my babel config:
Copy code
module.exports = {
  presets: [
    [
      '@babel/env',
      {
        targets: {
          ie: '10',
          node: 'current'
        },
        useBuiltIns: 'usage',
        corejs: 3,
      },
    ],
    '@babel/preset-typescript'
  ],
  'plugins': [
    ['@babel/plugin-proposal-decorators', { 'legacy': true }],
    ['@babel/plugin-proposal-class-properties']
  ],
};
m
and what about stubs, how do you test a beforSubmit for instance when you need to instantiate NS objects? do you create your own mockups? This was my concern because when i wanted to test one function i had to define every mock up call in the correct order so it was frustrating when i had to deal with modifications. Thanks in advance for sharing your experience
d
I don't test interactions with NetSuite, I create a whole new object for business logic and only cover it with unit tests. As the business logic object is independent, I don't need stubs or mockups
m
good call
thanks
and for TS i see this
“paths”: { “N/*“: [ “node_modules/@hitc/netsuite-types/N/*“, “../@hitc/netsuite-types/N/*” ], “N”: [ “node_modules/@hitc/netsuite-types/N”, “../@hitc/netsuite-types/N” ] }
are these types from a module you have defined?
or got it from somewhere
d
Yes, I need this because I have different paths to the types in main project and Helpers project (where I store my functions which I can import to any other projects). In common, I believe you can just use node_modules/@hitc/netsuite-types/N/*“
m
about your testing what i undestand then is that you usually isolate your code in other modules that receive only the context for instance and then you can test it independently right?
as for types, i have tested as I said this https://github.com/headintheclouddev/typings-suitescript-2.0
but what I dont like is that I dont want to modify the way a module is defined in NS
because i share my projects with other devs and not everyone use or will use TS so I want to stick to the basics
but at the same time, at my end I want to make use of TS to minimize type errors
so i just wanted to have NS types somewhere and be able to program using TS but run a transpiler before the code is ready to be push with SDF so the final SDF project only has plain JS code
looking just as if it was taken from NS examples, you see what I mean?
so something like this works
Copy code
import {EntryPoints} from 'N/types';
import * as record from 'N/record';

export let onRequest: EntryPoints.Suitelet.onRequest = (context: EntryPoints.Suitelet.onRequestContext) => {
    let folder = record.load({type: 'folder', id: 36464});
    let allfields = folder.getFields().join(', ');
    context.response.write(`<br>all fields: ${allfields}`);
};
but it is not acceptable to show to others NS devs until NS actually accepts it so
d
Well, I just set up my transpiler to put the code to ./src/FileCabinet/SuiteScripts
So, then you can share your compiled JS code with other devs
m
yes that is exaclty my point