https://serverless-stack.com/ logo
#guide
Title
# guide
c

Clayton

09/09/2021, 9:13 PM
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

Adrián Mouly

09/09/2021, 9:14 PM
+1 for this topic.
t

thdxr

09/09/2021, 9:14 PM
It's jest. Jest magically inserts stuff. I never liked this behavior but it's how it works
c

Clayton

09/09/2021, 9:21 PM
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

Ross Coundon

09/10/2021, 6:53 AM
Make sure you have the @types/jest installed
a

Adrián Mouly

09/10/2021, 6:57 AM
@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

Ross Coundon

09/10/2021, 6:59 AM
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

Adrián Mouly

09/10/2021, 7:18 AM
I see.
I don’t have config file, just using default.
Going to need to add one then.
r

Ross Coundon

09/10/2021, 7:23 AM
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

Adrián Mouly

09/10/2021, 7:32 AM
Nice.
Is there a jest config in TS?
I don’t use any JS.
r

Ross Coundon

09/10/2021, 7:55 AM
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

arda

10/19/2021, 6:58 PM
@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

thdxr

10/19/2021, 6:59 PM
^ 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

arda

10/19/2021, 7:00 PM
thank you @thdxr nice to see you in this thread too ☺️
t

thdxr

10/19/2021, 7:01 PM
I'm everywhere !
a

arda

10/19/2021, 7:02 PM
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

thdxr

10/19/2021, 7:08 PM
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

arda

10/19/2021, 7:13 PM
still hitting this..
t

thdxr

10/19/2021, 7:15 PM
My transform limits it to just
.ts
- maybe try including
.js
as well
a

arda

10/19/2021, 8:03 PM
still no luck
t

thdxr

10/19/2021, 8:07 PM
hm ok I'll look into this once I start my jest fiddling