Unit Tests in Serverless

#1

Link to chapter - https://serverless-stack.com/chapters/unit-tests-in-serverless.html

0 Likes

#2

Hey! Got the error:

FAIL tests/billing.test.js
● Test suite failed to run

Plugin/Preset files are not allowed to export objects, only functions. In D:\simply\noteit\noteit-api\node_modules\babel-preset-stage-3\lib\index.js

  at createDescriptor (node_modules/@babel/core/lib/config/config-descriptors.js:178:11)
  at items.map (node_modules/@babel/core/lib/config/config-descriptors.js:109:50)
      at Array.map (<anonymous>)
  at createDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:109:29)
  at createPresetDescriptors (node_modules/@babel/core/lib/config/config-descriptors.js:101:10)
  at presets (node_modules/@babel/core/lib/config/config-descriptors.js:47:19)
  at mergeChainOpts (node_modules/@babel/core/lib/config/config-chain.js:320:26)
  at node_modules/@babel/core/lib/config/config-chain.js:283:7
  at buildRootChain (node_modules/@babel/core/lib/config/config-chain.js:120:22)
  at loadPrivatePartialConfig (node_modules/@babel/core/lib/config/partial.js:85:55)

Test Suites: 1 failed, 1 total
Tests: 0 total
Snapshots: 0 total
Time: 1.818s
Ran all test suites.
npm ERR! Test failed. See above for more details.

When running npm test. My package.json (different because trying to solve issue):

Thanks

EDIT: copied exact package.json you guys have on GitHub, and only changed Jest to isolate. It seems Jest 24.5.0 has some problems with Babel.
Also, this makes builds fail when using Seed’s auto-deploy (with unit testing enabled)!
EDIT2: the problem arises from Jest 24.0.1 and up, 23.6.0 is fine. The should be a way to make Jest and Babel work fine together right?

0 Likes

#3

So I think I figured this out. Turns out I couldn’t find a way to make the plugins work (transform-runtime and source-map-support) but it seems to work fine without them. So with Jest and Babel updated, this is what my package.json (in part) and .babelrc look like:

package.json

“devDependencies”: {
@babel/cli”: “^7.2.3”,
@babel/core”: “^7.4.0”,
@babel/node”: “^7.2.2”,
@babel/plugin-transform-runtime”: “^7.4.0”,
@babel/preset-env”: “^7.4.2”,
“aws-sdk”: “^2.224.1”,
“babel-jest”: “^24.5.0”,
“babel-loader”: “^8.0.5”,
“babel-plugin-transform-runtime”: “^6.23.0”,
“jest”: “^24.5.0”,
“serverless-offline”: “^3.18.0”,
“serverless-webpack”: “^5.1.0”,
“webpack”: “^4.2.0”,
“webpack-node-externals”: “^1.6.0”
},
“dependencies”: {
“aws-sdk”: “^2.422.0”,
“babel-runtime”: “^6.26.0”,
“semver”: “^6.0.0”,
“serverless”: “^1.39.1”,
“source-map-support”: “^0.4.18”,
“stripe”: “^6.28.0”,
“uuid”: “^3.3.2”
},
“jest”: {
“testEnvironment”: “node”
}

.babelrc

{
“presets”: [
“jest”,
[
@babel/preset-env”,
{
“debug”: false,
“modules”: “commonjs”,
“targets”: {
“node”: “current”
},
“useBuiltIns”: false
}
]
]
}

And now it passes Jest tests!

2 Likes