Trying to better understand some of SSTs unit test...
# guide
c
Trying to better understand some of SSTs unit test functionality. In the guide’s section on unit tests, it utilizes a
test(…)
function that doesn’t seem to be imported/created anywhere within the code. I haven’t done much JS testing, so perhaps this is something basic I don’t know about yet. What is providing this global
test
function something - is via SST or something else? Thanks
a
+1 for this topic.
t
It's jest. Jest magically inserts stuff. I never liked this behavior but it's how it works
c
Thanks @thdxr. Any additional insight on how a code editor like VS Code could automatically know
test
is a legit function with typing? Is the SST project connecting these wires somehow? Seemed a bit magical since there are no explicit typescript files in the SST boilerplate.
r
Make sure you have the @types/jest installed
a
@Ross Coundon do you know how can I ignore some folders from jest?
I mean, I have a monorepo, and don’t want to test the frontends in this case.
r
In your jest.config.js you can set values for
Copy code
testMatch: ['**/test/**/*.it.ts?(x)', '**/test/**/*.test.ts?(x)'],
to only include the folders you need
a
I see.
I don’t have config file, just using default.
Going to need to add one then.
r
In case it's useful, we have this which targets our unit test and integration tests (the it.ts files)
Copy code
module.exports = {
  preset: 'ts-jest',
  testEnvironment: 'node',
  testPathIgnorePatterns: ['.d.ts', '.js'],
  testMatch: ['**/test/**/*.it.ts?(x)', '**/test/**/*.test.ts?(x)'],
  moduleNameMapper: {
    '@/(.*)$': '<rootDir>/src/main/$1',
  },
  moduleFileExtensions: ["ts", "tsx", "js"],
  collectCoverage: false,
  collectCoverageFrom: ['src/main/**/*.{js,ts}'],  
};
a
Nice.
Is there a jest config in TS?
I don’t use any JS.
r
I suspect so but haven't bothered. We're all TS too but as this is basically static I've never bothered to investigate
a
@Ross Coundon will adding my own
jest.config.js
override the default one provided by SST? I am having mine like:
Copy code
module.exports = {
  testEnvironment: 'node',
  testMatch: ['**/__tests__/test_cases/**/*']
}
but it still matches unintended files
t
^ I'm reworking sst's jest integration today
In the meanwhile you can just call jest directly
in package.json#scripts
test: AWS_SDK_LOAD_CONFIG=1 jest
a
thank you @thdxr nice to see you in this thread too ☺️
t
I'm everywhere !
a
you are indeed!
@thdxr in case you might be interested. I find using jest directly would get me into
SyntaxError: Cannot use import statement outside a module
which requires some additional Babel config, while
sst test
has it handled behind the scene
t
I don't use babel actually - I use esbuild-runner highly recommend it
let me find my config
We'll probably switch to it as the default when I make these updates
install
esbuild-runner
and then this is the config:
Copy code
module.exports = {
  roots: ["<rootDir>/test"],
  testMatch: ["**/*.*test.(ts|tsx|js)"],
  transform: {
    "\\.ts$": "esbuild-runner/jest",
  },
}
a
still hitting this..
t
My transform limits it to just
.ts
- maybe try including
.js
as well
a
still no luck
t
hm ok I'll look into this once I start my jest fiddling